From 32c57bcd22fa95257cc55b1e24f3f2576d7106dc Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Thu, 20 Apr 2023 00:11:45 -0700 Subject: [PATCH] Store buffer's diagnostic sets in a smallvec --- 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(-) diff --git a/Cargo.toml b/Cargo.toml index 8113c0cfcf..1275571668 100644 --- a/Cargo.toml +++ b/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" } diff --git a/crates/activity_indicator/Cargo.toml b/crates/activity_indicator/Cargo.toml index 78a4e752b2..b54271ee2d 100644 --- a/crates/activity_indicator/Cargo.toml +++ b/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 } diff --git a/crates/clock/Cargo.toml b/crates/clock/Cargo.toml index 1705fdc6d5..2ea2779659 100644 --- a/crates/clock/Cargo.toml +++ b/crates/clock/Cargo.toml @@ -9,4 +9,4 @@ path = "src/clock.rs" doctest = false [dependencies] -smallvec = { version = "1.6", features = ["union"] } +smallvec = { workspace = true } diff --git a/crates/context_menu/Cargo.toml b/crates/context_menu/Cargo.toml index d764d4ddb8..64449bdc92 100644 --- a/crates/context_menu/Cargo.toml +++ b/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 } diff --git a/crates/diagnostics/Cargo.toml b/crates/diagnostics/Cargo.toml index 83ee243b82..45621f10c6 100644 --- a/crates/diagnostics/Cargo.toml +++ b/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" } diff --git a/crates/editor/Cargo.toml b/crates/editor/Cargo.toml index 82b7082576..4cd81df1dc 100644 --- a/crates/editor/Cargo.toml +++ b/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 } diff --git a/crates/gpui/Cargo.toml b/crates/gpui/Cargo.toml index 8715142dd3..88c40665c2 100644 --- a/crates/gpui/Cargo.toml +++ b/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" diff --git a/crates/language/Cargo.toml b/crates/language/Cargo.toml index 4311f04391..4e15ccf679 100644 --- a/crates/language/Cargo.toml +++ b/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 } diff --git a/crates/language/src/buffer.rs b/crates/language/src/buffer.rs index 25536adcbb..65e4d3b8b6 100644 --- a/crates/language/src/buffer.rs +++ b/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, parsing_in_background: bool, parse_count: usize, - diagnostics: HashMap, + diagnostics: SmallVec<[(LanguageServerId, DiagnosticSet); 2]>, remote_selections: TreeMap, 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>, - diagnostics: HashMap, + 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, ) { 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::(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 { diff --git a/crates/rope/Cargo.toml b/crates/rope/Cargo.toml index 4566b8397d..bd72c7c263 100644 --- a/crates/rope/Cargo.toml +++ b/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"] } diff --git a/crates/search/Cargo.toml b/crates/search/Cargo.toml index f786d4abc6..fb4e79a703 100644 --- a/crates/search/Cargo.toml +++ b/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] diff --git a/crates/snippet/Cargo.toml b/crates/snippet/Cargo.toml index 429f5d416e..67f77e4483 100644 --- a/crates/snippet/Cargo.toml +++ b/crates/snippet/Cargo.toml @@ -10,4 +10,4 @@ doctest = false [dependencies] anyhow = "1.0" -smallvec = { version = "1.6", features = ["union"] } +smallvec = { workspace = true } diff --git a/crates/terminal/Cargo.toml b/crates/terminal/Cargo.toml index 56796fca59..107d325677 100644 --- a/crates/terminal/Cargo.toml +++ b/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" diff --git a/crates/terminal_view/Cargo.toml b/crates/terminal_view/Cargo.toml index 726a1a674f..f271cd118d 100644 --- a/crates/terminal_view/Cargo.toml +++ b/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" diff --git a/crates/text/Cargo.toml b/crates/text/Cargo.toml index 362a060c1f..d85464a446 100644 --- a/crates/text/Cargo.toml +++ b/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" diff --git a/crates/theme_testbench/Cargo.toml b/crates/theme_testbench/Cargo.toml index 763727fc68..ade8e39550 100644 --- a/crates/theme_testbench/Cargo.toml +++ b/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 } diff --git a/crates/workspace/Cargo.toml b/crates/workspace/Cargo.toml index 5a2380de3f..84db04e9a6 100644 --- a/crates/workspace/Cargo.toml +++ b/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"] } diff --git a/crates/zed/Cargo.toml b/crates/zed/Cargo.toml index 180a1fce24..3df019bb45 100644 --- a/crates/zed/Cargo.toml +++ b/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"