Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

perf(project): reuse cursor ID strings during stabilization

vedanth1a7bb19919d3
diff --git a/crates/project/examples/cursor-stabilization-benchmark.rs b/crates/project/examples/cursor-stabilization-benchmark.rs
new file mode 100644
index 000000000..da76f2e56
--- /dev/null
+++ b/crates/project/examples/cursor-stabilization-benchmark.rs
@@ -0,0 +1,86 @@
+use cap_project::{CursorClickEvent, CursorEvents, CursorMoveEvent};
+use std::{
+ collections::{HashSet, hash_map::DefaultHasher},
+ error::Error,
+ hash::{Hash, Hasher},
+ hint::black_box,
+ time::Instant,
+};
+
+fn fixture(count: usize, pointer: &str, other: &str, brief: bool) -> CursorEvents {
+ let id = |index: usize| {
+ if index % 240 < if brief { 225 } else { 120 } {
+ pointer
+ } else {
+ other
+ }
+ };
+ CursorEvents {
+ moves: (0..count)
+ .map(|index| CursorMoveEvent {
+ active_modifiers: Vec::new(),
+ cursor_id: id(index).into(),
+ time_ms: index as f64 * 1000.0 / 60.0,
+ x: (index % 1920) as f64 / 1920.0,
+ y: (index % 1080) as f64 / 1080.0,
+ })
+ .collect(),
+ clicks: (0..count)
+ .step_by(30)
+ .map(|index| CursorClickEvent {
+ active_modifiers: Vec::new(),
+ cursor_id: id(index).into(),
+ time_ms: index as f64 * 1000.0 / 60.0,
+ cursor_num: 0,
+ down: (index / 30) % 2 == 0,
+ })
+ .collect(),
+ }
+}
+
+fn fingerprint(events: &CursorEvents) -> Result<u64, Box<dyn Error>> {
+ let mut hash = DefaultHasher::new();
+ bincode::serde::encode_to_vec(events, bincode::config::standard())?.hash(&mut hash);
+ Ok(hash.finish())
+}
+
+fn main() -> Result<(), Box<dyn Error>> {
+ println!("case,moves,clicks,sample,elapsed_ms,fingerprint");
+ for (case, (count, pointer, other, brief, metadata)) in [
+ (3_600, "0", "0", true, true),
+ (36_000, "0", "1", false, true),
+ (36_000, "0", "1", true, true),
+ (216_000, "0", "1", true, true),
+ (36_000, "0", "1", true, false),
+ (36_000, "123", "0", true, true),
+ (36_000, "pointer", "ibeam", true, true),
+ ]
+ .into_iter()
+ .enumerate()
+ {
+ let source = fixture(count, pointer, other, brief);
+ let pointers = HashSet::from([pointer.to_string()]);
+ let pointers = metadata.then_some(&pointers);
+ let mut expected = source.clone();
+ expected.stabilize_short_lived_cursor_shapes(pointers, 1_000.0);
+ let expected = fingerprint(&expected)?;
+ for _ in 0..3 {
+ let mut input = source.clone();
+ input.stabilize_short_lived_cursor_shapes(pointers, 1_000.0);
+ black_box(input);
+ }
+ for sample in 0..25 {
+ let mut input = source.clone();
+ let started = Instant::now();
+ black_box(&mut input).stabilize_short_lived_cursor_shapes(pointers, 1_000.0);
+ let elapsed_ms = started.elapsed().as_secs_f64() * 1000.0;
+ assert_eq!(fingerprint(&input)?, expected);
+ println!(
+ "{case},{},{},{sample},{elapsed_ms:.6},{expected}",
+ input.moves.len(),
+ input.clicks.len()
+ );
+ }
+ }
+ Ok(())
+}
diff --git a/crates/project/src/cursor.rs b/crates/project/src/cursor.rs
index 3c0787e9c..5a19e368a 100644
--- a/crates/project/src/cursor.rs
+++ b/crates/project/src/cursor.rs
@@ -198,7 +198,7 @@ impl CursorEvents {
}
for event in &mut self.moves[segments[i].range.clone()] {
- event.cursor_id = replacement.clone();
+ event.cursor_id.clone_from(&replacement);
}
segments[i].id = replacement;
}
@@ -215,7 +215,7 @@ impl CursorEvents {
segment_index += 1;
}
- click.cursor_id = segments[segment_index].id.clone();
+ click.cursor_id.clone_from(&segments[segment_index].id);
}
}
@@ -479,6 +479,44 @@ mod tests {
}
}
+ #[test]
+ fn stabilization_preserves_event_fields_when_replacing_different_length_ids() {
+ for (pointer, other) in [
+ (String::from("0"), "1".into()),
+ ("123".into(), "4".into()),
+ ("a".into(), "🖱".into()),
+ ("".into(), "old".into()),
+ ("pointer".into(), "x".repeat(65_536)),
+ ] {
+ let pointers = HashSet::from([pointer.clone()]);
+ let mut events = CursorEvents {
+ moves: vec![
+ move_event(0.0, &pointer),
+ move_event(200.0, &other),
+ move_event(300.0, &other),
+ move_event(400.0, &pointer),
+ ],
+ clicks: vec![click_event(250.0, &other), click_event(1500.0, &pointer)],
+ };
+ events.moves[1].x = f64::from_bits(0x7ff8_0000_0000_1234);
+ events.moves[2].y = -0.0;
+ events.moves[1].active_modifiers = vec!["LShift".into()];
+ events.clicks[0].active_modifiers = vec!["LMeta".into()];
+ let mut expected = events.clone();
+ for event in &mut expected.moves {
+ event.cursor_id = pointer.clone();
+ }
+ for event in &mut expected.clicks {
+ event.cursor_id = pointer.clone();
+ }
+ events.stabilize_short_lived_cursor_shapes(Some(&pointers), 1_000.0);
+ assert_eq!(
+ bincode::serde::encode_to_vec(&events, bincode::config::standard()).unwrap(),
+ bincode::serde::encode_to_vec(&expected, bincode::config::standard()).unwrap()
+ );
+ }
+ }
+
#[test]
fn short_lived_segments_are_replaced_with_pointer() {
let mut pointer_ids = HashSet::new();