Store buffer's diagnostic sets in a smallvec

Max Brunsfeld created

Change summary

Cargo.toml                           |  1 
crates/activity_indicator/Cargo.toml |  2 
crates/clock/Cargo.toml              |  2 
crates/context_menu/Cargo.toml       |  2 
crates/diagnostics/Cargo.toml        |  2 
crates/editor/Cargo.toml             |  2 
crates/gpui/Cargo.toml               |  2 
crates/language/Cargo.toml           |  2 
crates/language/src/buffer.rs        | 32 ++++++++++++++++++-----------
crates/rope/Cargo.toml               |  2 
crates/search/Cargo.toml             |  2 
crates/snippet/Cargo.toml            |  2 
crates/terminal/Cargo.toml           |  2 
crates/terminal_view/Cargo.toml      |  2 
crates/text/Cargo.toml               |  2 
crates/theme_testbench/Cargo.toml    |  2 
crates/workspace/Cargo.toml          |  2 
crates/zed/Cargo.toml                |  2 
18 files changed, 37 insertions(+), 28 deletions(-)

Detailed changes

Cargo.toml 🔗

@@ -76,6 +76,7 @@ serde_derive = { version = "1.0", features = ["deserialize_in_place"] }
 serde_json = { version = "1.0", features = ["preserve_order", "raw_value"] }
 rand = { version = "0.8" }
 postage = { version = "0.5", features = ["futures-traits"] }
+smallvec = { version = "1.6", features = ["union"] }
 
 [patch.crates-io]
 tree-sitter = { git = "https://github.com/tree-sitter/tree-sitter", rev = "c51896d32dcc11a38e41f36e3deb1a6a9c4f4b14" }

crates/activity_indicator/Cargo.toml 🔗

@@ -18,4 +18,4 @@ settings = { path = "../settings" }
 util = { path = "../util" }
 workspace = { path = "../workspace" }
 futures = "0.3"
-smallvec = { version = "1.6", features = ["union"] }
+smallvec = { workspace = true }

crates/clock/Cargo.toml 🔗

@@ -9,4 +9,4 @@ path = "src/clock.rs"
 doctest = false
 
 [dependencies]
-smallvec = { version = "1.6", features = ["union"] }
+smallvec = { workspace = true }

crates/context_menu/Cargo.toml 🔗

@@ -13,4 +13,4 @@ gpui = { path = "../gpui" }
 menu = { path = "../menu" }
 settings = { path = "../settings" }
 theme = { path = "../theme" }
-smallvec = "1.6"
+smallvec = { workspace = true }

crates/diagnostics/Cargo.toml 🔗

@@ -10,7 +10,7 @@ doctest = false
 
 [dependencies]
 anyhow = "1.0"
-smallvec = { version = "1.6", features = ["union"] }
+smallvec = { workspace = true }
 collections = { path = "../collections" }
 editor = { path = "../editor" }
 language = { path = "../language" }

crates/editor/Cargo.toml 🔗

@@ -58,7 +58,7 @@ postage = { workspace = true }
 rand = { version = "0.8.3", optional = true }
 serde = { workspace = true }
 serde_derive = { workspace = true }
-smallvec = { version = "1.6", features = ["union"] }
+smallvec = { workspace = true }
 smol = "1.2"
 tree-sitter-rust = { version = "*", optional = true }
 tree-sitter-html = { version = "*", optional = true }

crates/gpui/Cargo.toml 🔗

@@ -44,7 +44,7 @@ seahash = "4.1"
 serde = { workspace = true }
 serde_derive = { workspace = true }
 serde_json = { workspace = true }
-smallvec = { version = "1.6", features = ["union"] }
+smallvec = { workspace = true }
 smol = "1.2"
 time = { version = "0.3", features = ["serde", "serde-well-known"] }
 tiny-skia = "0.5"

crates/language/Cargo.toml 🔗

@@ -50,7 +50,7 @@ serde = { workspace = true }
 serde_derive = { workspace = true }
 serde_json = { workspace = true }
 similar = "1.3"
