Introduce a new fingerprint field to TextSummary

This is calculated in `Rope` and uses the `bromberg_sl2` homomorphic
hash function to determine the fingerprint of a single chunk and
compose each chunk fingerprint into a single fingerprint for the entire
rope that is equivalent to hashing all the rope's bytes at once.
This commit is contained in:
Antonio Scandurra 2022-06-17 11:45:26 +02:00
parent cef85f5d84
commit c31a233aad
6 changed files with 33 additions and 22 deletions

View file

@ -16,6 +16,8 @@ collections = { path = "../collections" }
sum_tree = { path = "../sum_tree" }
anyhow = "1.0.38"
arrayvec = "0.7.1"
digest = { version = "0.9", features = ["std"] }
bromberg_sl2 = "0.6"
lazy_static = "1.4"
log = { version = "0.4.16", features = ["kv_unstable_serde"] }
parking_lot = "0.11"

View file

@ -2,6 +2,7 @@ use crate::PointUtf16;
use super::Point;
use arrayvec::ArrayString;
use bromberg_sl2::HashMatrix;
use smallvec::SmallVec;
use std::{cmp, fmt, io, mem, ops::Range, str};
use sum_tree::{Bias, Dimension, SumTree};
@ -725,6 +726,7 @@ pub struct TextSummary {
pub last_line_chars: u32,
pub longest_row: u32,
pub longest_row_chars: u32,
pub fingerprint: HashMatrix,
}
impl<'a> From<&'a str> for TextSummary {
@ -764,6 +766,7 @@ impl<'a> From<&'a str> for TextSummary {
last_line_chars,
longest_row,
longest_row_chars,
fingerprint: bromberg_sl2::hash_strict(text.as_bytes()),
}
}
}
@ -810,6 +813,7 @@ impl<'a> std::ops::AddAssign<&'a Self> for TextSummary {
self.bytes += other.bytes;
self.lines += other.lines;
self.lines_utf16 += other.lines_utf16;
self.fingerprint = self.fingerprint * other.fingerprint;
}
}

View file

@ -226,6 +226,7 @@ fn test_text_summary_for_range() {
last_line_chars: 0,
longest_row: 0,
longest_row_chars: 1,
fingerprint: bromberg_sl2::hash_strict(b"b\n")
}
);
assert_eq!(
@ -238,6 +239,7 @@ fn test_text_summary_for_range() {
last_line_chars: 0,
longest_row: 2,
longest_row_chars: 4,
fingerprint: bromberg_sl2::hash_strict(b"b\nefg\nhklm\n")
}
);
assert_eq!(
@ -250,6 +252,7 @@ fn test_text_summary_for_range() {
last_line_chars: 1,
longest_row: 3,
longest_row_chars: 6,
fingerprint: bromberg_sl2::hash_strict(b"ab\nefg\nhklm\nnopqrs\nt")
}
);
assert_eq!(
@ -262,6 +265,7 @@ fn test_text_summary_for_range() {
last_line_chars: 3,
longest_row: 3,
longest_row_chars: 6,
fingerprint: bromberg_sl2::hash_strict(b"ab\nefg\nhklm\nnopqrs\ntuv")
}
);
assert_eq!(
@ -274,6 +278,7 @@ fn test_text_summary_for_range() {
last_line_chars: 3,
longest_row: 1,
longest_row_chars: 6,
fingerprint: bromberg_sl2::hash_strict(b"hklm\nnopqrs\ntuv")
}
);
}