| 1 |
//! This modules encapsulates a tiny bit of unsafe code that |
| 2 |
//! makes diffing significantly faster and more ergonomic to implement. |
| 3 |
//! This code is necessary because diffing requires quick random |
| 4 |
//! access to the lines of the text that is being diffed. |
| 5 |
//! |
| 6 |
//! Therefore it is best to collect the `Rope::lines` iterator into a vec |
| 7 |
//! first because access to the vec is `O(1)` where `Rope::line` is `O(log N)`. |
| 8 |
//! However this process can allocate a (potentially quite large) vector. |
| 9 |
//! |
| 10 |
//! To avoid reallocation for every diff, the vector is reused. |
| 11 |
//! However the RopeSlice references the original rope and therefore forms a self-referential data structure. |
| 12 |
//! A transmute is used to change the lifetime of the slice to static to circumvent that project. |
| 13 |
use std::mem::transmute; |
| 14 |
|
| 15 |
use helix_core::{Rope, RopeSlice}; |
| 16 |
use imara_diff::{InternedInput, Interner}; |
| 17 |
|
| 18 |
use super::{MAX_DIFF_BYTES, MAX_DIFF_LINES}; |
| 19 |
|
| 20 |
/// A cache that stores the `lines` of a rope as a vector. |
| 21 |
/// It allows safely reusing the allocation of the vec when updating the rope |
| 22 |
pub(crate) struct InternedRopeLines { |
| 23 |
diff_base: Box<Rope>, |
| 24 |
doc: Box<Rope>, |
| 25 |
num_tokens_diff_base: u32, |
| 26 |
interned: InternedInput<RopeSlice<'static>>, |
| 27 |
} |
| 28 |
|
| 29 |
impl InternedRopeLines { |
| 30 |
pub fn new(diff_base: Rope, doc: Rope) -> InternedRopeLines { |
| 31 |
let mut res = InternedRopeLines { |
| 32 |
interned: InternedInput { |
| 33 |
before: Vec::with_capacity(diff_base.len_lines()), |
| 34 |
after: Vec::with_capacity(doc.len_lines()), |
| 35 |
interner: Interner::new(diff_base.len_lines() + doc.len_lines()), |
| 36 |
}, |
| 37 |
diff_base: Box::new(diff_base), |
| 38 |
doc: Box::new(doc), |
| 39 |
// will be populated by update_diff_base_impl |
| 40 |
num_tokens_diff_base: 0, |
| 41 |
}; |
| 42 |
res.update_diff_base_impl(); |
| 43 |
res |
| 44 |
} |
| 45 |
|
| 46 |
pub fn doc(&self) -> Rope { |
| 47 |
Rope::clone(&*self.doc) |
| 48 |
} |
| 49 |
|
| 50 |
pub fn diff_base(&self) -> Rope { |
| 51 |
Rope::clone(&*self.diff_base) |
| 52 |
} |
| 53 |
|
| 54 |
/// Updates the `diff_base` and optionally the document if `doc` is not None |
| 55 |
pub fn update_diff_base(&mut self, diff_base: Rope, doc: Option<Rope>) { |
| 56 |
self.interned.clear(); |
| 57 |
*self.diff_base = diff_base; |
| 58 |
if let Some(doc) = doc { |
| 59 |
*self.doc = doc |
| 60 |
} |
| 61 |
if !self.is_too_large() { |
| 62 |
self.update_diff_base_impl(); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
/// Updates the `doc` without reinterning the `diff_base`, this function |
| 67 |
/// is therefore significantly faster than `update_diff_base` when only the document changes. |
| 68 |
pub fn update_doc(&mut self, doc: Rope) { |
| 69 |
// Safety: we clear any tokens that were added after |
| 70 |
// the interning of `self.diff_base` finished so |
| 71 |
// all lines that refer to `self.doc` have been purged. |
| 72 |
|
| 73 |
self.interned |
| 74 |
.interner |
| 75 |
.erase_tokens_after(self.num_tokens_diff_base.into()); |
| 76 |
|
| 77 |
*self.doc = doc; |
| 78 |
if self.is_too_large() { |
| 79 |
self.interned.after.clear(); |
| 80 |
} else { |
| 81 |
self.update_doc_impl(); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
fn update_diff_base_impl(&mut self) { |
| 86 |
// Safety: This transmute is safe because it only transmutes a lifetime, which has no effect. |
| 87 |
// The backing storage for the RopeSlices referred to by the lifetime is stored in `self.diff_base`. |
| 88 |
// Therefore as long as `self.diff_base` is not dropped/replaced this memory remains valid. |
| 89 |
// `self.diff_base` is only changed in `self.update_diff_base`, which clears the interner. |
| 90 |
// When the interned lines are exposed to consumer in `self.diff_input`, the lifetime is bounded to a reference to self. |
| 91 |
// That means that on calls to update there exist no references to `self.interned`. |
| 92 |
let before = self |
| 93 |
.diff_base |
| 94 |
.lines() |
| 95 |
.map(|line: RopeSlice| -> RopeSlice<'static> { unsafe { transmute(line) } }); |
| 96 |
self.interned.update_before(before); |
| 97 |
self.num_tokens_diff_base = self.interned.interner.num_tokens(); |
| 98 |
// the has to be interned again because the interner was fully cleared |
| 99 |
self.update_doc_impl() |
| 100 |
} |
| 101 |
|
| 102 |
fn update_doc_impl(&mut self) { |
| 103 |
// Safety: This transmute is save because it only transmutes a lifetime, which has no effect. |
| 104 |
// The backing storage for the RopeSlices referred to by the lifetime is stored in `self.doc`. |
| 105 |
// Therefore as long as `self.doc` is not dropped/replaced this memory remains valid. |
| 106 |
// `self.doc` is only changed in `self.update_doc`, which clears the interner. |
| 107 |
// When the interned lines are exposed to consumer in `self.diff_input`, the lifetime is bounded to a reference to self. |
| 108 |
// That means that on calls to update there exist no references to `self.interned`. |
| 109 |
let after = self |
| 110 |
.doc |
| 111 |
.lines() |
| 112 |
.map(|line: RopeSlice| -> RopeSlice<'static> { unsafe { transmute(line) } }); |
| 113 |
self.interned.update_after(after); |
| 114 |
} |
| 115 |
|
| 116 |
fn is_too_large(&self) -> bool { |
| 117 |
// bound both lines and bytes to avoid huge files with few (but huge) lines |
| 118 |
// or huge file with tiny lines. While this makes no difference to |
| 119 |
// diff itself (the diff performance only depends on the number of tokens) |
| 120 |
// the interning runtime depends mostly on filesize and is actually dominant |
| 121 |
// for large files |
| 122 |
self.doc.len_lines() > MAX_DIFF_LINES |
| 123 |
|| self.diff_base.len_lines() > MAX_DIFF_LINES |
| 124 |
|| self.doc.len_bytes() > MAX_DIFF_BYTES |
| 125 |
|| self.diff_base.len_bytes() > MAX_DIFF_BYTES |
| 126 |
} |
| 127 |
|
| 128 |
/// Returns the `InternedInput` for performing the diff. |
| 129 |
/// If `diff_base` or `doc` is so large that performing a diff could slow the editor |
| 130 |
/// this function returns `None`. |
| 131 |
pub fn interned_lines(&self) -> Option<&InternedInput<RopeSlice<'_>>> { |
| 132 |
if self.is_too_large() { |
| 133 |
None |
| 134 |
} else { |
| 135 |
Some(&self.interned) |
| 136 |
} |
| 137 |
} |
| 138 |
} |