From 62a9b27544f94ba303ae307c33a2eb9a2360cbd4 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 12 Oct 2026 17:39:35 +0100 Subject: [PATCH] The kernels, with their Pascal twins test/bench holds three: a heap sort, a sum over a slice and a search for a word in a text, each with check off; and its twin, built by FPC -O2. make kernels times the two of each, the fastest of five runs: today 1.34, 1.75 and 1.2 times FPC's. --- diff --git a/CLAUDE.md b/CLAUDE.md index 6cc9ecf..d780f24 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -11,6 +11,7 @@ make raz # the compiler, ./raz; its library is lib/ make check # every example and test against its expected output make bench # must report at least 400,000 lines a second + make kernels # test/bench's kernels against FPC -O2's twins make loc # lines of the compiler, by rloc; the last, code, is the budget make tests # the small Pascal tests of scan, x64, link and elf make gdb # gdb stops at a line of a test and names it diff --git a/Makefile b/Makefile index e0b3cad..4c9add0 100644 --- a/Makefile +++ b/Makefile @@ -52,6 +52,11 @@ echo "$$lines lines in $$((best / 1000000)) ms: $$rate lines a second"; \ test $$rate -ge 400000 +# the code's speed: the kernels of test/bench, which say check off, +# against their twins in Pascal built by FPC -O2 +kernels: raz + test/bench/run.sh + # native threads under load: 40,000 spawned and joined, 200 alive at a # time, all waiting on one Mutex; then, with little memory, spawn must # fail with Thread.Failed. make check runs a small version. @@ -114,4 +119,4 @@ rm -rf $(PREFIX)/lib/raz $(PREFIX)/bin/raz $(MAN)/man1/raz.1 $(MAN)/man3/raz-lib.3 \ $(MAN)/man7/raz.7 $(MAN)/man7/raz-tutorial.7 -.PHONY: units tests check gdb bench stress fmt loc man pdf clean install uninstall +.PHONY: units tests check gdb bench kernels stress fmt loc man pdf clean install uninstall diff --git a/PLAN.md b/PLAN.md index d11c22f..8dc293a 100644 --- a/PLAN.md +++ b/PLAN.md @@ -11,10 +11,10 @@ ## Now: better code again (v0.8.0) For: on the same program, raz within 1.3 times the time of FPC `-O2` -with `check off;` (today 1.3 on a heap sort, 1.7 on a sum over a slice, -1.8 on a search: these kernels go in -`test/bench` first, with their Pascal twins, so anyone can measure -them), and every default program smaller; the compiler as clear as +with `check off;` (on `test/bench`'s kernels, which `make kernels` +measures against their Pascal twins: today 1.34 on a heap sort, 1.75 +on a sum over a slice, 1.2 on a search for a word in a text), and +every default program smaller; the compiler as clear as v0.7.0 left it, and under 10,000 lines. No change to the language. Each step is measured as v0.7.0's were, on rat and on its kernel, and one that buys less than 2% on both is reverted and noted here. diff --git a/test/bench/Heapsort.raz b/test/bench/Heapsort.raz new file mode 100644 index 0000000..856e87f --- /dev/null +++ b/test/bench/Heapsort.raz @@ -0,0 +1,61 @@ +(* A heap sort of pseudo-random numbers. *) +module Heapsort; +import Out; +check off; + +const + N = 200000; + Rounds = 8; + +var a: [N]INT; + +(* s[lo] sifted down the heap of s[0..hi] *) +proc sift(s: []INT; lo, hi: INT); +var root, child, t: INT; +begin + root := lo; + child := 2 * root + 1; + while child < hi do + if (child + 1 < hi) and (s[child] < s[child + 1]) then INC(child); end; + if s[root] >= s[child] then return; end; + t := s[root]; + s[root] := s[child]; + s[child] := t; + root := child; + child := 2 * root + 1; + end; +end; + +proc sort(s: []INT); +var i, t: INT; +begin + for i := LEN(s) div 2 - 1 to 0 by -1 do sift(s, i, LEN(s)); end; + for i := LEN(s) - 1 to 1 by -1 do + t := s[0]; + s[0] := s[i]; + s[i] := t; + sift(s, 0, i); + end; +end; + +proc main*; +var i, r, x, total: INT; +begin + x := 1; + total := 0; + for r := 1 to Rounds do + for i := 0 to N - 1 do + x := (x * 1103515245 + 12345) & 7FFFFFFFH; + a[i] := x; + end; + sort(a); + for i := 1 to N - 1 do + if a[i - 1] > a[i] then Out.str("not sorted\n"); end; + end; + total := total + a[N div 2]; + end; + Out.int(total); + Out.ln; +end; + +end Heapsort. diff --git a/test/bench/Search.raz b/test/bench/Search.raz new file mode 100644 index 0000000..ff6cfad --- /dev/null +++ b/test/bench/Search.raz @@ -0,0 +1,42 @@ +(* A search: where a word first is in a text, by trying each place in + turn, over a text of four letters. *) +module Search; +import Out; +check off; + +const + N = 100000; + Rounds = 400; + +var text: [N]U8; + +(* where sub first starts in s, or -1 *) +proc find(s, sub: []U8): INT; +var i, j: INT; +begin + for i := 0 to LEN(s) - LEN(sub) do + j := 0; + while (j < LEN(sub)) and (s[i + j] = sub[j]) do INC(j); end; + if j = LEN(sub) then return i; end; + end; + return -1; +end; + +proc main*; +var i, r, x, total: INT; word: [16]U8; +begin + x := 1; + for i := 0 to N - 1 do + x := (x * 1103515245 + 12345) & 7FFFFFFFH; + text[i] := U8('a' + (x shr 16) & 3); + end; + total := 0; + for r := 1 to Rounds do + COPY(word, text[N - 16 - r..N - r]); + total := total + find(text, word); + end; + Out.int(total); + Out.ln; +end; + +end Search. diff --git a/test/bench/Sum.raz b/test/bench/Sum.raz new file mode 100644 index 0000000..b89bba1 --- /dev/null +++ b/test/bench/Sum.raz @@ -0,0 +1,30 @@ +(* A sum over a slice: the loop of a procedure that takes one. *) +module Sum; +import Out; +check off; + +const + N = 1000000; + Rounds = 300; + +var a: [N]INT; + +proc sum(s: []INT): INT; +var i, t: INT; +begin + t := 0; + for i := 0 to LEN(s) - 1 do t := t + s[i]; end; + return t; +end; + +proc main*; +var i, r, total: INT; +begin + for i := 0 to N - 1 do a[i] := i; end; + total := 0; + for r := 1 to Rounds do total := total + sum(a); end; + Out.int(total); + Out.ln; +end; + +end Sum. diff --git a/test/bench/heapsort.pas b/test/bench/heapsort.pas new file mode 100644 index 0000000..b372412 --- /dev/null +++ b/test/bench/heapsort.pas @@ -0,0 +1,53 @@ +{ Heapsort.raz's twin. } +program heapsort; +{$mode objfpc} +const + N = 200000; + Rounds = 8; +var + a: array[0..N - 1] of int64; + i, r, x, total: int64; + +procedure Sift(var s: array of int64; lo, hi: int64); +var root, child, t: int64; +begin + root := lo; + child := 2 * root + 1; + while child < hi do begin + if (child + 1 < hi) and (s[child] < s[child + 1]) then inc(child); + if s[root] >= s[child] then exit; + t := s[root]; + s[root] := s[child]; + s[child] := t; + root := child; + child := 2 * root + 1; + end; +end; + +procedure Sort(var s: array of int64); +var i, t: int64; +begin + for i := Length(s) div 2 - 1 downto 0 do Sift(s, i, Length(s)); + for i := High(s) downto 1 do begin + t := s[0]; + s[0] := s[i]; + s[i] := t; + Sift(s, 0, i); + end; +end; + +begin + x := 1; + total := 0; + for r := 1 to Rounds do begin + for i := 0 to N - 1 do begin + x := (x * 1103515245 + 12345) and $7FFFFFFF; + a[i] := x; + end; + Sort(a); + for i := 1 to N - 1 do + if a[i - 1] > a[i] then writeln('not sorted'); + total := total + a[N div 2]; + end; + writeln(total); +end. diff --git a/test/bench/run.sh b/test/bench/run.sh new file mode 100755 index 0000000..501e147 --- /dev/null +++ b/test/bench/run.sh @@ -0,0 +1,27 @@ +#!/bin/sh +# The kernels: each raz program here, which says check off, against its +# twin in Pascal, built by FPC -O2; each run five times, the fastest +# kept. Run from the directory above test/: +# test/bench/run.sh [compiler] +raz=${1:-./raz} +out=build/bench +mkdir -p $out + +best() { # best program: the fewest milliseconds of five runs + b=0 + for i in 1 2 3 4 5; do + s=$(date +%s%N); $1 > /dev/null; e=$(date +%s%N) + t=$(( (e - s) / 1000000 )) + if [ $b = 0 ] || [ $t -lt $b ]; then b=$t; fi + done + echo $b +} + +for f in test/bench/*.raz; do + n=$(basename $f .raz) + $raz -o $out/$n $f || exit 1 + fpc -O2 -v0 -FE$out -o$out/$n.fpc test/bench/$(echo $n | tr A-Z a-z).pas > /dev/null || exit 1 + if [ "$($out/$n)" != "$($out/$n.fpc)" ]; then echo "$n: the two print differently"; exit 1; fi + r=$(best $out/$n); p=$(best $out/$n.fpc) + echo "$n $r $p" | awk '{ printf "%-10s raz %5d ms fpc -O2 %5d ms %.2f times\n", $1, $2, $3, $2 / $3 }' +done diff --git a/test/bench/search.pas b/test/bench/search.pas new file mode 100644 index 0000000..24c70fb --- /dev/null +++ b/test/bench/search.pas @@ -0,0 +1,35 @@ +{ Search.raz's twin. } +program search; +{$mode objfpc} +const + N = 100000; + Rounds = 400; +var + text: array[0..N - 1] of byte; + word: array[0..15] of byte; + i, r, x, total: int64; + +function Find(const s, sub: array of byte): int64; +var i, j: int64; +begin + for i := 0 to Length(s) - Length(sub) do begin + j := 0; + while (j < Length(sub)) and (s[i + j] = sub[j]) do inc(j); + if j = Length(sub) then exit(i); + end; + Find := -1; +end; + +begin + x := 1; + for i := 0 to N - 1 do begin + x := (x * 1103515245 + 12345) and $7FFFFFFF; + text[i] := ord('a') + (x shr 16) and 3; + end; + total := 0; + for r := 1 to Rounds do begin + Move(text[N - 16 - r], word, 16); + total := total + Find(text, word); + end; + writeln(total); +end. diff --git a/test/bench/sum.pas b/test/bench/sum.pas new file mode 100644 index 0000000..db621ad --- /dev/null +++ b/test/bench/sum.pas @@ -0,0 +1,24 @@ +{ Sum.raz's twin: a sum over an open array, its slice. } +program sum; +{$mode objfpc} +const + N = 1000000; + Rounds = 300; +var + a: array[0..N - 1] of int64; + i, r, total: int64; + +function Sum(const s: array of int64): int64; +var i, t: int64; +begin + t := 0; + for i := 0 to High(s) do t := t + s[i]; + Sum := t; +end; + +begin + for i := 0 to N - 1 do a[i] := i; + total := 0; + for r := 1 to Rounds do total := total + Sum(a); + writeln(total); +end.