Merge pull request #393 from QuarticCat/master

Some optimizations
pull/396/head
Wilfred Hughes 2022-09-28 23:11:57 +07:00 committed by GitHub
commit 7e102e1386
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 84 additions and 55 deletions

25
Cargo.lock generated

@ -20,15 +20,6 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "archery"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0a8da9bc4c4053ee067669762bcaeea6e241841295a2b6c948312dad6ef4cc02"
dependencies = [
"static_assertions",
]
[[package]] [[package]]
name = "atty" name = "atty"
version = "0.2.14" version = "0.2.14"
@ -204,7 +195,6 @@ dependencies = [
"radix-heap", "radix-heap",
"rayon", "rayon",
"regex", "regex",
"rpds",
"rustc-hash", "rustc-hash",
"strsim", "strsim",
"term_size", "term_size",
@ -535,15 +525,6 @@ version = "0.6.26"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49b3de9ec5dc0a3417da371aab17d729997c15010e7fd24ff707773a33bddb64" checksum = "49b3de9ec5dc0a3417da371aab17d729997c15010e7fd24ff707773a33bddb64"
[[package]]
name = "rpds"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "054e417ded02a19ae192c8c89412eaec7d1c2cdd826aa412565761d86ca6315e"
dependencies = [
"archery",
]
[[package]] [[package]]
name = "rustc-hash" name = "rustc-hash"
version = "1.1.0" version = "1.1.0"
@ -579,12 +560,6 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "static_assertions"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]] [[package]]
name = "strsim" name = "strsim"
version = "0.10.0" version = "0.10.0"

@ -42,7 +42,6 @@ walkdir = "2.3.2"
terminal_size = "0.2.1" terminal_size = "0.2.1"
const_format = "0.2.22" const_format = "0.2.22"
owo-colors = "3.3.0" owo-colors = "3.3.0"
rpds = "0.10.0"
wu-diff = "0.1.2" wu-diff = "0.1.2"
rayon = "1.5.2" rayon = "1.5.2"
tree_magic_mini = "3.0.3" tree_magic_mini = "3.0.3"
@ -65,6 +64,7 @@ version_check = "0.9.4"
# #
# https://doc.rust-lang.org/cargo/reference/profiles.html#release # https://doc.rust-lang.org/cargo/reference/profiles.html#release
debug = false debug = false
lto = "thin"
[[bin]] [[bin]]
name = "difft" name = "difft"

