makepad/libs/relax/compiler/tests/bench1.rs
Admin b22a714840 makepad run: the Rust JIT (libs/relax) and makepad-remote
The JIT that runs makepad apps from source: lexer, front end, type checker,
lazy borrow checker (Gandalf; `--check-first` opts in), codegen, session and
warm/cold caches keyed by compiler stamp, provided crates bound late,
startup images, live patching. User-facing name is `makepad run` (binary
`makepad`, MAKEPAD_RUN_* env, ~/.makepad-run); libs/relax stays the internal
library name. `makepad check` reports rustc-style diagnostics with rustc
error codes and checks a whole workspace (`--workspace`). Runs on macOS,
Linux, Windows and Android. makepad-remote: one tool schema for apps and the
WM (app/1): grabs, captures, traces, frame profiles and tweaks.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-13 12:26:21 +02:00

109 lines
2.7 KiB
Rust

// Fixed O1 benchmark kernels (beside libs/math deterministic.rs tests).
// Each test does a fixed amount of work and checks a checksum also produced by rustc.
#[derive(Clone, Copy)]
struct V2 {
x: f32,
y: f32,
}
impl V2 {
fn new(x: f32, y: f32) -> V2 {
V2 { x, y }
}
fn add(self, o: V2) -> V2 {
V2 { x: self.x + o.x, y: self.y + o.y }
}
fn scale(self, s: f32) -> V2 {
V2 { x: self.x * s, y: self.y * s }
}
fn dot(self, o: V2) -> f32 {
self.x * o.x + self.y * o.y
}
}
/// live_id-style string hash over a byte slice
fn hash_bytes(seed: u64, bytes: &[u8]) -> u64 {
let mut x = seed;
let mut i = 0;
while i < bytes.len() {
x = x.wrapping_add(bytes[i] as u64);
x ^= x >> 32;
x = x.wrapping_mul(0xd6e8_feb8_6659_fd93);
x ^= x >> 32;
i += 1;
}
x & 0x0000_3fff_ffff_ffff
}
/// particle step: small structs by value
fn vec_step(p: &mut [V2; 64], v: &mut [V2; 64], dt: f32) -> f32 {
let g = V2::new(0.0, -9.81);
let mut e = 0.0f32;
for i in 0..64 {
v[i] = v[i].add(g.scale(dt));
p[i] = p[i].add(v[i].scale(dt));
if p[i].y < 0.0 {
p[i].y = -p[i].y;
v[i].y = -v[i].y * 0.9;
}
e += v[i].dot(v[i]);
}
e
}
/// insertion sort of a fixed array (bounds-checked indexing in nested loops)
fn sort_ints(a: &mut [i32; 256]) {
let mut i = 1;
while i < 256 {
let k = a[i];
let mut j = i;
while j > 0 && a[j - 1] > k {
a[j] = a[j - 1];
j -= 1;
}
a[j] = k;
i += 1;
}
}
#[test]
fn bench_hash() {
let text = b"the quick brown fox jumps over the lazy dog; widgets, draw, platform, script";
let mut h = 0u64;
for r in 0..200000u64 {
h ^= hash_bytes(r, text);
}
assert_eq!(h, 45583272550279);
}
#[test]
fn bench_vec2() {
let mut p = [V2::new(0.0, 0.0); 64];
let mut v = [V2::new(0.0, 0.0); 64];
for i in 0..64 {
p[i] = V2::new(i as f32, 10.0 + i as f32 * 0.5);
v[i] = V2::new(1.0, 0.0);
}
let mut e = 0.0f32;
for _ in 0..40000 {
e = vec_step(&mut p, &mut v, 0.001);
}
assert!(e.to_bits() == 1136748848, "e = {} bits {}", e, e.to_bits());
}
#[test]
fn bench_sort() {
let mut sum = 0i64;
for r in 0..2000 {
let mut a = [0i32; 256];
let mut s = r as u32 * 2654435761u32;
for i in 0..256 {
s = s.wrapping_mul(1664525).wrapping_add(1013904223);
a[i] = (s >> 8) as i32 % 10000;
}
sort_ints(&mut a);
sum += a[0] as i64 + a[128] as i64 + a[255] as i64;
}
assert_eq!(sum, 30041520);
}