diff --git a/crates/ide-host/src/ops/doc/commands.rs b/crates/ide-host/src/ops/doc/commands.rs index fbbf81fc8b..75c5e26661 100644 --- a/crates/ide-host/src/ops/doc/commands.rs +++ b/crates/ide-host/src/ops/doc/commands.rs @@ -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::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 { + /// 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> { self.memoized( doc, lang, @@ -211,7 +212,8 @@ impl SyntaxCache { ) } - pub fn folding(&self, doc: DocId, lang: &LanguageId, version: u32, text: &str) -> Vec { + /// The memoized folding ranges, shared like [`Self::outline`]. + pub fn folding(&self, doc: DocId, lang: &LanguageId, version: u32, text: &str) -> Arc> { 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( &self, doc: DocId, @@ -252,7 +256,7 @@ impl SyntaxCache { version: u32, slot: impl Fn(&mut Memo) -> &mut Option>>, compute: impl FnOnce() -> Option>, - ) -> Vec { + ) -> Arc> { 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, ""); 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. diff --git a/crates/ide-language/src/syntax/tests.rs b/crates/ide-language/src/syntax/tests.rs index fc17658528..ca1daf2285 100644 --- a/crates/ide-language/src/syntax/tests.rs +++ b/crates/ide-language/src/syntax/tests.rs @@ -2033,9 +2033,9 @@ fn cached_outline_matches_syntax_outline_for_web_documents() { let id = DocId::next(); let src = "\n\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); }