Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some optimizations #393

Merged
merged 6 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 0 additions & 25 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ walkdir = "2.3.2"
terminal_size = "0.2.1"
const_format = "0.2.22"
owo-colors = "3.3.0"
rpds = "0.10.0"
wu-diff = "0.1.2"
rayon = "1.5.2"
tree_magic_mini = "3.0.3"
Expand All @@ -65,6 +64,7 @@ version_check = "0.9.4"
#
# https://doc.rust-lang.org/cargo/reference/profiles.html#release
debug = false
lto = "thin"

[[bin]]
name = "difft"
Expand Down
8 changes: 4 additions & 4 deletions src/diff/dijkstra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ fn shortest_vertex_path<'a, 'b>(
let (edge, next) = neighbour;
let distance_to_next = distance + edge.cost();

let found_shorter_route = match &*next.predecessor.borrow() {
Some((prev_shortest, _)) => distance_to_next < *prev_shortest,
let found_shorter_route = match next.predecessor.get() {
Some((prev_shortest, _)) => distance_to_next < prev_shortest,
None => true,
};

Expand Down Expand Up @@ -74,7 +74,7 @@ fn shortest_vertex_path<'a, 'b>(
let mut vertex_route: Vec<&'b Vertex<'a, 'b>> = vec![];
while let Some((_, node)) = current {
vertex_route.push(node);
current = *node.predecessor.borrow();
current = node.predecessor.get();
}

vertex_route.reverse();
Expand All @@ -91,7 +91,7 @@ fn shortest_path_with_edges<'a, 'b>(

for vertex in route.iter().skip(1) {
let edge = edge_between(prev, vertex);
res.push((edge, prev.clone()));
res.push((edge, *prev));
cost += edge.cost();

prev = vertex;
Expand Down
34 changes: 18 additions & 16 deletions src/diff/graph.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
//! A graph representation for computing tree diffs.

use bumpalo::Bump;
use rpds::Stack;
use rustc_hash::FxHashMap;
use std::{
cell::RefCell,
cell::{Cell, RefCell},
cmp::min,
fmt,
hash::{Hash, Hasher},
};
use strsim::normalized_levenshtein;

use crate::{
diff::changes::{insert_deep_unchanged, ChangeKind, ChangeMap},
diff::{
changes::{insert_deep_unchanged, ChangeKind, ChangeMap},
stack::Stack,
},
parse::syntax::{AtomKind, Syntax, SyntaxId},
};
use Edge::*;
Expand Down Expand Up @@ -48,7 +50,7 @@ use Edge::*;
#[derive(Debug, Clone)]
pub struct Vertex<'a, 'b> {
pub neighbours: RefCell<Option<Vec<(Edge, &'b Vertex<'a, 'b>)>>>,
pub predecessor: RefCell<Option<(u64, &'b Vertex<'a, 'b>)>>,
pub predecessor: Cell<Option<(u64, &'b Vertex<'a, 'b>)>>,
pub lhs_syntax: Option<&'a Syntax<'a>>,
pub rhs_syntax: Option<&'a Syntax<'a>>,
parents: Stack<EnteredDelimiter<'a>>,
Expand Down Expand Up @@ -258,7 +260,7 @@ impl<'a, 'b> Vertex<'a, 'b> {
let parents = Stack::new();
Vertex {
neighbours: RefCell::new(None),
predecessor: RefCell::new(None),
predecessor: Cell::new(None),
lhs_syntax,
rhs_syntax,
parents,
Expand Down Expand Up @@ -442,7 +444,7 @@ pub fn get_set_neighbours<'syn, 'b>(
allocate_if_new(
Vertex {
neighbours: RefCell::new(None),
predecessor: RefCell::new(None),
predecessor: Cell::new(None),
lhs_syntax: lhs_parent.next_sibling(),
rhs_syntax: rhs_parent.next_sibling(),
can_pop_either: can_pop_either_parent(&parents_next),
Expand All @@ -467,7 +469,7 @@ pub fn get_set_neighbours<'syn, 'b>(
allocate_if_new(
Vertex {
neighbours: RefCell::new(None),
predecessor: RefCell::new(None),
predecessor: Cell::new(None),
lhs_syntax: lhs_parent.next_sibling(),
rhs_syntax: v.rhs_syntax,
can_pop_either: can_pop_either_parent(&parents_next),
Expand All @@ -492,7 +494,7 @@ pub fn get_set_neighbours<'syn, 'b>(
allocate_if_new(
Vertex {
neighbours: RefCell::new(None),
predecessor: RefCell::new(None),
predecessor: Cell::new(None),
lhs_syntax: v.lhs_syntax,
rhs_syntax: rhs_parent.next_sibling(),
can_pop_either: can_pop_either_parent(&parents_next),
Expand All @@ -519,7 +521,7 @@ pub fn get_set_neighbours<'syn, 'b>(
allocate_if_new(
Vertex {
neighbours: RefCell::new(None),
predecessor: RefCell::new(None),
predecessor: Cell::new(None),
lhs_syntax: lhs_syntax.next_sibling(),
rhs_syntax: rhs_syntax.next_sibling(),
parents: v.parents.clone(),
Expand Down Expand Up @@ -565,7 +567,7 @@ pub fn get_set_neighbours<'syn, 'b>(
allocate_if_new(
Vertex {
neighbours: RefCell::new(None),
predecessor: RefCell::new(None),
predecessor: Cell::new(None),
lhs_syntax: lhs_next,
rhs_syntax: rhs_next,
parents: parents_next,
Expand Down Expand Up @@ -603,7 +605,7 @@ pub fn get_set_neighbours<'syn, 'b>(
allocate_if_new(
Vertex {
neighbours: RefCell::new(None),
predecessor: RefCell::new(None),
predecessor: Cell::new(None),
lhs_syntax: lhs_syntax.next_sibling(),
rhs_syntax: rhs_syntax.next_sibling(),
parents: v.parents.clone(),
Expand Down Expand Up @@ -633,7 +635,7 @@ pub fn get_set_neighbours<'syn, 'b>(
allocate_if_new(
Vertex {
neighbours: RefCell::new(None),
predecessor: RefCell::new(None),
predecessor: Cell::new(None),
lhs_syntax: lhs_syntax.next_sibling(),
rhs_syntax: v.rhs_syntax,
parents: v.parents.clone(),
Expand All @@ -659,7 +661,7 @@ pub fn get_set_neighbours<'syn, 'b>(
allocate_if_new(
Vertex {
neighbours: RefCell::new(None),
predecessor: RefCell::new(None),
predecessor: Cell::new(None),
lhs_syntax: lhs_next,
rhs_syntax: v.rhs_syntax,
parents: parents_next,
Expand Down Expand Up @@ -687,7 +689,7 @@ pub fn get_set_neighbours<'syn, 'b>(
allocate_if_new(
Vertex {
neighbours: RefCell::new(None),
predecessor: RefCell::new(None),
predecessor: Cell::new(None),
lhs_syntax: v.lhs_syntax,
rhs_syntax: rhs_syntax.next_sibling(),
parents: v.parents.clone(),
Expand All @@ -713,7 +715,7 @@ pub fn get_set_neighbours<'syn, 'b>(
allocate_if_new(
Vertex {
neighbours: RefCell::new(None),
predecessor: RefCell::new(None),
predecessor: Cell::new(None),
lhs_syntax: v.lhs_syntax,
rhs_syntax: rhs_next,
parents: parents_next,
Expand All @@ -729,7 +731,7 @@ pub fn get_set_neighbours<'syn, 'b>(
}
}
assert!(
res.len() > 0,
!res.is_empty(),
"Must always find some next steps if node is not the end"
);

Expand Down
1 change: 1 addition & 0 deletions src/diff/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ pub mod dijkstra;
mod graph;
pub mod myers_diff;
pub mod sliders;
mod stack;
pub mod unchanged;
52 changes: 52 additions & 0 deletions src/diff/stack.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use std::rc::Rc;

#[derive(Debug, Clone, Default, PartialEq, Eq)]
struct Node<T> {
val: T,
next: Option<Rc<Node<T>>>,
}

#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Stack<T> {
head: Option<Rc<Node<T>>>,
}

impl<T> Stack<T> {
pub fn new() -> Self {
Self { head: None }
}

pub fn peek(&self) -> Option<&T> {
self.head.as_deref().map(|n| &n.val)
}

pub fn pop(&self) -> Option<Stack<T>> {
self.head.as_deref().map(|n| Self {
head: n.next.clone(),
})
}

pub fn push(&self, v: T) -> Stack<T> {
Self {
head: Some(Rc::new(Node {
val: v,
next: self.head.clone(),
})),
}
}

// O(n)
pub fn size(&self) -> usize {
let mut res = 0;
let mut node = &self.head;
while let Some(next) = node {
res += 1;
node = &next.next;
}
res
}

pub fn is_empty(&self) -> bool {
self.head.is_none()
}
}
2 changes: 1 addition & 1 deletion src/display/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ pub fn split_and_apply(
let end_col = span.end_col as usize;

// The remaining spans are beyond the end of this line_part.
if start_col >= part_start + byte_len(&line_part) {
if start_col >= part_start + byte_len(line_part) {
break;
}

Expand Down
11 changes: 5 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ use diff::sliders::fix_all_sliders;
use options::{DisplayMode, DisplayOptions, FileArgument, Mode, DEFAULT_TAB_WIDTH};
use owo_colors::OwoColorize;
use rayon::prelude::*;
use std::path::PathBuf;
use std::{env, path::Path};
use summary::{DiffResult, FileContent};
use syntax::init_next_prev;
Expand Down Expand Up @@ -142,7 +141,7 @@ fn main() {
print!("{}", name);

let mut extensions: Vec<&str> = (*extensions).into();
extensions.sort();
extensions.sort_unstable();

for extension in extensions {
print!(" .{}", extension);
Expand Down Expand Up @@ -179,8 +178,8 @@ fn main() {
options::FileArgument::NamedPath(rhs_path),
) if lhs_path.is_dir() && rhs_path.is_dir() => {
diff_directories(
&lhs_path,
&rhs_path,
lhs_path,
rhs_path,
&display_options,
graph_limit,
byte_limit,
Expand Down Expand Up @@ -403,8 +402,8 @@ fn diff_file_content(
/// When more than one file is modified, the hg extdiff extension passes directory
/// paths with the all the modified files.
fn diff_directories<'a>(
lhs_dir: &'a PathBuf,
rhs_dir: &'a PathBuf,
lhs_dir: &'a Path,
rhs_dir: &'a Path,
display_options: &DisplayOptions,
graph_limit: usize,
byte_limit: usize,
Expand Down
2 changes: 1 addition & 1 deletion src/parse/guess_language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ pub fn language_name(language: Language) -> &'static str {
}
}

pub const LANG_EXTENSIONS: &'static [(Language, &[&str])] = &[
pub const LANG_EXTENSIONS: &[(Language, &[&str])] = &[
(
Bash,
&[
Expand Down
2 changes: 1 addition & 1 deletion src/parse/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ impl<'a> Syntax<'a> {
) -> &'a Syntax<'a> {
// If a parser hasn't cleaned up \r on CRLF files with
// comments, discard it.
if content.ends_with("\r") {
if content.ends_with('\r') {
content = &content[..content.len() - 1];
}

Expand Down