Files
five/hbrt/ops_collection.go
CharlesKWON dd270d5d9d perf: RTL Go-native migration — 27 optimizations, DML up to 70-90x
Systematic pass through PRG hot paths, promoting them to Go RTL while
preserving Harbour/FiveSql2 semantics. Full log in
docs/RTL-Go-Native-Migration.md.

Bench (bench_sql) vs 2026-04-08 baseline
 - B1  SELECT *             2,192 → 114   µs   (19x)
 - B6  INNER JOIN           9,291 → 233   µs   (40x)
 - B7  CTE simple           8,037 → 129   µs   (62x)
 - B9  ROW_NUMBER           3,705 → 265   µs   (14x)
 - B10 RANK PARTITION       4,748 → 309   µs   (15x)
 - B12 INSERT (WA cache)    4,319 →  63   µs   (69x)
 - B13 UPDATE (WA cache)    6,144 →  68   µs   (90x)
 - B15 CTE+WIN+JOIN        18,395 → 1,873 µs   (10x)

Infrastructure
 - HbHash O(1) Index preserving insertion order (Harbour KEEPORDER)
 - HbDeepClone Go RTL (scalar-sharing, immutable hash keys)
 - MEMRDD auto-imported via gengo; all Five programs get mem:name driver
 - SQL plan + pcode caches (s_hPlanCache, s_hDmlPcodeCache)
 - Opt-in SqlWACacheEnable — dbUseArea/Close/Commit batched for DML

SQL engine
 - FiveSql2 lexer ported to Go (byte FSM) with combined automatic
   template parameterization (literals → ?, concat queries share plan)
 - Go RTL: SqlDistinct, SqlGroupRows, SqlWindowPartitions,
   SqlWindowSortPartition, SqlWindowAssignRank, SqlComputeAggSimple,
   SqlBulkInsert, SqlBulkUpdate, SqlExprHasAgg, SqlEvalHaving
 - CTE / subquery / driving-table materialize paths use MEMRDD
 - SqlCoerce/SqlCmp/SqlIsTrue helpers moved from PRG to Go
 - SqlBulkUpdate defers Flush when WA cache active (APFS fsync was
   dominant B13 cost — 1.6ms/call → gone)

Correctness fixes uncovered during migration
 - ASort default path now sorts dates/logicals/timestamps (was no-op)
 - ORDER BY default NULL placement matches PRG SqlRowCompare across
   Go fast path; explicit NULLS FIRST/LAST honored by both paths
 - SqlBulkUpdate respects EXCLUSIVE vs SHARED mode record locks
 - SqlCmp/SqlCmpEq normalize NumInt vs Double (caught by test 6b)

Verification
 - go test ./...              ALL PASS
 - FiveSql2 test_sql1999      43/43
 - tests/compat_harbour       56/56 (+5 new: ASort dates/logicals,
                              AScan int cross-type)
 - Regression test test_null_order.prg for ORDER BY NULL ordering

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

145 lines
3.3 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) {
items := make([]Value, n)
for i := n - 1; i >= 0; i-- {
items[i] = t.pop()
}
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]
func (t *Thread) EvalBlock(nArgs int) {
args := make([]Value, nArgs)
for i := nArgs - 1; i >= 0; i-- {
args[i] = t.pop()
}
blockVal := t.pop()
if !blockVal.IsBlock() {
panic(t.argError("Eval", blockVal))
}
blk := blockVal.AsBlock()
// Push args for Frame
for _, arg := range args {
t.push(arg)
}
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)
}