From 3df967056bf48bd9f87900f3a1ad9af2aedce948 Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Wed, 28 Oct 2026 16:14:30 -0700 Subject: Backport some minor speedups --- .../java/com/seaofnodes/simple/Chapter21Test.java | 2 +- .../seaofnodes/simple/SchedulingScratchTest.java | 32 ++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 chapter23/src/test/java/com/seaofnodes/simple/SchedulingScratchTest.java (limited to 'chapter23/src/test/java/com/seaofnodes') diff --git a/chapter23/src/test/java/com/seaofnodes/simple/Chapter21Test.java b/chapter23/src/test/java/com/seaofnodes/simple/Chapter21Test.java index f25a881a..cfd09502 100644 --- a/chapter23/src/test/java/com/seaofnodes/simple/Chapter21Test.java +++ b/chapter23/src/test/java/com/seaofnodes/simple/Chapter21Test.java @@ -416,7 +416,7 @@ public class Chapter21Test { String sprimes = sb.p("]").toString(); // Compile, link against native C; expect the above string of primes to be printed out by C - TestC.run("sieve",sprimes, 186); + TestC.run("sieve",sprimes, 113); // Evaluate on RISC5 emulator; expect return of an array of primes in // the simulated heap. diff --git a/chapter23/src/test/java/com/seaofnodes/simple/SchedulingScratchTest.java b/chapter23/src/test/java/com/seaofnodes/simple/SchedulingScratchTest.java new file mode 100644 index 00000000..319c94b0 --- /dev/null +++ b/chapter23/src/test/java/com/seaofnodes/simple/SchedulingScratchTest.java @@ -0,0 +1,32 @@ +package com.seaofnodes.simple; + +import com.seaofnodes.simple.codegen.CodeGen; +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +public class SchedulingScratchTest { + @Test public void testScratchAcrossCompilations() { + String src = """ + int[] !xs = new int[2]; + xs[0] = arg; + int sum = 0; + int i = 0; + while( i<5 ) { + int old = xs[arg&1]; + xs[0] = i; + sum = sum + old; + i = i + 1; + } + return sum; + """; + // Each fresh compilation reuses node IDs and the scheduler's scratch. + for( int round=0; round<3; round++ ) { + CodeGen code = new CodeGen(src).driver(CodeGen.Phase.TypeCheck); + // A small graph after many earlier node allocations. + if( round==0 ) code._uid += 100_000; + assertEquals("6",Eval2.eval(code,0)); + assertEquals("0",Eval2.eval(code,1)); + assertEquals("8",Eval2.eval(code,2)); + } + } +} -- cgit v1.2.3