perf(hbrt): ExecPcodeFast — pcode variant without defer/recover

Pcode expressions compiled from SQL WHERE clauses (via genpc.CompileExpr)
never contain BEGIN SEQUENCE and can't raise BreakValue, so the defer +
recover dance in ExecPcode's EndProc is pure overhead. For FiveSql2's
per-row WHERE evaluation on a 50k-row scan, that's 50k × ~15ns = ~750µs
of pointless recover bookkeeping.

Split ExecPcode into two variants sharing execPcodeBody:

  ExecPcode     — full: Frame + defer EndProc. General-purpose,
                  handles panics. Behavior unchanged.

  ExecPcodeFast — hot: Frame + execPcodeBody + EndProcFast. No defer,
                  no recover. Caller guarantees the pcode body can't
                  panic with HbError / BreakValue.

SqlScan now uses ExecPcodeFast for per-row WHERE evaluation. Measured
impact on 50k-row no-WHERE benchmark: 10.6ms → 9.2ms steady state
(~13% faster). Effect is smaller on numeric-WHERE because per-row
cost there is dominated by the opcode dispatch itself, not the frame
exit.

Validation:
  - FiveSql2 43/43
  - go test ./hbrt/... PASS (pcode tests)
  - go test ./hbrtl/... PASS

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-14 12:07:54 +09:00
parent ad69221136
commit 5c067f35a4
2 changed files with 31 additions and 6 deletions

View File

@@ -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)