Files
five/_FiveSql2/test/bench_prep_sql.prg
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

107 lines
2.7 KiB
Plaintext

// Prepared-statement vs concatenated-SQL benchmark.
// Demonstrates the plan cache win for parameterized queries — the same
// `?` template hits cache on every call after the first; concatenated
// SQL strings vary by value and miss every time.
#include "FiveSqlDef.ch"
#define ITERS 1000
PROCEDURE Main()
LOCAL t0, t1, i
ErrorBlock( {|e| QOut( "TRAP: " + e:description + " " + e:operation ), Break(e) } )
? "================================================================"
? " FiveSql2 Prepared-Statement Benchmark"
? " " + hb_ntos( ITERS ) + " iterations per pattern"
? "================================================================"
?
SetupTable()
/* A: concatenated INSERT — SQL text changes per iteration, every
* call misses the plan cache and re-parses. */
t0 := hb_MilliSeconds()
FOR i := 1 TO ITERS
five_SQL( "INSERT INTO bench_prep (id, val) VALUES (" + hb_ntos( i ) + ", 'a')" )
NEXT
t1 := hb_MilliSeconds()
R( "CONCAT_INSERT", t1 - t0 )
TruncateTable()
/* B: prepared INSERT — same SQL text every iteration, cache hits
* from the 2nd call onward. */
t0 := hb_MilliSeconds()
FOR i := 1 TO ITERS
five_SQL( "INSERT INTO bench_prep (id, val) VALUES (?, ?)", { i, "a" } )
NEXT
t1 := hb_MilliSeconds()
R( "PREPARED_INSERT", t1 - t0 )
TruncateTable()
/* C: concatenated SELECT by id. */
t0 := hb_MilliSeconds()
FOR i := 1 TO ITERS
five_SQL( "SELECT val FROM bench_prep WHERE id = " + hb_ntos( i ) )
NEXT
t1 := hb_MilliSeconds()
R( "CONCAT_SELECT", t1 - t0 )
/* D: prepared SELECT by id. */
t0 := hb_MilliSeconds()
FOR i := 1 TO ITERS
five_SQL( "SELECT val FROM bench_prep WHERE id = ?", { i } )
NEXT
t1 := hb_MilliSeconds()
R( "PREPARED_SELECT", t1 - t0 )
CleanupTable()
?
? "================================================================"
RETURN
STATIC PROCEDURE SetupTable()
IF hb_FileExists( "bench_prep.dbf" )
FErase( "bench_prep.dbf" )
ENDIF
dbCreate( "bench_prep.dbf", { ;
{ "ID", "N", 10, 0 }, ;
{ "VAL", "C", 10, 0 } ;
} )
/* Pre-populate enough rows so SELECT benchmark has real data. */
USE bench_prep.dbf NEW EXCLUSIVE
LOCAL i
FOR i := 1 TO ITERS
dbAppend()
FieldPut( 1, i )
FieldPut( 2, "a" )
NEXT
dbCommit()
CLOSE bench_prep
RETURN
STATIC PROCEDURE TruncateTable()
USE bench_prep.dbf NEW EXCLUSIVE
dbZap()
CLOSE bench_prep
RETURN
STATIC PROCEDURE CleanupTable()
dbCloseAll()
FErase( "bench_prep.dbf" )
RETURN
STATIC FUNCTION R( cLabel, nMs )
? " ", PadR( cLabel, 18 ) + Str( nMs, 7 ) + " ms " + ;
Str( nMs * 1000 / ITERS, 8, 2 ) + " us/query"
RETURN NIL