plum

#treesitter#compiler#wasm

git clone https://git.pyrossh.dev/plum

A statically typed, imperative programming language inspired by rust, python


54cfd9b — Peter John 2026-11-10T11:01:46+05:30
Faster test-all: compiler crates at opt-level 1, slowest test files first, sweep modes side by side
Cargo.toml CHANGED
@@ -13,3 +13,19 @@ opt-level = 3
13
13
 
14
14
  [profile.dev.package.wasmtime-internal-cranelift]
15
15
  opt-level = 3
16
+
17
+ # The compiler crates run in every test: each `plum test` file and most Rust tests compile
18
+ # std. At opt-level 1 a sweep takes about 25% less CPU than unoptimised (3 a further 15%,
19
+ # but a cold build of the four takes nearly twice as long); a rebuild after an edit stays
20
+ # incremental and about as quick.
21
+ [profile.dev.package.plum-core]
22
+ opt-level = 1
23
+
24
+ [profile.dev.package.plum-checker]
25
+ opt-level = 1
26
+
27
+ [profile.dev.package.plum-driver]
28
+ opt-level = 1
29
+
30
+ [profile.dev.package.plum-wasm-codegen]
31
+ opt-level = 1
compiler/cli/src/main.rs CHANGED
@@ -563,15 +563,20 @@ fn runSweepParallel(
563
563
  let exe = std::env::current_exe().context("cannot find the plum executable")?;
564
564
  let color = useColor();
565
565
  let next = AtomicUsize::new(0);
566
+ // The slowest files start first, so one slow file does not run alone at the end.
567
+ let mut durations = loadTestDurations();
568
+ let order = sweepOrder(files, &durations);
569
+ let took: std::sync::Mutex<Vec<(usize, std::time::Duration)>> = std::sync::Mutex::new(Vec::new());
566
570
  let (tx, rx) = std::sync::mpsc::channel::<(usize, std::io::Result<(process::Output, Option<std::time::Duration>)>)>();
567
571
  let (mut passed, mut failed, mut filesFailed) = (0usize, 0usize, 0usize);
568
572
  std::thread::scope(|scope| {
569
573
  for _ in 0..jobs.min(files.len()) {
570
574
  let tx = tx.clone();
571
- let (next, exe) = (&next, &exe);
575
+ let (next, exe, order, took) = (&next, &exe, &order, &took);
572
576
  scope.spawn(move || loop {
573
- let i = next.fetch_add(1, Ordering::Relaxed);
577
+ let Some(&i) = order.get(next.fetch_add(1, Ordering::Relaxed)) else { break };
578
+ let file = &files[i];
574
- let Some(file) = files.get(i) else { break };
579
+ let started = std::time::Instant::now();
575
580
  let mut cmd = process::Command::new(exe);
576
581
  cmd.arg("test").arg("--lib-path").arg(lib_path);
577
582
  if !fusion.fusion {
@@ -591,7 +596,9 @@ fn runSweepParallel(
591
596
  // Each test has its own deadline inside the child; this one catches a file
592
597
  // that hangs outside its tests (in the compiler, say).
593
598
  let fileLimit = timeout.map(|t| (t * 5).max(std::time::Duration::from_secs(300)));
594
- if tx.send((i, outputWithin(cmd, fileLimit))).is_err() {
599
+ let result = outputWithin(cmd, fileLimit);
600
+ took.lock().unwrap().push((i, started.elapsed()));
601
+ if tx.send((i, result)).is_err() {
595
602
  break;
596
603
  }
597
604
  });
@@ -646,9 +653,54 @@ fn runSweepParallel(
646
653
  }
647
654
  }
648
655
  });
656
+ for (i, d) in took.into_inner().unwrap() {
657
+ durations.insert(durationKey(&files[i]), d.as_secs_f64());
658
+ }
659
+ saveTestDurations(&durations);
649
660
  Ok((passed, failed, filesFailed))
650
661
  }
651
662
 
663
+ /// Where a sweep keeps how long each file took last time, to start the slowest first.
664
+ fn testDurationsPath() -> std::path::PathBuf {
665
+ std::env::temp_dir().join("plum-test-durations.txt")
666
+ }
667
+
668
+ fn durationKey(file: &std::path::Path) -> String {
669
+ fs::canonicalize(file).unwrap_or_else(|_| file.to_path_buf()).display().to_string()
670
+ }
671
+
672
+ /// `seconds<TAB>path` lines; a missing or damaged file gives what it can.
673
+ fn loadTestDurations() -> std::collections::HashMap<String, f64> {
674
+ let text = fs::read_to_string(testDurationsPath()).unwrap_or_default();
675
+ text.lines()
676
+ .filter_map(|l| {
677
+ let (secs, path) = l.split_once('\t')?;
678
+ Some((path.to_string(), secs.parse().ok()?))
679
+ })
680
+ .collect()
681
+ }
682
+
683
+ /// Written to a temporary file and renamed, so sweeps running side by side never read half a file.
684
+ fn saveTestDurations(durations: &std::collections::HashMap<String, f64>) {
685
+ let mut lines: Vec<String> = durations.iter().map(|(p, s)| format!("{s:.3}\t{p}")).collect();
686
+ lines.sort();
687
+ let path = testDurationsPath();
688
+ let tmp = path.with_extension(format!("{}.tmp", process::id()));
689
+ if fs::write(&tmp, lines.join("\n") + "\n").is_ok() && fs::rename(&tmp, &path).is_err() {
690
+ let _ = fs::remove_file(&tmp);
691
+ }
692
+ }
693
+
694
+ /// The order a sweep starts its files in: slowest last time first; a file not timed yet
695
+ /// counts as slow (it may be), larger before smaller.
696
+ fn sweepOrder(files: &[std::path::PathBuf], durations: &std::collections::HashMap<String, f64>) -> Vec<usize> {
697
+ let cost = |f: &std::path::PathBuf| durations.get(&durationKey(f)).copied().unwrap_or(f64::INFINITY);
698
+ let size = |f: &std::path::PathBuf| fs::metadata(f).map_or(0, |m| m.len());
699
+ let mut order: Vec<usize> = (0..files.len()).collect();
700
+ order.sort_by(|&a, &b| cost(&files[b]).total_cmp(&cost(&files[a])).then(size(&files[b]).cmp(&size(&files[a]))));
701
+ order
702
+ }
703
+
652
704
  /// Runs `cmd` to the end like `Command::output`, but kills it once it runs past `limit`;
653
705
  /// the second value is then `Some(limit)`.
654
706
  fn outputWithin(
@@ -886,4 +938,15 @@ mod runtime_path_tests {
886
938
  fn runtimeCrateIsWhereBuildLooks() {
887
939
  assert!(super::runtimeCrateDir().join("Cargo.toml").is_file(), "{:?}", super::runtimeCrateDir());
888
940
  }
941
+
942
+ /// A sweep starts the files it has not timed, then the slowest last time.
943
+ #[test]
944
+ fn sweepStartsTheSlowestFirst() {
945
+ let files: Vec<std::path::PathBuf> = ["a.plum", "b.plum", "c.plum", "d.plum"].iter().map(Into::into).collect();
946
+ let mut d = std::collections::HashMap::new();
947
+ d.insert(super::durationKey(&files[0]), 1.0);
948
+ d.insert(super::durationKey(&files[1]), 9.0);
949
+ d.insert(super::durationKey(&files[3]), 3.0);
950
+ assert_eq!(super::sweepOrder(&files, &d), vec![2, 1, 3, 0]);
951
+ }
889
952
  }
compiler/scripts/test-all.sh CHANGED
@@ -67,16 +67,23 @@ failureLines() {
67
67
  '
68
68
  }
69
69
 
70
+ # The three modes run side by side (each one's slow tail overlaps the others'), reported in order.
70
- for flag in "" --no-fbip --no-fusion; do
71
+ modes='default --no-fbip --no-fusion'
72
+ for mode in $modes; do
73
+ flag=$mode
74
+ [ "$mode" = default ] && flag=""
71
75
  # shellcheck disable=SC2086
76
+ { target/debug/plum test $flag std examples tests > "$tmp/sweep$mode.out" 2>&1; echo $? > "$tmp/sweep$mode.status"; } &
77
+ done
78
+ wait
79
+ for mode in $modes; do
72
- out=$(target/debug/plum test $flag std examples tests 2>&1)
80
+ if [ "$(cat "$tmp/sweep$mode.status")" = "0" ]; then
73
- if [ $? -eq 0 ]; then
74
- echo "ok sweep ${flag:-default}"
81
+ echo "ok sweep $mode"
75
82
  else
76
- echo "FAIL sweep ${flag:-default}"
83
+ echo "FAIL sweep $mode"
77
84
  fail=$((fail + 1))
78
85
  failed_names="$failed_names
79
- $(echo "$out" | failureLines | head -80)"
86
+ $(failureLines < "$tmp/sweep$mode.out" | head -80)"
80
87
  fi
81
88
  done
82
89