From 5f7b2c7900a5d7f7cd35e9986ce718b8773283ad Mon Sep 17 00:00:00 2001 From: folbrich Date: Tue, 20 Oct 2026 13:20:37 +0200 Subject: [PATCH] Check inline whether a VM's stack may shrink, so a VM call skips shrink when it cannot Since the stack shrink was added, every VM call ran shrink's full check, about 35 instructions, and VM.leave was no longer inlined: 47 more instructions per call into a private VM. shrink is now an inlined guard that runs the full check only when the stack or frames are past the pool's bounds or a shrink is counting down; otherwise the check could only reset a counter that is already zero. A call into a private VM goes from 828 to 803 instructions. --- live.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/live.go b/live.go index 1cae916..96be225 100644 --- a/live.go +++ b/live.go @@ -161,6 +161,15 @@ func (c *Call) clearDead() { // that goes deep now and then keeps what it grew, rather than paying to grow it again; one // deep call long past is given back. The frames follow the stack: every frame takes a slot. func (ex *executor) shrink(used int) { + // inlined at every VM call: a stack and frames within the pool's bounds never shrink, + // so only the counter of a shrink under way needs the full check + if len(ex.stack) > maxPoolStack || len(ex.pframes) > maxPoolFrames || ex.quietChecks != 0 { + ex.shrinkCheck(used) + } +} + +// shrinkCheck is shrink's full check. +func (ex *executor) shrinkCheck(used int) { need := used + stackSlack n := max(64, 2*need, 2*(ex.st.nf+1)) stack := len(ex.stack) > maxPoolStack && len(ex.stack) > 4*need