perf(FiveSql2): PcOpFieldTrim fused peephole — string WHERE at raw RDD parity
Second pcode peephole to match the one added for FieldGet(literal). SqlExprToPrg auto-wraps CHAR column references with AllTrim() to match SqlCmpEq's CHAR-padding trim semantics, so every string WHERE predicate evaluates `AllTrim(FieldGet(n)) == 'literal'` per row. Before this commit each of those per-row evaluations did: 1. PushSymbol ALLTRIM 2. PushSymbol FIELDGET → Function(1) [1 RTL Frame] 3. parseCharField → MakeString [alloc: copies raw bytes] 4. Function(1) → AllTrim RTL [1 RTL Frame] 5. strings.TrimSpace [alloc: new string] 6. Return, continue New opcode `PcOpFieldTrim <idx>` (0x47) fuses the two RTL calls into a single opcode that: 1. Calls FastFieldGetter directly (no Frame/Function dispatch). 2. Walks the returned string with ASCII-space trim in place. 3. Pushes `s[lo:hi]` — a sub-slice, no new allocation. 4. Short-circuits back to the same string if no trim needed. genpc recognizes the shape `AllTrim(FieldGet(<int-literal>))` in emitCall and emits the fused opcode automatically — no SQL-side API change. Matches the existing FieldGet peephole's shape. Bench impact (50k rows, 3-run steady state, vs raw RDD baseline 6.2ms): String WHERE before 7.9ms → after 6.2ms 1.00x (parity!) Numeric WHERE 6.9ms (unchanged) 1.11x No WHERE 9.1ms (unchanged) 1.47x String WHERE is now at parity with the raw Harbour-style RDD scan. Compared to session start (119ms), that's a 19x speedup. Validation: - FiveSql2 43/43 - Harbour compat 51/51 - go test ./... ALL PASS Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -498,6 +498,25 @@ func (g *generator) emitCall(e *ast.CallExpr) {
|
||||
}
|
||||
}
|
||||
}
|
||||
// Peephole: AllTrim(FieldGet(<int literal>)) → PcOpFieldTrim <idx>.
|
||||
// Fuses the character-field CHAR-trim normalization that
|
||||
// SqlExprToPrg auto-wraps into one opcode, saving one Function
|
||||
// dispatch + one intermediate string allocation per row.
|
||||
if strings.EqualFold(ident.Name, "AllTrim") && len(e.Args) == 1 {
|
||||
if inner, ok := e.Args[0].(*ast.CallExpr); ok {
|
||||
if innerIdent, ok := inner.Func.(*ast.IdentExpr); ok &&
|
||||
strings.EqualFold(innerIdent.Name, "FieldGet") &&
|
||||
len(inner.Args) == 1 {
|
||||
if lit, ok := inner.Args[0].(*ast.LiteralExpr); ok && lit.Kind == token.INT {
|
||||
if n, err := strconv.Atoi(lit.Value); err == nil && n > 0 && n <= 0xFFFF {
|
||||
g.emit(hbrt.PcOpFieldTrim)
|
||||
g.emitU16(uint16(n))
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
g.emitString(hbrt.PcOpPushSymbol, strings.ToUpper(ident.Name))
|
||||
g.emit(hbrt.PcOpPushNil)
|
||||
for _, arg := range e.Args {
|
||||
|
||||
Reference in New Issue
Block a user