Files
five/hbrt/ops_collection.go
CharlesKWON 15aa6dd4b3 perf(vm): in-place stack ops + ArrayGen/EvalBlock shift — global 2-6%
Three small tweaks to the pop-push hotspots in the VM.

 - ops_arith.go Inc/Dec/AddInt: unary ops mutate the top stack slot in
   place via peekPtr() instead of pop-compute-push. Drops the bounds
   check + cachedNil clear + push bounds check per call. Biggest
   beneficiary: FOR loop counters (implicit Inc) — every iteration of
   every PRG loop pays these ops once.

 - ops_collection.go ArrayGen: consume N slots via a single `copy`
   into the freshly-allocated result slice, then rewind sp and clear
   the intermediate slots for GC (the first slot is overwritten by
   the array push). Skips the N-deep pop loop.

 - ops_collection.go EvalBlock: read block value before shift, collapse
   args down one slot to overwrite the block position, then let the
   block run against the same in-place layout. Matches the
   Function()/PushSymbol round-trip removal from the prior commit.

bench_sql deltas

 - B2  WHERE            83 →  78 µs  (6%)
 - B3  ORDER BY         96 →  90 µs  (6%)
 - B4  GROUP_HAVING    554 → 528 µs  (5%)
 - B9  ROW_NUMBER      255 → 241 µs  (5%)
 - B10 RANK PART       296 → 278 µs  (6%)
 - B11 SUM OVER        320 → 300 µs  (6%)
 - B15 CTE+WIN+JOIN   1826 →1743 µs  (5%)

Verification
 - go test ./...              ALL PASS
 - FiveSql2 test_sql1999      43/43
 - tests/compat_harbour       56/56

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-17 20:48:15 +09:00

161 lines
3.9 KiB
Go

// Copyright (c) 2026 Charles KWON OhJun (charleskwonohjun@gmail.com)
// All rights reserved.
// Collection operations for the Five runtime.
// Array generation, indexing, hash generation, and code block evaluation.
// Harbour: HB_P_ARRAYGEN, HB_P_ARRAYPUSH, HB_P_ARRAYPOP, etc.
package hbrt
import "unsafe"
// ArrayGen pops n items from stack and creates an array.
// Harbour: HB_P_ARRAYGEN / hb_vmArrayGen
func (t *Thread) ArrayGen(n int) {
if n <= 0 {
t.push(MakeArrayFrom(nil))
return
}
items := make([]Value, n)
base := t.sp - n
copy(items, t.stack[base:t.sp])
// Clear the consumed slots so the popped values can be GC'd.
// The first slot gets overwritten by the push below; clear the
// rest (base+1 … sp-1).
for i := base + 1; i < t.sp; i++ {
t.stack[i] = cachedNil
}
t.sp = base
t.push(MakeArrayFrom(items))
}
// HashGen pops n key-value pairs and creates a hash.
// Stack: [key1] [val1] [key2] [val2] ... → Hash
//
// Duplicate keys follow Harbour hash-literal semantics: the last
// assignment wins and no second slot is created. Lookup/Set invoked
// inside the reverse-scan pop loop would be order-inverted, so we
// first materialize all N pairs in stack order and then feed them
// forward into the hash via Set.
func (t *Thread) HashGen(n int) {
keys := make([]Value, n)
vals := make([]Value, n)
for i := n - 1; i >= 0; i-- {
vals[i] = t.pop()
keys[i] = t.pop()
}
hh := &HbHash{}
for i := 0; i < n; i++ {
hh.Set(keys[i], vals[i])
}
t.push(Value{
info: makeInfo(tHash, 0, 0),
ptr: unsafe.Pointer(hh),
})
}
// ArrayPush pops index and array/hash, pushes array[index] or hash[key].
// Harbour: HB_P_ARRAYPUSH
func (t *Thread) ArrayPush() {
idx := t.pop()
arr := t.pop()
// Hash: h[key] → value
if arr.IsHash() {
hh := arr.AsHash()
if i := hh.Lookup(idx); i >= 0 {
t.push(hh.Values[i])
return
}
t.push(MakeNil())
return
}
if !arr.IsArray() {
// String indexing: cString[n] → single char
if arr.IsString() {
s := arr.AsString()
n := int(idx.AsNumInt())
if n >= 1 && n <= len(s) {
t.push(MakeString(string(s[n-1])))
return
}
t.push(MakeString(""))
return
}
panic(t.argError("[]", arr, idx))
}
ha := arr.AsArray()
n := int(idx.AsNumInt())
// Harbour: 1-based indexing
if n < 1 || n > len(ha.Items) {
panic(t.runtimeError("array index out of bounds"))
}
t.push(ha.Items[n-1])
}
// ArrayPop pops value, index, array/hash and sets array[index]=value or hash[key]=value.
// Harbour: HB_P_ARRAYPOP
func (t *Thread) ArrayPop() {
val := t.pop()
idx := t.pop()
arr := t.pop()
// Hash: h[key] := value
if arr.IsHash() {
arr.AsHash().Set(idx, val)
return
}
if !arr.IsArray() {
panic(t.argError("[]=", arr, idx))
}
ha := arr.AsArray()
n := int(idx.AsNumInt())
if n < 1 || n > len(ha.Items) {
panic(t.runtimeError("array index out of bounds"))
}
ha.Items[n-1] = val
}
// EvalBlock evaluates a code block on the stack with nArgs arguments.
// Stack: [block] [arg1] ... [argN] → [result]
//
// Same pop-push-dance removal as call.Function(): shift the args down
// by one to overwrite the block slot instead of popping and pushing
// round-trip. The block Value itself is read before the shift.
func (t *Thread) EvalBlock(nArgs int) {
blockIdx := t.sp - nArgs - 1
if blockIdx < 0 {
panic(t.runtimeError("stack underflow (EvalBlock)"))
}
blockVal := t.stack[blockIdx]
if !blockVal.IsBlock() {
panic(t.argError("Eval", blockVal))
}
blk := blockVal.AsBlock()
// Shift args down by one to overwrite the block slot.
if nArgs > 0 {
copy(t.stack[blockIdx:blockIdx+nArgs], t.stack[blockIdx+1:t.sp])
}
t.stack[t.sp-1] = cachedNil
t.sp--
t.pendingParams = nArgs
blk.Fn(t)
t.push(t.retVal)
}
// PushBlock creates a code block and pushes it onto the stack.
func (t *Thread) PushBlock(fn func(*Thread), detachedLocals int) {
t.push(MakeBlock(fn, detachedLocals))
}
// PushSelf pushes the current Self object (for :: access in methods).
func (t *Thread) PushSelf() {
t.push(t.self)
}