@ -44,8 +44,8 @@ fn shortest_vertex_path<'a, 'b>(
let (edge, next) = neighbour; let (edge, next) = neighbour;
let distance_to_next = distance + edge.cost(); let distance_to_next = distance + edge.cost();
let found_shorter_route = match &*next.predecessor.borrow() { let found_shorter_route = match next.predecessor.get() {
Some((prev_shortest, _)) => distance_to_next < *prev_shortest, Some((prev_shortest, _)) => distance_to_next < prev_shortest,
None => true, None => true,
}; };
@ -74,7 +74,7 @@ fn shortest_vertex_path<'a, 'b>(
let mut vertex_route: Vec<&'b Vertex<'a, 'b>> = vec![]; let mut vertex_route: Vec<&'b Vertex<'a, 'b>> = vec![];
while let Some((_, node)) = current { while let Some((_, node)) = current {
vertex_route.push(node); vertex_route.push(node);
current = *node.predecessor.borrow(); current = node.predecessor.get();
} }
vertex_route.reverse(); vertex_route.reverse();
@ -91,7 +91,7 @@ fn shortest_path_with_edges<'a, 'b>(
for vertex in route.iter().skip(1) { for vertex in route.iter().skip(1) {
let edge = edge_between(prev, vertex); let edge = edge_between(prev, vertex);
res.push((edge, prev.clone())); res.push((edge, *prev));
cost += edge.cost(); cost += edge.cost();
prev = vertex; prev = vertex;

@ -1,10 +1,9 @@
//! A graph representation for computing tree diffs. //! A graph representation for computing tree diffs.
use bumpalo::Bump; use bumpalo::Bump;
use rpds::Stack;
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use std::{ use std::{
cell::RefCell, cell::{Cell, RefCell},
cmp::min, cmp::min,
fmt, fmt,
hash::{Hash, Hasher}, hash::{Hash, Hasher},
@ -12,7 +11,10 @@ use std::{
use strsim::normalized_levenshtein; use strsim::normalized_levenshtein;
use crate::{ use crate::{
diff::changes::{insert_deep_unchanged, ChangeKind, ChangeMap}, diff::{
changes::{insert_deep_unchanged, ChangeKind, ChangeMap},
stack::Stack,
},
parse::syntax::{AtomKind, Syntax, SyntaxId}, parse::syntax::{AtomKind, Syntax, SyntaxId},
}; };
use Edge::*; use Edge::*;
@ -48,7 +50,7 @@ use Edge::*;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Vertex<'a, 'b> { pub struct Vertex<'a, 'b> {
pub neighbours: RefCell<Option<Vec<(Edge, &'b 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 lhs_syntax: Option<&'a Syntax<'a>>,
pub rhs_syntax: Option<&'a Syntax<'a>>, pub rhs_syntax: Option<&'a Syntax<'a>>,
parents: Stack<EnteredDelimiter<'a>>, parents: Stack<EnteredDelimiter<'a>>,
@ -258,7 +260,7 @@ impl<'a, 'b> Vertex<'a, 'b> {
let parents = Stack::new(); let parents = Stack::new();
Vertex { Vertex {
neighbours: RefCell::new(None), neighbours: RefCell::new(None),
predecessor: RefCell::new(None), predecessor: Cell::new(None),
lhs_syntax, lhs_syntax,
rhs_syntax, rhs_syntax,
parents, parents,
@ -442,7 +444,7 @@ pub fn get_set_neighbours<'syn, 'b>(
allocate_if_new( allocate_if_new(
Vertex { Vertex {
neighbours: RefCell::new(None), neighbours: RefCell::new(None),
predecessor: RefCell::new(None), predecessor: Cell::new(None),
lhs_syntax: lhs_parent.next_sibling(), lhs_syntax: lhs_parent.next_sibling(),
rhs_syntax: rhs_parent.next_sibling(), rhs_syntax: rhs_parent.next_sibling(),
can_pop_either: can_pop_either_parent(&parents_next), can_pop_either: can_pop_either_parent(&parents_next),
@ -467,7 +469,7 @@ pub fn get_set_neighbours<'syn, 'b>(
allocate_if_new( allocate_if_new(
Vertex { Vertex {
neighbours: RefCell::new(None), neighbours: RefCell::new(None),
predecessor: RefCell::new(None), predecessor: Cell::new(None),
lhs_syntax: lhs_parent.next_sibling(), lhs_syntax: lhs_parent.next_sibling(),
rhs_syntax: v.rhs_syntax, rhs_syntax: v.rhs_syntax,
can_pop_either: can_pop_either_parent(&parents_next), can_pop_either: can_pop_either_parent(&parents_next),
@ -492,7 +494,7 @@ pub fn get_set_neighbours<'syn, 'b>(
allocate_if_new( allocate_if_new(
Vertex { Vertex {
neighbours: RefCell::new(None), neighbours: RefCell::new(None),
predecessor: RefCell::new(None), predecessor: Cell::new(None),
lhs_syntax: v.lhs_syntax, lhs_syntax: v.lhs_syntax,
rhs_syntax: rhs_parent.next_sibling(), rhs_syntax: rhs_parent.next_sibling(),
can_pop_either: can_pop_either_parent(&parents_next), can_pop_either: can_pop_either_parent(&parents_next),
@ -519,7 +521,7 @@ pub fn get_set_neighbours<'syn, 'b>(
allocate_if_new( allocate_if_new(
Vertex { Vertex {
neighbours: RefCell::new(None), neighbours: RefCell::new(None),
predecessor: RefCell::new(None), predecessor: Cell::new(None),
lhs_syntax: lhs_syntax.next_sibling(), lhs_syntax: lhs_syntax.next_sibling(),
rhs_syntax: rhs_syntax.next_sibling(), rhs_syntax: rhs_syntax.next_sibling(),
parents: v.parents.clone(), parents: v.parents.clone(),
@ -565,7 +567,7 @@ pub fn get_set_neighbours<'syn, 'b>(
allocate_if_new( allocate_if_new(
Vertex { Vertex {
neighbours: RefCell::new(None), neighbours: RefCell::new(None),
predecessor: RefCell::new(None), predecessor: Cell::new(None),
lhs_syntax: lhs_next, lhs_syntax: lhs_next,
rhs_syntax: rhs_next, rhs_syntax: rhs_next,
parents: parents_next, parents: parents_next,
@ -603,7 +605,7 @@ pub fn get_set_neighbours<'syn, 'b>(
allocate_if_new( allocate_if_new(
Vertex { Vertex {
neighbours: RefCell::new(None), neighbours: RefCell::new(None),
predecessor: RefCell::new(None), predecessor: Cell::new(None),
lhs_syntax: lhs_syntax.next_sibling(), lhs_syntax: lhs_syntax.next_sibling(),
rhs_syntax: rhs_syntax.next_sibling(), rhs_syntax: rhs_syntax.next_sibling(),
parents: v.parents.clone(), parents: v.parents.clone(),
@ -633,7 +635,7 @@ pub fn get_set_neighbours<'syn, 'b>(
allocate_if_new( allocate_if_new(
Vertex { Vertex {
neighbours: RefCell::new(None), neighbours: RefCell::new(None),
predecessor: RefCell::new(None), predecessor: Cell::new(None),
lhs_syntax: lhs_syntax.next_sibling(), lhs_syntax: lhs_syntax.next_sibling(),
rhs_syntax: v.rhs_syntax, rhs_syntax: v.rhs_syntax,
parents: v.parents.clone(), parents: v.parents.clone(),
@ -659,7 +661,7 @@ pub fn get_set_neighbours<'syn, 'b>(
allocate_if_new( allocate_if_new(
Vertex { Vertex {
neighbours: RefCell::new(None), neighbours: RefCell::new(None),
predecessor: RefCell::new(None), predecessor: Cell::new(None),
lhs_syntax: lhs_next, lhs_syntax: lhs_next,
rhs_syntax: v.rhs_syntax, rhs_syntax: v.rhs_syntax,
parents: parents_next, parents: parents_next,
@ -687,7 +689,7 @@ pub fn get_set_neighbours<'syn, 'b>(
allocate_if_new( allocate_if_new(
Vertex { Vertex {
neighbours: RefCell::new(None), neighbours: RefCell::new(None),
predecessor: RefCell::new(None), predecessor: Cell::new(None),
lhs_syntax: v.lhs_syntax, lhs_syntax: v.lhs_syntax,
rhs_syntax: rhs_syntax.next_sibling(), rhs_syntax: rhs_syntax.next_sibling(),
parents: v.parents.clone(), parents: v.parents.clone(),
@ -713,7 +715,7 @@ pub fn get_set_neighbours<'syn, 'b>(
allocate_if_new( allocate_if_new(
Vertex { Vertex {
neighbours: RefCell::new(None), neighbours: RefCell::new(None),
predecessor: RefCell::new(None), predecessor: Cell::new(None),
lhs_syntax: v.lhs_syntax, lhs_syntax: v.lhs_syntax,
rhs_syntax: rhs_next, rhs_syntax: rhs_next,
parents: parents_next, parents: parents_next,
@ -729,7 +731,7 @@ pub fn get_set_neighbours<'syn, 'b>(
} }
} }
assert!( assert!(
res.len() > 0, !res.is_empty(),
"Must always find some next steps if node is not the end" "Must always find some next steps if node is not the end"
); );

@ -3,4 +3,5 @@ pub mod dijkstra;
mod graph; mod graph;
pub mod myers_diff; pub mod myers_diff;
pub mod sliders; pub mod sliders;
mod stack;
pub mod unchanged; pub mod unchanged;

@ -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()
}
}

@ -130,7 +130,7 @@ pub fn split_and_apply(
let end_col = span.end_col as usize; let end_col = span.end_col as usize;
// The remaining spans are beyond the end of this line_part. // 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; break;
} }

@ -55,7 +55,6 @@ use diff::sliders::fix_all_sliders;
use options::{DisplayMode, DisplayOptions, FileArgument, Mode, DEFAULT_TAB_WIDTH}; use options::{DisplayMode, DisplayOptions, FileArgument, Mode, DEFAULT_TAB_WIDTH};
use owo_colors::OwoColorize; use owo_colors::OwoColorize;
use rayon::prelude::*; use rayon::prelude::*;
use std::path::PathBuf;
use std::{env, path::Path}; use std::{env, path::Path};
use summary::{DiffResult, FileContent}; use summary::{DiffResult, FileContent};
use syntax::init_next_prev; use syntax::init_next_prev;
@ -142,7 +141,7 @@ fn main() {
print!("{}", name); print!("{}", name);
let mut extensions: Vec<&str> = (*extensions).into(); let mut extensions: Vec<&str> = (*extensions).into();
extensions.sort(); extensions.sort_unstable();
for extension in extensions { for extension in extensions {
print!(" .{}", extension); print!(" .{}", extension);
@ -179,8 +178,8 @@ fn main() {
options::FileArgument::NamedPath(rhs_path), options::FileArgument::NamedPath(rhs_path),
) if lhs_path.is_dir() && rhs_path.is_dir() => { ) if lhs_path.is_dir() && rhs_path.is_dir() => {
diff_directories( diff_directories(
&lhs_path, lhs_path,
&rhs_path, rhs_path,
&display_options, &display_options,
graph_limit, graph_limit,
byte_limit, byte_limit,
@ -403,8 +402,8 @@ fn diff_file_content(
/// When more than one file is modified, the hg extdiff extension passes directory /// When more than one file is modified, the hg extdiff extension passes directory
/// paths with the all the modified files. /// paths with the all the modified files.
fn diff_directories<'a>( fn diff_directories<'a>(
lhs_dir: &'a PathBuf, lhs_dir: &'a Path,
rhs_dir: &'a PathBuf, rhs_dir: &'a Path,
display_options: &DisplayOptions, display_options: &DisplayOptions,
graph_limit: usize, graph_limit: usize,
byte_limit: usize, byte_limit: usize,

@ -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, Bash,
&[ &[

@ -246,7 +246,7 @@ impl<'a> Syntax<'a> {
) -> &'a Syntax<'a> { ) -> &'a Syntax<'a> {
// If a parser hasn't cleaned up \r on CRLF files with // If a parser hasn't cleaned up \r on CRLF files with
// comments, discard it. // comments, discard it.
if content.ends_with("\r") { if content.ends_with('\r') {
content = &content[..content.len() - 1]; content = &content[..content.len() - 1];
} }