| 1 | use std::{ |
| 2 | alloc::{GlobalAlloc, Layout, System}, |
| 3 | hint::black_box, |
| 4 | sync::atomic::{AtomicUsize, Ordering}, |
| 5 | time::Instant, |
| 6 | }; |
| 7 | |
| 8 | use gpui::{SpringConfig, SpringState}; |
| 9 | use gpui_base::{Easing, Keyframe, Keyframes, Stagger, StaggerOrigin, Timing}; |
| 10 | |
| 11 | const BATCHES: usize = 31; |
| 12 | const WARMUP_BATCHES: usize = 5; |
| 13 | const ITERATIONS: usize = 200; |
| 14 | |
| 15 | struct CountingAllocator; |
| 16 | |
| 17 | static ALLOCATIONS: AtomicUsize = AtomicUsize::new(0); |
| 18 | |
| 19 | // SAFETY: every operation is forwarded unchanged to the process System |
| 20 | // allocator; the counter has no bearing on allocation validity or lifetime. |
| 21 | unsafe impl GlobalAlloc for CountingAllocator { |
| 22 | unsafe fn alloc(&self, layout: Layout) -> *mut u8 { |
| 23 | ALLOCATIONS.fetch_add(1, Ordering::Relaxed); |
| 24 | // SAFETY: `layout` is passed through from the GlobalAlloc caller. |
| 25 | unsafe { System.alloc(layout) } |
| 26 | } |
| 27 | |
| 28 | unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { |
| 29 | // SAFETY: `ptr` and `layout` are the same pair supplied by the caller. |
| 30 | unsafe { System.dealloc(ptr, layout) } |
| 31 | } |
| 32 | |
| 33 | unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 { |
| 34 | ALLOCATIONS.fetch_add(1, Ordering::Relaxed); |
| 35 | // SAFETY: all arguments are passed through from the GlobalAlloc caller. |
| 36 | unsafe { System.realloc(ptr, layout, new_size) } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | #[global_allocator] |
| 41 | static GLOBAL_ALLOCATOR: CountingAllocator = CountingAllocator; |
| 42 | |
| 43 | fn main() { |
| 44 | println!( |
| 45 | "motion benchmark: {} {} release={} batches={} iterations={}", |
| 46 | std::env::consts::OS, |
| 47 | std::env::consts::ARCH, |
| 48 | !cfg!(debug_assertions), |
| 49 | BATCHES, |
| 50 | ITERATIONS |
| 51 | ); |
| 52 | |
| 53 | let easing = Easing::Ease; |
| 54 | let timing = Timing::new(std::time::Duration::from_millis(240)).ease(easing.clone()); |
| 55 | measure("1,000 scalar timing + easing samples", || { |
| 56 | let mut sum = 0.0; |
| 57 | for index in 0..1_000 { |
| 58 | let elapsed = std::time::Duration::from_nanos((index * 211_003 % 240_000_000) as u64); |
| 59 | sum += timing.sample(black_box(elapsed)).directed_progress; |
| 60 | } |
| 61 | black_box(sum); |
| 62 | }); |
| 63 | |
| 64 | for frame_count in [2, 8, 32] { |
| 65 | let track = Keyframes::try_new((0..frame_count).map(|index| { |
| 66 | let offset = index as f32 / (frame_count - 1) as f32; |
| 67 | Keyframe::new(offset, offset * 100.0).ease(easing.clone()) |
| 68 | })) |
| 69 | .unwrap(); |
| 70 | measure( |
| 71 | &format!("1,000 keyframe samples ({frame_count} frames)"), |
| 72 | || { |
| 73 | let mut sum = 0.0; |
| 74 | for index in 0..1_000 { |
| 75 | sum += track.sample(black_box((index % 997) as f32 / 996.0)); |
| 76 | } |
| 77 | black_box(sum); |
| 78 | }, |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | let spring = SpringConfig::new(438.65, 41.89, 1.0); |
| 83 | measure("1,000 analytic spring integration samples", || { |
| 84 | let mut state = SpringState { |
| 85 | position: 0.0, |
| 86 | velocity: 0.0, |
| 87 | }; |
| 88 | for index in 0..1_000 { |
| 89 | state = spring.step(state, black_box(1.0), (index % 3 + 1) as f32 / 240.0); |
| 90 | } |
| 91 | black_box(state); |
| 92 | }); |
| 93 | |
| 94 | let stagger = Stagger::new(std::time::Duration::from_millis(24), StaggerOrigin::Center); |
| 95 | measure("1,000 stagger delay calculations", || { |
| 96 | let mut sum = 0; |
| 97 | for index in 0..1_000 { |
| 98 | sum += stagger.delay(black_box(index % 32), 32).as_nanos(); |
| 99 | } |
| 100 | black_box(sum); |
| 101 | }); |
| 102 | } |
| 103 | |
| 104 | fn measure(name: &str, mut operation: impl FnMut()) { |
| 105 | for _ in 0..WARMUP_BATCHES { |
| 106 | for _ in 0..ITERATIONS { |
| 107 | operation(); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | let allocations_before = ALLOCATIONS.load(Ordering::Relaxed); |
| 112 | for _ in 0..ITERATIONS { |
| 113 | operation(); |
| 114 | } |
| 115 | let steady_allocations = ALLOCATIONS.load(Ordering::Relaxed) - allocations_before; |
| 116 | assert_eq!( |
| 117 | steady_allocations, 0, |
| 118 | "{name} allocated {steady_allocations} times in the steady sampling loop" |
| 119 | ); |
| 120 | |
| 121 | let mut samples = Vec::with_capacity(BATCHES); |
| 122 | for _ in 0..BATCHES { |
| 123 | let started = Instant::now(); |
| 124 | for _ in 0..ITERATIONS { |
| 125 | operation(); |
| 126 | } |
| 127 | samples.push(started.elapsed().as_secs_f64() * 1_000_000.0 / ITERATIONS as f64); |
| 128 | } |
| 129 | samples.sort_by(f64::total_cmp); |
| 130 | let median = samples[BATCHES / 2]; |
| 131 | let p95 = samples[((BATCHES as f64 * 0.95).ceil() as usize - 1).min(BATCHES - 1)]; |
| 132 | let worst = samples[BATCHES - 1]; |
| 133 | println!("{name}: median={median:.3}us p95={p95:.3}us worst={worst:.3}us allocations=0"); |
| 134 | |
| 135 | if name.starts_with("1,000 scalar") && median > 100.0 { |
| 136 | panic!("Motion Core scalar sampling median {median:.3}us exceeds the 100us budget"); |
| 137 | } |
| 138 | } |