From 92e0ac5ead11e260f556e37070681e934bdfb9da Mon Sep 17 00:00:00 2001 From: Project516 <138796702+Project516@users.noreply.github.com> Date: Sun, 22 Nov 2026 18:06:26 -0600 Subject: [PATCH 1/2] perf(sched): shorten the turns of a thread that spins while others block --- src/jvm.ts | 28 +++++++++++++++++--------- src/thread.ts | 3 +++ test/java/SpinningNotifier.java | 35 +++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 9 deletions(-) create mode 100644 test/java/SpinningNotifier.java diff --git a/src/jvm.ts b/src/jvm.ts index 37e2bcc..164522f 100644 --- a/src/jvm.ts +++ b/src/jvm.ts @@ -136,8 +136,11 @@ const WARM_MIN_CLASSES = 200; const WOKEN_RUN = 16; -// instructions a thread runs per turn +// instructions a thread runs per turn, at most and at least const SLICE = 20000; +const MIN_SLICE = 200; +// a turn this long (ms) is a spin or a long computation +const LONG_TURN = 0.05; export class JVM { readonly opts: JvmOptions; @@ -164,6 +167,7 @@ export class JVM { // threads another thread woke in its turn: they run before the rotation, so a hand-off does not wait behind spinners private woken: JThread[] = []; private wokenStreak = 0; + private shortTurns = 0; private queued = new Set(); private scheduled = false; private hashSeed = 0x2545f491; @@ -1100,8 +1104,7 @@ export class JVM { private loop() { noteYield(); const end = performance.now() + 20; - // instructions run since the clock was read: short turns of threads that hand off share one read - let spent = 0; + let clock = performance.now(); try { while (this.exitCode === null) { const t = this.next(); @@ -1109,17 +1112,24 @@ export class JVM { this.queued.delete(t); if (t.state !== RUNNABLE) continue; sched.thread = t; - spent += SLICE - runThread(t, SLICE); + const q = t.quantum || SLICE; + runThread(t, q); if (t.unmounting !== null) unmountBlocked(t); + const now = performance.now(); + // a thread that runs long turns while others run short ones gets shorter turns + const dt = now - clock; + const others = this.shortTurns !== t.shortSeen && (this.runQueue.length > 0 || this.woken.length > 0); + if (t.state !== RUNNABLE || !others) t.quantum = SLICE; + else if (dt > LONG_TURN) t.quantum = Math.max(MIN_SLICE, q >> 1); + else t.quantum = q; + if (t.state !== RUNNABLE || (q === SLICE && dt <= LONG_TURN)) this.shortTurns++; + t.shortSeen = this.shortTurns; + clock = now; if (t.state === RUNNABLE && !this.queued.has(t)) { this.queued.add(t); this.runQueue.push(t); } - if (sched.pause) break; - if (spent >= SLICE) { - if (performance.now() > end) break; - spent = 0; - } + if (sched.pause || now > end) break; sched.yieldNow = false; } } finally { diff --git a/src/thread.ts b/src/thread.ts index d723d0d..7691c02 100644 --- a/src/thread.ts +++ b/src/thread.ts @@ -172,6 +172,9 @@ export class JThread { // what is left of the run loop's budget while compiled code runs: its // loops and the calls it interprets in place spend it slice = 0; + // instructions of its next turn, and the VM's count of short turns when it last ran + quantum = 0; + shortSeen = 0; // wakes the thread early from sleep, park or wait, for interrupt/unpark wake: (() => void) | null = null; // set when the bottom frame finishes diff --git a/test/java/SpinningNotifier.java b/test/java/SpinningNotifier.java new file mode 100644 index 0000000..1d4d44f --- /dev/null +++ b/test/java/SpinningNotifier.java @@ -0,0 +1,35 @@ +import java.util.concurrent.Executors; +import java.util.concurrent.atomic.AtomicInteger; + +// The main thread spins on notify() while virtual threads wait on the same +// locks; the spinning must not hold up the threads it wakes. +public class SpinningNotifier { + public static void main(String[] args) { + int threads = 3000; + Object[] locks = {new Object(), new Object(), new Object(), new Object()}; + var finished = new AtomicInteger(); + try (var executor = Executors.newVirtualThreadPerTaskExecutor()) { + for (int i = 0; i < threads; i++) { + Object lock = locks[i % locks.length]; + executor.submit(() -> { + synchronized (lock) { + lock.wait(Long.MAX_VALUE); + } + finished.incrementAndGet(); + return null; + }); + synchronized (lock) { + lock.notify(); + } + } + while (finished.get() < threads) { + for (Object lock : locks) { + synchronized (lock) { + lock.notify(); + } + } + } + } + System.out.println("finished " + finished.get()); + } +} From eefdc1e3d7b69fbd3f19cdf543e9fc15d125966d Mon Sep 17 00:00:00 2001 From: Project516 <138796702+Project516@users.noreply.github.com> Date: Sun, 22 Nov 2026 18:28:18 -0600 Subject: [PATCH 2/2] perf(sched): read the clock only for threads that stayed runnable after a turn --- src/jvm.ts | 44 +++++++++++++++++++++++++++++--------------- src/thread.ts | 2 ++ 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/jvm.ts b/src/jvm.ts index 164522f..d322d30 100644 --- a/src/jvm.ts +++ b/src/jvm.ts @@ -1104,7 +1104,8 @@ export class JVM { private loop() { noteYield(); const end = performance.now() + 20; - let clock = performance.now(); + // instructions run since the clock was read: short turns of threads that hand off share one read + let spent = 0; try { while (this.exitCode === null) { const t = this.next(); @@ -1113,23 +1114,36 @@ export class JVM { if (t.state !== RUNNABLE) continue; sched.thread = t; const q = t.quantum || SLICE; - runThread(t, q); + // only a thread that stayed runnable is timed, so hand-offs read no clock + const t0 = t.timed ? performance.now() : 0; + spent += q - runThread(t, q); if (t.unmounting !== null) unmountBlocked(t); - const now = performance.now(); - // a thread that runs long turns while others run short ones gets shorter turns - const dt = now - clock; - const others = this.shortTurns !== t.shortSeen && (this.runQueue.length > 0 || this.woken.length > 0); - if (t.state !== RUNNABLE || !others) t.quantum = SLICE; - else if (dt > LONG_TURN) t.quantum = Math.max(MIN_SLICE, q >> 1); - else t.quantum = q; - if (t.state !== RUNNABLE || (q === SLICE && dt <= LONG_TURN)) this.shortTurns++; + if (t.state !== RUNNABLE) { + this.shortTurns++; + t.quantum = SLICE; + t.timed = false; + } else { + // a thread that runs long turns while others run short ones gets shorter turns + const now = performance.now(); + const dt = now - t0; + const others = this.shortTurns !== t.shortSeen && (this.runQueue.length > 0 || this.woken.length > 0); + const long = t.timed && dt > LONG_TURN; + if (t.timed && q === SLICE && !long) this.shortTurns++; + t.quantum = !others ? SLICE : long ? Math.max(MIN_SLICE, q >> 1) : q; + t.timed = true; + if (!this.queued.has(t)) { + this.queued.add(t); + this.runQueue.push(t); + } + if (now > end) break; + spent = 0; + } t.shortSeen = this.shortTurns; - clock = now; - if (t.state === RUNNABLE && !this.queued.has(t)) { - this.queued.add(t); - this.runQueue.push(t); + if (sched.pause) break; + if (spent >= SLICE) { + if (performance.now() > end) break; + spent = 0; } - if (sched.pause || now > end) break; sched.yieldNow = false; } } finally { diff --git a/src/thread.ts b/src/thread.ts index 7691c02..e227d29 100644 --- a/src/thread.ts +++ b/src/thread.ts @@ -175,6 +175,8 @@ export class JThread { // instructions of its next turn, and the VM's count of short turns when it last ran quantum = 0; shortSeen = 0; + // its last turn ended with it still runnable + timed = false; // wakes the thread early from sleep, park or wait, for interrupt/unpark wake: (() => void) | null = null; // set when the bottom frame finishes