plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
9f12e1b
— Peter John
2026-11-10T12:30:56+05:30
trmc test: the 1M List.map bound is measured in fuel, not wall-clock time
compiler/core/tests/core/trmc_test.rs
CHANGED
|
@@ -464,8 +464,12 @@ fn hasOp(wasm: &[u8], name: &str, pred: fn(&wasmparser::Operator) -> bool) -> bo
|
|
|
464
464
|
/// list. The loop is not the cause: one `struct.new` per cell, closure inlined.
|
|
465
465
|
/// The time was wasmtime's copying GC copying every live cell on each heap
|
|
466
466
|
/// growth, landing on whichever phase grew the heap (`build` gets the same
|
|
467
|
+
/// spikes). What this guards is the loop itself, measured without a clock (a wall-clock
|
|
468
|
+
/// ratio failed about one run in three on a loaded machine):
|
|
469
|
+
/// - a 1M `map` runs in a small stack, so it is a loop and not a recursion;
|
|
470
|
+
/// - it executes at most 3x the wasm instructions of a 1M build (wasmtime's fuel, which is
|
|
471
|
+
/// the same number on every run).
|
|
467
|
-
///
|
|
472
|
+
/// The GC heap is pre-sized so no collection runs.
|
|
468
|
-
/// at most 3x a 1M build.
|
|
469
473
|
#[test]
|
|
470
474
|
fn std_map_1m_within_3x_of_build() {
|
|
471
475
|
let head = "import std/Int\nimport std/List\n\nfun build(n: Int, acc: List[Int]) -> List[Int] = if n == 0 then acc else build(n - 1, Cons(head: n, tail: acc))\n\n";
|
|
@@ -485,29 +489,34 @@ fn std_map_1m_within_3x_of_build() {
|
|
|
485
489
|
config.wasm_function_references(true);
|
|
486
490
|
config.wasm_tail_call(true);
|
|
487
491
|
config.gc_heap_initial_size(512 << 20);
|
|
492
|
+
config.consume_fuel(true);
|
|
493
|
+
// A frame per element would need tens of megabytes: 1M elements in 256 KiB is a loop.
|
|
494
|
+
config.max_wasm_stack(256 << 10);
|
|
488
495
|
let engine = wasmtime::Engine::new(&config).unwrap();
|
|
496
|
+
// The fuel (wasm instructions, roughly) one run of `main` burns, and how long it took.
|
|
489
|
-
let
|
|
497
|
+
let run = |name: &str, src: &str, want: i64| -> (u64, std::time::Duration) {
|
|
490
498
|
let bytes = compileUnfused(&load(name, src)).unwrap();
|
|
491
499
|
let module = wasmtime::Module::new(&engine, &bytes).unwrap();
|
|
492
|
-
(0..3)
|
|
493
|
-
.map(|_| {
|
|
494
|
-
|
|
500
|
+
let mut store = wasmtime::Store::new(&engine, ());
|
|
501
|
+
store.set_fuel(u64::MAX).unwrap();
|
|
495
|
-
|
|
502
|
+
let mut linker = wasmtime::Linker::new(&engine);
|
|
496
|
-
|
|
503
|
+
linker.define_unknown_imports_as_default_values(&mut store, &module).unwrap();
|
|
497
|
-
|
|
504
|
+
let instance = linker.instantiate(&mut store, &module).unwrap();
|
|
498
|
-
|
|
505
|
+
let main = instance.get_typed_func::<(), i64>(&mut store, "main").unwrap();
|
|
506
|
+
let before = store.get_fuel().unwrap();
|
|
499
|
-
|
|
507
|
+
let t = std::time::Instant::now();
|
|
500
|
-
|
|
508
|
+
assert_eq!(main.call(&mut store, ()).unwrap(), want, "{name}");
|
|
501
|
-
|
|
509
|
+
(before - store.get_fuel().unwrap(), t.elapsed())
|
|
502
|
-
})
|
|
503
|
-
.min()
|
|
504
|
-
.unwrap()
|
|
505
510
|
};
|
|
506
|
-
let tBuild =
|
|
511
|
+
let (fBuild, tBuild) = run("stdbuild", &buildSrc, 1_000_000);
|
|
507
|
-
let tBoth =
|
|
512
|
+
let (fBoth, tBoth) = run("stdmap2", &mapSrc, 2_000_000);
|
|
513
|
+
// `build+map` also runs a second `length`, so this counts against the map: it only
|
|
514
|
+
// makes the bound stricter.
|
|
508
|
-
let
|
|
515
|
+
let fMap = fBoth.saturating_sub(fBuild);
|
|
509
|
-
eprintln!("build {
|
|
516
|
+
eprintln!("fuel: build {fBuild} build+map {fBoth} map {fMap}; time (not asserted): build {tBuild:?} build+map {tBoth:?}");
|
|
517
|
+
assert!(fBuild >= 1_000_000, "the build ran: {fBuild}");
|
|
518
|
+
assert!(fMap >= 1_000_000, "the map ran: {fMap}");
|
|
510
|
-
assert!(
|
|
519
|
+
assert!(fMap <= fBuild * 3, "map burns {fMap} fuel, build {fBuild}");
|
|
511
520
|
}
|
|
512
521
|
|
|
513
522
|
#[test]
|