-smallvec = { version = "1.6", features = ["union"] }
+smallvec = { workspace = true }
 smol = "1.2"
 tree-sitter = "0.20"
 tree-sitter-rust = { version = "*", optional = true }

crates/language/src/buffer.rs 🔗

@@ -13,7 +13,6 @@ use crate::{
 };
 use anyhow::{anyhow, Result};
 use clock::ReplicaId;
-use collections::HashMap;
 use fs::LineEnding;
 use futures::FutureExt as _;
 use gpui::{fonts::HighlightStyle, AppContext, Entity, ModelContext, Task};
@@ -21,6 +20,7 @@ use lsp::LanguageServerId;
 use parking_lot::Mutex;
 use settings::Settings;
 use similar::{ChangeTag, TextDiff};
+use smallvec::SmallVec;
 use smol::future::yield_now;
 use std::{
     any::Any,
@@ -73,7 +73,7 @@ pub struct Buffer {
     syntax_map: Mutex<SyntaxMap>,
     parsing_in_background: bool,
     parse_count: usize,
-    diagnostics: HashMap<LanguageServerId, DiagnosticSet>,
+    diagnostics: SmallVec<[(LanguageServerId, DiagnosticSet); 2]>,
     remote_selections: TreeMap<ReplicaId, SelectionSet>,
     selections_update_count: usize,
     diagnostics_update_count: usize,
@@ -90,7 +90,7 @@ pub struct BufferSnapshot {
     pub git_diff: git::diff::BufferDiff,
     pub(crate) syntax: SyntaxSnapshot,
     file: Option<Arc<dyn File>>,
-    diagnostics: HashMap<LanguageServerId, DiagnosticSet>,
+    diagnostics: SmallVec<[(LanguageServerId, DiagnosticSet); 2]>,
     diagnostics_update_count: usize,
     file_update_count: usize,
     git_diff_update_count: usize,
@@ -1652,7 +1652,10 @@ impl Buffer {
         cx: &mut ModelContext<Self>,
     ) {
         if lamport_timestamp > self.diagnostics_timestamp {
-            self.diagnostics.insert(server_id, diagnostics);
+            match self.diagnostics.binary_search_by_key(&server_id, |e| e.0) {
+                Err(ix) => self.diagnostics.insert(ix, (server_id, diagnostics)),
+                Ok(ix) => self.diagnostics[ix].1 = diagnostics,
+            };
             self.diagnostics_timestamp = lamport_timestamp;
             self.diagnostics_update_count += 1;
             self.text.lamport_clock.observe(lamport_timestamp);
@@ -2530,8 +2533,8 @@ impl BufferSnapshot {
     {
         let mut iterators: Vec<_> = self
             .diagnostics
-            .values()
-            .map(|collection| {
+            .iter()
+            .map(|(_, collection)| {
                 collection
                     .range::<T, O>(search_range.clone(), self, true, reversed)
                     .peekable()
@@ -2555,12 +2558,17 @@ impl BufferSnapshot {
         let mut groups = Vec::new();
 
         if let Some(language_server_id) = language_server_id {
-            if let Some(diagnostics) = self.diagnostics.get(&language_server_id) {
-                diagnostics.groups(language_server_id, &mut groups, self);
+            if let Ok(ix) = self
+                .diagnostics
+                .binary_search_by_key(&language_server_id, |e| e.0)
+            {
+                self.diagnostics[ix]
+                    .1
+                    .groups(language_server_id, &mut groups, self);
             }
         } else {
-            for (&language_server_id, diagnostics) in self.diagnostics.iter() {
-                diagnostics.groups(language_server_id, &mut groups, self);
+            for (language_server_id, diagnostics) in self.diagnostics.iter() {
+                diagnostics.groups(*language_server_id, &mut groups, self);
             }
         }
 
@@ -2581,8 +2589,8 @@ impl BufferSnapshot {
         O: 'a + FromAnchor,
     {
         self.diagnostics
-            .values()
-            .flat_map(move |set| set.group(group_id, self))
+            .iter()
+            .flat_map(move |(_, set)| set.group(group_id, self))
     }
 
     pub fn diagnostics_update_count(&self) -> usize {

crates/rope/Cargo.toml 🔗

@@ -9,7 +9,7 @@ path = "src/rope.rs"
 
 [dependencies]
 bromberg_sl2 = { git = "https://github.com/zed-industries/bromberg_sl2", rev = "950bc5482c216c395049ae33ae4501e08975f17f" }
-smallvec = { version = "1.6", features = ["union"] }
+smallvec = { workspace = true }
 sum_tree = { path = "../sum_tree" }
 arrayvec = "0.7.1"
 log = { version = "0.4.16", features = ["kv_unstable_serde"] }

crates/search/Cargo.toml 🔗

@@ -25,7 +25,7 @@ log = { version = "0.4.16", features = ["kv_unstable_serde"] }
 postage = { workspace = true }
 serde = { workspace = true }
 serde_derive = { workspace = true }
-smallvec = { version = "1.6", features = ["union"] }
+smallvec = { workspace = true }
 smol = "1.2"
 
 [dev-dependencies]

crates/snippet/Cargo.toml 🔗

@@ -10,4 +10,4 @@ doctest = false
 
 [dependencies]
 anyhow = "1.0"
-smallvec = { version = "1.6", features = ["union"] }
+smallvec = { workspace = true }

crates/terminal/Cargo.toml 🔗

@@ -17,7 +17,7 @@ theme = { path = "../theme" }
 util = { path = "../util" }
 alacritty_terminal = { git = "https://github.com/zed-industries/alacritty", rev = "a51dbe25d67e84d6ed4261e640d3954fbdd9be45" }
 procinfo = { git = "https://github.com/zed-industries/wezterm", rev = "5cd757e5f2eb039ed0c6bb6512223e69d5efc64d", default-features = false }
-smallvec = { version = "1.6", features = ["union"] }
+smallvec = { workspace = true }
 smol = "1.2.5"
 mio-extras = "2.0.6"
 futures = "0.3"

crates/terminal_view/Cargo.toml 🔗

@@ -21,7 +21,7 @@ workspace = { path = "../workspace" }
 db = { path = "../db" }
 procinfo = { git = "https://github.com/zed-industries/wezterm", rev = "5cd757e5f2eb039ed0c6bb6512223e69d5efc64d", default-features = false }
 terminal = { path = "../terminal" }
-smallvec = { version = "1.6", features = ["union"] }
+smallvec = { workspace = true }
 smol = "1.2.5"
 mio-extras = "2.0.6"
 futures = "0.3"

crates/text/Cargo.toml 🔗

@@ -24,7 +24,7 @@ log = { version = "0.4.16", features = ["kv_unstable_serde"] }
 parking_lot = "0.11"
 postage = { workspace = true }
 rand = { version = "0.8.3", optional = true }
-smallvec = { version = "1.6", features = ["union"] }
+smallvec = { workspace = true }
 util = { path = "../util" }
 regex = "1.5"
 

crates/theme_testbench/Cargo.toml 🔗

@@ -16,4 +16,4 @@ settings = { path = "../settings" }
 workspace = { path = "../workspace" }
 project = { path = "../project" }
 
-smallvec = { version = "1.6", features = ["union"] }
+smallvec = { workspace = true }

crates/workspace/Cargo.toml 🔗

@@ -47,7 +47,7 @@ postage = { workspace = true }
 serde = { workspace = true }
 serde_derive = { workspace = true }
 serde_json = { workspace = true }
-smallvec = { version = "1.6", features = ["union"] }
+smallvec = { workspace = true }
 indoc = "1.0.4"
 uuid = { version = "1.1.2", features = ["v4"] }
 

crates/zed/Cargo.toml 🔗

@@ -96,7 +96,7 @@ serde_derive = { workspace = true }
 serde_json = { workspace = true }
 serde_path_to_error = "0.1.4"
 simplelog = "0.9"
-smallvec = { version = "1.6", features = ["union"] }
+smallvec = { workspace = true }
 smol = "1.2.5"
 tempdir = { version = "0.3.7" }
 thiserror = "1.0.29"