perf(fivesql2): Go-native SqlIsAggName — drop per-row substring scan
B4 GROUP+HAVING profile showed SqlIsAggName at ~9% of CPU —
SqlEvalFunc checks it for every function in every row, and the
PRG body was two string allocations + a substring scan:
RETURN ("," + c + ",") $ ("," + AGG_FUNCTIONS + ",")
Replace with a hash lookup against the existing aggFuncSet map
in hbrtl/sqlexpr.go (already populated for SqlExprHasAgg, same
AGG_FUNCTIONS list). Upper-casing skips the allocation when the
input is already upper, which it almost always is in practice.
Bench deltas (median of 3 steady runs, 1000 iters):
B4_GROUP_HAVING 447 → 418 us -6.5%
B14_COUNT 252 → 235 us -7%
B15_CTE_WIN_JOIN 1595 → 1577 us -1%
Other benches unchanged (no aggregate calls per row).
FiveSql2 43/43, Harbour compat 56/56.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -637,6 +637,7 @@ func RegisterRTL(vm *hbrt.VM) {
|
||||
hbrt.Sym("SQLCOERCENUM", hbrt.FsPublic, SqlCoerceNum),
|
||||
hbrt.Sym("SQLCOERCEFORCMP", hbrt.FsPublic, SqlCoerceForCmp),
|
||||
hbrt.Sym("SQLISTRUE", hbrt.FsPublic, SqlIsTrue),
|
||||
hbrt.Sym("SQLISAGGNAME", hbrt.FsPublic, SqlIsAggName),
|
||||
hbrt.Sym("SQLCMPEQ", hbrt.FsPublic, SqlCmpEq),
|
||||
hbrt.Sym("SQLCMPLT", hbrt.FsPublic, SqlCmpLt),
|
||||
hbrt.Sym("SQLEXTRACTTEMPLATE", hbrt.FsPublic, SqlExtractTemplate),
|
||||
|
||||
@@ -585,3 +585,30 @@ func sqlCmpLt(a, b hbrt.Value) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// SqlIsAggName(cName) → lBool
|
||||
// Go-native replacement for TSqlExpr.prg SqlIsAggName. The PRG version
|
||||
// was `("," + c + ",") $ ("," + AGG_FUNCTIONS + ",")` — two string
|
||||
// allocations + a substring scan per call. Profile showed this at
|
||||
// 8.7% of B4 GROUP+HAVING CPU. Uses the aggFuncSet already declared
|
||||
// in sqlexpr.go for SqlExprHasAgg.
|
||||
func SqlIsAggName(t *hbrt.Thread) {
|
||||
t.Frame(1, 0)
|
||||
defer t.EndProc()
|
||||
name := t.Local(1).AsString()
|
||||
if name == "" {
|
||||
t.RetBool(false)
|
||||
return
|
||||
}
|
||||
// Upper-case without allocating unless needed.
|
||||
upper := name
|
||||
for i := 0; i < len(name); i++ {
|
||||
c := name[i]
|
||||
if c >= 'a' && c <= 'z' {
|
||||
upper = strings.ToUpper(name)
|
||||
break
|
||||
}
|
||||
}
|
||||
_, ok := aggFuncSet[upper]
|
||||
t.RetBool(ok)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user