diff --git a/hbrt/pcinterp.go b/hbrt/pcinterp.go index 4fd00b5..fe46699 100644 --- a/hbrt/pcinterp.go +++ b/hbrt/pcinterp.go @@ -14,12 +14,34 @@ import ( ) // ExecPcode runs a pcode function on the given thread. +// Full variant — installs a defer/recover so panics from inside the +// pcode body (HbError, BreakValue, user Break) are re-panicked with +// proper frame unwinding. Used for general-purpose pcode evaluation. func ExecPcode(t *Thread, fn *PcodeFunc, mod *PcodeModule) { - code := fn.Code - pc := 0 // program counter - t.Frame(fn.Params, fn.Locals) defer t.EndProc() + execPcodeBody(t, fn, mod) +} + +// ExecPcodeFast is a hot-path variant for short, pure expressions +// (FiveSql2 WHERE predicates, inline lambdas) where the caller has +// already guaranteed that the body will not panic with HbError / +// BreakValue. Skips the defer+recover dance in EndProc, saving ~15ns +// per call × tens of thousands of rows in scan loops. +// +// Contract: caller is responsible for panic discipline. If the pcode +// body panics, the frame stack is still cleaned up (EndProcFast) but +// no diagnostic is logged and SEQUENCE/RECOVER will not see the panic. +func ExecPcodeFast(t *Thread, fn *PcodeFunc, mod *PcodeModule) { + t.Frame(fn.Params, fn.Locals) + execPcodeBody(t, fn, mod) + t.EndProcFast() +} + +// execPcodeBody is the shared opcode dispatch loop. +func execPcodeBody(t *Thread, fn *PcodeFunc, mod *PcodeModule) { + code := fn.Code + pc := 0 // program counter for pc < len(code) { op := code[pc] diff --git a/hbrtl/sqlscan.go b/hbrtl/sqlscan.go index 15cac38..c06c203 100644 --- a/hbrtl/sqlscan.go +++ b/hbrtl/sqlscan.go @@ -111,14 +111,17 @@ func SqlScan(t *hbrt.Thread) { } rows := make([]hbrt.Value, 0, estRows) flat := make([]hbrt.Value, 0, estRows*nFields) + slab := hbrt.NewArraySlab(estRows) // Scan area.GoTop() for !area.EOF() { - // WHERE evaluation (if any) + // WHERE evaluation (if any). Fast variant — WHERE expressions + // compiled from SQL AST don't contain BEGIN SEQUENCE, so we can + // skip the defer/recover frame exit. keep := true if whereFn != nil { - hbrt.ExecPcode(t, whereFn, nil) + hbrt.ExecPcodeFast(t, whereFn, nil) keep = t.GetRetValue().AsBool() } @@ -141,7 +144,7 @@ func SqlScan(t *hbrt.Thread) { v, _ := area.GetValue(fieldPos[i] - 1) row[i] = v } - rows = append(rows, hbrt.MakeArrayFrom(row)) + rows = append(rows, slab.WrapNext(row)) } area.Skip(1)