дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-06 01:57:13 +02:00
fix(syntax): share the memoized outline/folding instead of cloning it (#8309)
SyntaxCache::memoized stored each result behind an Arc but deep-cloned the Vec out of it on every cache hit, which for the outline (a tree of Strings) cost milliseconds on a 2 MB file. memoized now returns the Arc, and outline() and folding() return Arc<Vec<_>>; the two IPC commands that own and extend their result take it out once with Arc::unwrap_or_clone. Results are unchanged; outline_cached_hit went from 4.68 ms to 80 ns and folding_cached_hit from 21.7 µs to 72 ns in the criterion benches. Co-authored-by: Ttimmahlax <[email protected]>
Цей коміт міститься в:
1 джерело
0ca4c7a21b
коміт
fe04de3862
3 files changed
+34
-20
No files matched your search
@@ -8,6 +8,7 @@ use ide_text::LineEnding;
|
||||
use ide_workspace::settings::SettingsScope;
|
||||
use ide_workspace::{DocumentEncoding, DocumentInfo, DocumentLanguage, FileDefaults, HistoryEntry};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Opens a read-only virtual document (see `lsp_virtual_document`) as an
|
||||
/// editor buffer, so syntax features and language-server requests work in it.
|
||||
@@ -165,7 +166,7 @@ pub async fn doc_outline(host: &Host, window: &str, doc: DocId) -> IdeResult<Vec
|
||||
items
|
||||
}
|
||||
None if snap.large && !matches!(snap.language.as_str(), "markdown" | "mdx") => Vec::new(),
|
||||
None => syntax(),
|
||||
None => Arc::unwrap_or_clone(syntax()),
|
||||
})
|
||||
})
|
||||
.await
|
||||
@@ -186,7 +187,7 @@ pub async fn doc_folding(host: &Host, window: &str, doc: DocId) -> IdeResult<Vec
|
||||
if snap.large {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
let mut folds = project.syntax.folding(doc, &snap.language, snap.version, &snap.text);
|
||||
let mut folds = Arc::unwrap_or_clone(project.syntax.folding(doc, &snap.language, snap.version, &snap.text));
|
||||
if let Some(plugins) = crate::plugins::editor_providers_for(crate::plugins::ProviderKind::Folding) {
|
||||
let d = ide_plugin::ProviderDocument {
|
||||
path: project.documents.identity(doc)?.0,
|
||||
|
||||
@@ -186,7 +186,8 @@ impl SyntaxCache {
|
||||
stats
|
||||
}
|
||||
|
||||
pub fn outline(&self, doc: DocId, lang: &LanguageId, version: u32, text: &str) -> Vec<OutlineItem> {
|
||||
/// The memoized outline, shared: a cache hit clones none of its `String`s.
|
||||
pub fn outline(&self, doc: DocId, lang: &LanguageId, version: u32, text: &str) -> Arc<Vec<OutlineItem>> {
|
||||
self.memoized(
|
||||
doc,
|
||||
lang,
|
||||
@@ -211,7 +212,8 @@ impl SyntaxCache {
|
||||
)
|
||||
}
|
||||
|
||||
pub fn folding(&self, doc: DocId, lang: &LanguageId, version: u32, text: &str) -> Vec<FoldRange> {
|
||||
/// The memoized folding ranges, shared like [`Self::outline`].
|
||||
pub fn folding(&self, doc: DocId, lang: &LanguageId, version: u32, text: &str) -> Arc<Vec<FoldRange>> {
|
||||
self.memoized(
|
||||
doc,
|
||||
lang,
|
||||
@@ -230,7 +232,7 @@ impl SyntaxCache {
|
||||
|
||||
/// Highlighting from the grammar's own query; empty without one.
|
||||
pub fn highlights(&self, doc: DocId, lang: &LanguageId, version: u32, text: &str) -> SyntaxHighlights {
|
||||
SyntaxHighlights::new(self.memoized(
|
||||
SyntaxHighlights::new(Arc::unwrap_or_clone(self.memoized(
|
||||
doc,
|
||||
lang,
|
||||
version,
|
||||
@@ -241,10 +243,12 @@ impl SyntaxCache {
|
||||
})
|
||||
.or_else(|| super::external::highlights(lang, text))
|
||||
},
|
||||
))
|
||||
)))
|
||||
}
|
||||
|
||||
/// `compute()` once per document version; `None` (no grammar / query) is not memoized.
|
||||
/// Returns the `Arc` the memo holds; a caller that must own the result uses
|
||||
/// `Arc::unwrap_or_clone`.
|
||||
fn memoized<T: Clone>(
|
||||
&self,
|
||||
doc: DocId,
|
||||
@@ -252,7 +256,7 @@ impl SyntaxCache {
|
||||
version: u32,
|
||||
slot: impl Fn(&mut Memo) -> &mut Option<Arc<Vec<T>>>,
|
||||
compute: impl FnOnce() -> Option<Vec<T>>,
|
||||
) -> Vec<T> {
|
||||
) -> Arc<Vec<T>> {
|
||||
let entry = self.entries.lock().get(&doc).cloned();
|
||||
if let Some(e) = &entry {
|
||||
let mut e = e.lock();
|
||||
@@ -260,10 +264,11 @@ impl SyntaxCache {
|
||||
&& let Some(hit) = slot(&mut e.memo).clone()
|
||||
{
|
||||
self.hits.fetch_add(1, Ordering::Relaxed);
|
||||
return (*hit).clone();
|
||||
return hit;
|
||||
}
|
||||
}
|
||||
let Some(result) = compute() else { return Vec::new() };
|
||||
let Some(result) = compute() else { return Arc::new(Vec::new()) };
|
||||
let result = Arc::new(result);
|
||||
if let Some(e) = &entry {
|
||||
let mut e = e.lock();
|
||||
// Containers without their own grammar memoize their web outline too.
|
||||
@@ -271,7 +276,7 @@ impl SyntaxCache {
|
||||
if e.version == version
|
||||
&& (e.tree.is_some() || (e.lang == *lang && self.syntax.web_grammar_name(lang) == Some("html")))
|
||||
{
|
||||
*slot(e.memo.at(version, lang)) = Some(Arc::new(result.clone()));
|
||||
*slot(e.memo.at(version, lang)) = Some(result.clone());
|
||||
}
|
||||
}
|
||||
result
|
||||
@@ -426,7 +431,7 @@ mod tests {
|
||||
let mut doc = Document::new(SRC);
|
||||
cache.open(id, &rust(), doc.version());
|
||||
let first = cache.outline(id, &rust(), 0, &doc.text());
|
||||
assert_eq!(first, cache.syntax().outline(&rust(), SRC));
|
||||
assert_eq!(first.as_ref(), &cache.syntax().outline(&rust(), SRC));
|
||||
assert_eq!(cache.stats().full_parses, 1);
|
||||
cache.folding(id, &rust(), 0, &doc.text());
|
||||
assert_eq!(cache.stats().hits, 1);
|
||||
@@ -444,8 +449,16 @@ mod tests {
|
||||
cache.edit(id, doc.version(), &edits);
|
||||
let text = doc.text();
|
||||
let v = doc.version();
|
||||
assert_eq!(cache.outline(id, &rust(), v, &text), cache.syntax().outline(&rust(), &text), "round {round}");
|
||||
assert_eq!(cache.folding(id, &rust(), v, &text), cache.syntax().folding(&rust(), &text), "round {round}");
|
||||
assert_eq!(
|
||||
cache.outline(id, &rust(), v, &text).as_ref(),
|
||||
&cache.syntax().outline(&rust(), &text),
|
||||
"round {round}"
|
||||
);
|
||||
assert_eq!(
|
||||
cache.folding(id, &rust(), v, &text).as_ref(),
|
||||
&cache.syntax().folding(&rust(), &text),
|
||||
"round {round}"
|
||||
);
|
||||
let off = seed % (doc.len_utf16() + 1);
|
||||
assert_eq!(
|
||||
cache.selection_ranges(id, &rust(), v, &text, off),
|
||||
@@ -481,7 +494,7 @@ mod tests {
|
||||
cache.edit(id, doc.version(), &edits);
|
||||
let text = doc.text();
|
||||
let edited = cache.outline(id, &rust(), 1, &text);
|
||||
assert_eq!(edited, cache.syntax().outline(&rust(), &text));
|
||||
assert_eq!(edited.as_ref(), &cache.syntax().outline(&rust(), &text));
|
||||
assert_ne!(edited, outline);
|
||||
// Another language at the same version is not answered from the memo.
|
||||
assert!(cache.outline(id, &LanguageId::new("plaintext"), 1, &text).is_empty());
|
||||
@@ -504,7 +517,7 @@ mod tests {
|
||||
cache.edit(id, doc.version(), &edits);
|
||||
let second = cache.outline(id, &lang, doc.version(), &doc.text());
|
||||
assert_ne!(second, first);
|
||||
assert_eq!(second, cache.syntax().outline(&lang, &doc.text()));
|
||||
assert_eq!(second.as_ref(), &cache.syntax().outline(&lang, &doc.text()));
|
||||
// An older query must not replace the current version's memo.
|
||||
cache.outline(id, &lang, 0, "<script>const old = 0;</script>");
|
||||
let hits = cache.stats().hits;
|
||||
@@ -528,7 +541,7 @@ mod tests {
|
||||
let edits = edit(&mut doc, 0, 0, "// y\n");
|
||||
cache.edit(id, doc.version(), &edits);
|
||||
let text = doc.text();
|
||||
assert_eq!(cache.outline(id, &rust(), 2, &text), cache.syntax().outline(&rust(), &text));
|
||||
assert_eq!(cache.outline(id, &rust(), 2, &text).as_ref(), &cache.syntax().outline(&rust(), &text));
|
||||
assert_eq!(cache.stats().full_parses, 2, "dropped tree re-parses from scratch");
|
||||
|
||||
// A query for a version the cache has not reached is answered but not stored.
|
||||
|
||||
@@ -2033,9 +2033,9 @@ fn cached_outline_matches_syntax_outline_for_web_documents() {
|
||||
let id = DocId::next();
|
||||
let src = "<template><div/></template>\n<script lang=\"ts\">\nconst app = 1;\n</script>\n";
|
||||
cache.open(id, &lang("vue"), 0);
|
||||
assert_eq!(cache.outline(id, &lang("vue"), 0, src), syntax().outline(&lang("vue"), src));
|
||||
assert_eq!(cache.outline(id, &lang("vue"), 0, src).as_ref(), &syntax().outline(&lang("vue"), src));
|
||||
cache.open(id, &lang("html"), 0);
|
||||
assert_eq!(cache.outline(id, &lang("html"), 0, src), syntax().outline(&lang("html"), src));
|
||||
assert_eq!(cache.outline(id, &lang("html"), 0, src).as_ref(), &syntax().outline(&lang("html"), src));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -2137,7 +2137,7 @@ fn outline_restructuredtext_unicode_crlf_and_cached_edits() {
|
||||
let cache = SyntaxCache::new(Arc::new(Syntax::new()));
|
||||
cache.open(id, &language, 0);
|
||||
let items = cache.outline(id, &language, 0, src);
|
||||
assert_eq!(items, syntax().outline(&language, src));
|
||||
assert_eq!(items.as_ref(), &syntax().outline(&language, src));
|
||||
assert_eq!(items[0].name, "中文😀");
|
||||
assert_eq!(items[0].selection_range, Range::new(Pos::new(1, 1), Pos::new(1, 5)));
|
||||
assert_eq!(items[0].children[0].name, "子章节");
|
||||
@@ -2146,7 +2146,7 @@ fn outline_restructuredtext_unicode_crlf_and_cached_edits() {
|
||||
assert_eq!(items[0].children[1].name, "Other");
|
||||
let edited = src.replace("子章节", "新章节😀");
|
||||
let updated = cache.outline(id, &language, 1, &edited);
|
||||
assert_eq!(updated, syntax().outline(&language, &edited));
|
||||
assert_eq!(updated.as_ref(), &syntax().outline(&language, &edited));
|
||||
assert_eq!(updated[0].children[0].selection_range.end, Pos::new(4, 5));
|
||||
assert_ne!(items, updated);
|
||||
}
|
||||
|
||||
Reference in new issue
Заблокувати користувача