tags, highlight: Avoid completely deallocating buffers when shrinking
This commit is contained in:
parent
32f69dbe15
commit
f91b19c089
2 changed files with 20 additions and 12 deletions
|
|
@ -900,12 +900,8 @@ impl HtmlRenderer {
|
|||
}
|
||||
|
||||
pub fn reset(&mut self) {
|
||||
self.html.truncate(BUFFER_HTML_RESERVE_CAPACITY);
|
||||
self.line_offsets.truncate(BUFFER_LINES_RESERVE_CAPACITY);
|
||||
self.html.shrink_to_fit();
|
||||
self.line_offsets.shrink_to_fit();
|
||||
self.html.clear();
|
||||
self.line_offsets.clear();
|
||||
shrink_and_clear(&mut self.html, BUFFER_HTML_RESERVE_CAPACITY);
|
||||
shrink_and_clear(&mut self.line_offsets, BUFFER_LINES_RESERVE_CAPACITY);
|
||||
self.line_offsets.push(0);
|
||||
}
|
||||
|
||||
|
|
@ -1069,3 +1065,11 @@ fn injection_for_match<'a>(
|
|||
|
||||
(language_name, content_node, include_children)
|
||||
}
|
||||
|
||||
fn shrink_and_clear<T>(vec: &mut Vec<T>, capacity: usize) {
|
||||
if vec.len() > capacity {
|
||||
vec.truncate(capacity);
|
||||
vec.shrink_to_fit();
|
||||
}
|
||||
vec.clear();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -119,12 +119,8 @@ pub extern "C" fn ts_tagger_tag(
|
|||
let scope_name = unsafe { unwrap(CStr::from_ptr(scope_name).to_str()) };
|
||||
|
||||
if let Some(config) = tagger.languages.get(scope_name) {
|
||||
buffer.tags.truncate(BUFFER_TAGS_RESERVE_CAPACITY);
|
||||
buffer.docs.truncate(BUFFER_DOCS_RESERVE_CAPACITY);
|
||||
buffer.tags.shrink_to_fit();
|
||||
buffer.docs.shrink_to_fit();
|
||||
buffer.tags.clear();
|
||||
buffer.docs.clear();
|
||||
shrink_and_clear(&mut buffer.tags, BUFFER_TAGS_RESERVE_CAPACITY);
|
||||
shrink_and_clear(&mut buffer.docs, BUFFER_DOCS_RESERVE_CAPACITY);
|
||||
|
||||
let source_code = unsafe { slice::from_raw_parts(source_code, source_code_len as usize) };
|
||||
let cancellation_flag = unsafe { cancellation_flag.as_ref() };
|
||||
|
|
@ -262,3 +258,11 @@ fn unwrap<T, E: fmt::Display>(result: Result<T, E>) -> T {
|
|||
abort();
|
||||
})
|
||||
}
|
||||
|
||||
fn shrink_and_clear<T>(vec: &mut Vec<T>, capacity: usize) {
|
||||
if vec.len() > capacity {
|
||||
vec.truncate(capacity);
|
||||
vec.shrink_to_fit();
|
||||
}
|
||||
vec.clear();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue