Skip to content

Visual Studio Code, rebuilt in Rust

Rust

perf(config): cache config context keys until a layer changes

rokartur6105ed48904e
diff --git a/native/crates/app/src/workbench/keyboard.rs b/native/crates/app/src/workbench/keyboard.rs
index 2065234c9c9..8b554de7f2c 100644
--- a/native/crates/app/src/workbench/keyboard.rs
+++ b/native/crates/app/src/workbench/keyboard.rs
@@ -398,12 +398,7 @@ impl Workbench {
/// The context keys the keybindings' `when` clauses see: the keys extensions set, then the focused part's.
pub fn key_context(&self, window: &Window) -> HashMap<String, Value> {
- // contextKeyService.ts `ConfigAwareContextValuesContainer`: `config.<setting>` is the setting's value.
- let mut context = HashMap::new();
- let configuration = self.settings.lock().unwrap().get_value(None, &config::configuration::Overrides::default());
- if let Some(Value::Object(sections)) = configuration {
- insert_config_keys("config", sections, &mut context);
- }
+ let mut context = self.settings.lock().unwrap().configuration.context_keys();
context.extend(self.context_keys.clone());
// update.ts CONTEXT_UPDATE_STATE.
context.insert("updateState".into(), json!(self.update_state.type_name()));
@@ -719,16 +714,5 @@ pub(super) fn report_failure(outcome: Outcome, cx: &mut Context<Workbench>) {
.detach();
}
-/// The settings below `prefix` as `prefix.<section>.<key>` context keys, one per value that is not an object.
-fn insert_config_keys(prefix: &str, sections: serde_json::Map<String, Value>, context: &mut HashMap<String, Value>) {
- for (key, value) in sections {
- let key = format!("{prefix}.{key}");
- match value {
- Value::Object(children) => insert_config_keys(&key, children, context),
- value => drop(context.insert(key, value)),
- }
- }
-}
-
#[cfg(test)]
mod tests;
diff --git a/native/crates/config/src/configuration.rs b/native/crates/config/src/configuration.rs
index 6be904290b2..7c4c3f63ba0 100644
--- a/native/crates/config/src/configuration.rs
+++ b/native/crates/config/src/configuration.rs
@@ -26,6 +26,8 @@ pub struct Configuration {
/// `_workspaceConsolidatedConfiguration`, `_foldersConsolidatedConfigurations` and `overrideConfigurations`:
/// the merged model per folder and language. A write to a layer field must call `layers_changed`.
pub(crate) consolidated: RefCell<HashMap<FolderAndLanguage, ConfigurationModel>>,
+ /// `context_keys`, dropped with the merged models.
+ pub(crate) context_keys: RefCell<Option<HashMap<String, Value>>>,
}
type FolderAndLanguage = (Option<PathBuf>, Option<String>);
@@ -68,6 +70,22 @@ impl Configuration {
/// Drops the merged models after a layer changed.
pub fn layers_changed(&mut self) {
self.consolidated.get_mut().clear();
+ *self.context_keys.get_mut() = None;
+ }
+
+ /// contextKeyService.ts `ConfigAwareContextValuesContainer`: `config.<setting>` is the setting's value, one key per
+ /// value that is not an object. Kept until a layer changes: the workbench asks for them several times a frame.
+ pub fn context_keys(&self) -> HashMap<String, Value> {
+ let mut cached = self.context_keys.borrow_mut();
+ cached
+ .get_or_insert_with(|| {
+ let mut keys = HashMap::new();
+ if let Some(Value::Object(sections)) = self.get_value(None, &Overrides::default()) {
+ insert_context_keys("config", sections, &mut keys);
+ }
+ keys
+ })
+ .clone()
}
/// `inspect(key, overrides)`, the layers MainThreadConfiguration and `updateValue` decide a write target from.
@@ -189,6 +207,16 @@ impl Configuration {
}
}
+/// The settings below `prefix` as `prefix.<section>.<key>` context keys, one per value that is not an object.
+fn insert_context_keys(prefix: &str, sections: serde_json::Map<String, Value>, context: &mut HashMap<String, Value>) {
+ for (key, value) in sections {
+ let key = format!("{prefix}.{key}");
+ match value {
+ Value::Object(children) => insert_context_keys(&key, children, context),
+ value => drop(context.insert(key, value)),
+ }
+ }
+}
/// `UriComponents` of a `file:` URI, as the extension host revives it.
fn file_uri(path: &Path) -> Value {
json!({ "$mid": 1, "scheme": "file", "authority": "", "path": path.to_string_lossy(), "query": "", "fragment": "" })
diff --git a/native/crates/config/src/service.rs b/native/crates/config/src/service.rs
index 217b85c0a8b..113e29e0333 100644
--- a/native/crates/config/src/service.rs
+++ b/native/crates/config/src/service.rs
@@ -857,6 +857,15 @@ mod tests {
json::parse(&std::fs::read_to_string(path).unwrap(), &mut Vec::new(), ParseOptions::default()).unwrap()
}
+ #[test]
+ fn context_keys_follow_a_write() {
+ let mut fixture = Fixture::new("context-keys", false);
+ let key = "config.configurationEditing.service.testSetting";
+ assert_eq!(fixture.settings.configuration.context_keys()[key], json!("isSet"));
+ fixture.write("configurationEditing.service.testSetting", Some(json!("written")), Target::User).unwrap();
+ assert_eq!(fixture.settings.configuration.context_keys()[key], json!("written"));
+ }
+
#[test]
fn user_settings_are_written_atomically_and_folder_settings_in_place() {
use std::os::unix::fs::MetadataExt;