Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Merge remote-tracking branch 'origin/main' into wip/gpui-local-2026-10-15

Richie McIlroy5b2fd728aea5
diff --cc apps/desktop-gpui/src/store.rs
index 2567910cc,1bb8c4715..73e17c7df
--- a/apps/desktop-gpui/src/store.rs
+++ b/apps/desktop-gpui/src/store.rs
@@@ -450,33 -457,13 +457,31 @@@ pub fn set_store_value(section: &str, v
})
}
+/// Replace one top-level section only if `update` agrees, deciding from the
+/// section as it is on disk at write time rather than from a copy read before
+/// a network round trip.
+pub fn update_store_section(
+ section: &str,
+ update: impl FnOnce(&mut Map<String, Value>) -> bool,
+) -> bool {
- modify_store(|store| {
++ update_store(|store| {
+ let mut current = match store.remove(section) {
+ Some(Value::Object(map)) => map,
+ _ => Map::new(),
+ };
+ let keep = update(&mut current);
+ store.insert(section.to_string(), Value::Object(current));
+ keep
+ })
+}
+
- /// Every write is a read-modify-write of the whole file through one temp
- /// path, so two threads writing at once (an auth write from a tokio worker and
- /// a settings toggle on the main thread) could drop one update or fail the
- /// rename. One process-wide lock serializes them.
- fn modify_store(update: impl FnOnce(&mut Map<String, Value>) -> bool) -> bool {
- static STORE_WRITE: std::sync::Mutex<()> = std::sync::Mutex::new(());
- let _guard = STORE_WRITE
+ /// Reads, mutates and rewrites the store as one step, so two writers cannot
+ /// both start from the same snapshot and drop each other's changes. Returning
+ /// false from `mutate` leaves the file untouched.
+ fn update_store(mutate: impl FnOnce(&mut Map<String, Value>) -> bool) -> bool {
+ let _guard = STORE_UPDATE_LOCK
.lock()
- .unwrap_or_else(|error| error.into_inner());
+ .unwrap_or_else(std::sync::PoisonError::into_inner);
let path = tauri_store_path();
let Some(mut store) = read_store(&path) else {
return false;
@@@ -1938,29 -1922,80 +1940,103 @@@ mod tests
}
}
+ fn load_general_settings_separately() -> GeneralSettings {
+ GeneralSettings::from_sections(
+ &store_section(GENERAL_SETTINGS),
+ &store_section(RECORDING_START_SAFETY),
+ &store_section(AUDIO_ENHANCEMENT),
+ )
+ }
+
+ #[test]
+ fn general_settings_load_matches_separate_section_reads() {
+ for contents in [
+ None,
+ Some("invalid json"),
+ Some("[]"),
+ Some(r#"{"general_settings":["wrong shape"],"audio_enhancement":null}"#),
+ Some(
+ r#"{"general_settings":{"theme":"dark","maxFps":24},"recording_start_safety":{"confirmBeforeRecordingWithoutMicrophone":false},"audio_enhancement":{"enabledByDefault":false}}"#,
+ ),
+ ] {
+ let _store = TempStore::new("general-settings-load", contents);
+ assert_eq!(GeneralSettings::load(), load_general_settings_separately());
+ }
+ }
+
+ #[test]
+ #[ignore]
+ fn benchmark_general_settings_load() {
+ use std::{hint::black_box, time::Instant};
+
+ for preset_count in [0, 10, 50] {
+ let config =
+ crate::presets::preset_config(&cap_project::ProjectConfiguration::default());
+ let presets: Vec<_> = (0..preset_count)
+ .map(|index| serde_json::json!({"name": format!("Preset {index}"), "config": config}))
+ .collect();
+ let contents = serde_json::to_string_pretty(&serde_json::json!({
+ "general_settings": {"theme": "dark", "maxFps": 60},
+ "recording_start_safety": {"confirmBeforeRecordingWithoutMicrophone": false},
+ "audio_enhancement": {"enabledByDefault": true},
+ "presets": {"presets": presets, "default": null},
+ "teleprompter": {"script": "A recording script. ".repeat(100)}
+ }))
+ .unwrap();
+ let _store = TempStore::new("general-settings-benchmark", Some(&contents));
+ assert_eq!(GeneralSettings::load(), load_general_settings_separately());
+ let measure = |load: fn() -> GeneralSettings| {
+ let start = Instant::now();
+ for _ in 0..200 {
+ black_box(load());
+ }
+ start.elapsed().as_secs_f64() * 1_000_000.0 / 200.0
+ };
+ let mut separate = Vec::new();
+ let mut current = Vec::new();
+ for round in 0..9 {
+ if round % 2 == 0 {
+ separate.push(measure(load_general_settings_separately));
+ current.push(measure(GeneralSettings::load));
+ } else {
+ current.push(measure(GeneralSettings::load));
+ separate.push(measure(load_general_settings_separately));
+ }
+ }
+ separate.sort_by(f64::total_cmp);
+ current.sort_by(f64::total_cmp);
+ eprintln!(
+ "presets={preset_count} bytes={} separate_median_us={:.3} current_median_us={:.3} separate_runs_us={separate:?} current_runs_us={current:?}",
+ contents.len(),
+ separate[4],
+ current[4]
+ );
+ }
+ }
+
+ #[test]
+ fn section_updates_merge_into_disk_state_and_can_decline() {
+ let store = TempStore::new(
+ "section-update",
+ Some(r#"{"auth":{"user_id":"a","plan":null},"general_settings":{"x":1}}"#),
+ );
+
+ assert!(update_store_section("auth", |auth| {
+ auth.insert("organizations".into(), serde_json::json!([]));
+ true
+ }));
+ let written = store.read();
+ assert_eq!(written["auth"]["user_id"], "a");
+ assert_eq!(written["auth"]["organizations"], serde_json::json!([]));
+ assert_eq!(written["general_settings"]["x"], 1);
+
+ assert!(!update_store_section("auth", |auth| {
+ auth.insert("user_id".into(), "b".into());
+ false
+ }));
+ assert_eq!(store.read()["auth"]["user_id"], "a");
+ }
+
#[test]
fn notification_sounds_follow_tauri_settings_without_writing() {
for (contents, enabled) in [