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

@ -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"
@ -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"

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

@ -1,10 +1,9 @@
//! 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},
@ -12,7 +11,10 @@ use std::{
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::*;
@ -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>>,
@ -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,
@ -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),
@ -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),
@ -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),
@ -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(),
@ -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,
@ -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(),
@ -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(),
@ -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,
@ -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(),
@ -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,
@ -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"
);

@ -3,4 +3,5 @@ pub mod dijkstra;
mod graph;
pub mod myers_diff;
pub mod sliders;
mod stack;
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;
// 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;
}

@ -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;
@ -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);
@ -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,
@ -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,

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

@ -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];
}