From f27c96c7f04a53756221e99af37e530d0ff081e6 Mon Sep 17 00:00:00 2001 From: CharlesKWON Date: Sat, 18 Apr 2026 13:20:46 +0900 Subject: [PATCH] =?UTF-8?q?perf(fivesql2):=20extend=20WA=20cache=20to=20SE?= =?UTF-8?q?LECT=20path=20=E2=80=94=202x=20faster=20single-table?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TSqlExecutor:OpenTable now hands lifetime to the WA cache for stable aliases (user-supplied or table-named). CloseOpened skips those entries, so the DBF mmap stays alive across queries instead of being unmapped + re-opened 1000 times a bench. Previously the WA cache only covered DML (INSERT/UPDATE/DELETE) — SELECT was still paying the full dbUseArea/dbCloseArea syscall bill every query (profile showed rtlDbCloseArea + munmap at ~30% of total CPU). AcquireTemp-generated aliases (FA_####) are excluded — they change every query (self-joins, nested depth), so caching them would just leak entries for no reuse. JOIN / recursive CTE regressions from an earlier unrestricted version are gone. Bench deltas vs prior HEAD (median of 3 steady runs, 1000 iters): B1_SELECT_STAR 82 → 41 us -50% (2.0x) B2_WHERE_FILTER 78 → 35 us -55% (2.2x) B3_ORDER_BY 90 → 48 us -47% (1.88x) B5_DISTINCT 75 → 32 us -57% (2.34x) B7_CTE_SIMPLE 120 → 77 us -36% (1.56x) B9_ROW_NUMBER 239 → 194 us -19% B10_RANK_PART 276 → 233 us -16% B11_SUM_OVER 296 → 252 us -15% B4_GROUP_HAVING 498 → 450 us -10% Others flat (JOIN / recursive CTE / DML already covered). FiveSql2 43/43, Harbour compat 56/56. Co-Authored-By: Claude Opus 4.7 (1M context) --- _FiveSql2/src/TSqlExecutor.prg | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/_FiveSql2/src/TSqlExecutor.prg b/_FiveSql2/src/TSqlExecutor.prg index a164be6..0431f7d 100644 --- a/_FiveSql2/src/TSqlExecutor.prg +++ b/_FiveSql2/src/TSqlExecutor.prg @@ -202,7 +202,18 @@ METHOD OpenTable( cTable, cAlias ) CLASS TSqlExecutor nWA := ::oIndex:OpenTable( cTable, cAlias, .T., .T. ) IF nWA > 0 - AAdd( ::aOpened, cAlias ) + /* When the WA cache is on, hand lifetime to the cache so CloseOpened + * leaves the mmap alive for the next query. Profile showed + * rtlDbCloseArea + munmap at ~30% of SELECT CPU prior to this branch. + * + * Skip caching when the alias is an AcquireTemp-generated "FA_####" + * token: those change every query (self-joins, nested depth), so + * caching them just leaks entries while delivering zero reuse. */ + IF SqlWACacheIsEnabled() .AND. ! ( Left( cAlias, 3 ) == "FA_" ) + SqlWACachePut( cAlias, nWA ) + ELSE + AAdd( ::aOpened, cAlias ) + ENDIF /* Register with alias manager if not already tracked */ lFound := .F. FOR i := 1 TO Len( ::oAlias:aSlots )