|
2026-11-25
| ||
| 06:46 | • Ticket [2a914bf99e] TEBC: two small fast paths (inline eval-stack alloc; skip em... artifact: 8a53aa6911 user: pounceandmiss | |
| 06:38 | • Add attachment compare.sh to ticket [2a914bf99e] artifact: a97188fb7d user: anonymous | |
| 06:38 | • Add attachment bench.tcl to ticket [2a914bf99e] artifact: 8057874b3b user: anonymous | |
| 06:38 | • Add attachment tebc-fastpaths.fossil.diff to ticket [2a914bf99e684c88|2... artifact: af74c79364 user: anonymous | |
| 06:37 | • Add attachment fix2-numcompare.patch to ticket [2a914bf99e684c88|2a914b... artifact: 97c08f3e99 user: anonymous | |
| 06:37 | • Add attachment fix1-stackalloc.patch to ticket [2a914bf99e684c88|2a914b... artifact: 4210e0148f user: anonymous | |
| 06:35 | • New ticket [2a914bf99e] TEBC: two small fast paths (inline eval-stack alloc; ski... artifact: 8c37262204 user: anonymous | |
TEBC: two small fast paths (inline eval-stack alloc; skip empty-string check for numeric compares)
Makes numeric comparisons about 20-30% faster and proc/method calls about 7-10% faster (a recursive fib 20 runs about 7% faster), with no behaviour change and the full test suite passing.
Against trunk [2f6d1b8032].
Summary
Profiling proc calls and simple loops on trunk with perf showed two cheap
hot spots in generic/tclExecute.c. This patch fixes both without changing
behaviour. Each fix is attached as a separate patch so they can be reviewed
and committed separately.
Fix 1: inline the fast path of GrowEvaluationStack. Every proc call does
three evaluation-stack allocations: the call frame (TclPushStackFrame), the
compiled locals (InitArgsAndLocals) and the bytecode's operand stack
(TclNRExecuteByteCode). Each one is an out-of-line call to
GrowEvaluationStack, even when there is room and the allocation is just a
pointer bump plus a marker. In perf, GrowEvaluationStack plus
TclStackAlloc take about 8% of the self time of an empty proc call.
The patch splits the function in two:
- a
static inlinewrapper holding only the "room available, no move" branch, which is the same logic as the existing#ifndef PURIFYbranch; - the existing function, renamed
GrowEvaluationStackSlow, which is otherwise unchanged.
PURIFY builds behave exactly as before.
Fix 2: skip TclCheckEmptyString() for operands that are already numbers.
INST_EQ/NEQ/LT/GT/LE/GE call TclCheckEmptyString() on both operands before
trying a numeric comparison. For an operand with an int or double internal
rep, that function checks the bytearray, list and dict cases before it returns
"unknown". So every $i < $n makes two out-of-line calls that cannot succeed.
A value with an int or double internal rep can never be the empty string, so
the patch tests for that rep first (new IsNumericRep() macro). The empty and
non-numeric cases still take the exact same path as before.
Results
Built with the default ../unix/configure && make (gcc 16.2.1, x86_64 Linux).
bench.tcl and compare.sh are attached. Instruction counts come from
perf stat -e instructions:u. Each case is run at N and 2N iterations and the
difference is used, so startup cost cancels out. These counts are
deterministic. Wall-clock is the best of 5 interleaved rounds on a desktop that
was not idle, so treat it as indicative.
Every case runs inside a for {...} {$i < $n} {incr i} loop, so fix 2 also
shows up in the call_* rows of the combined results. The per-fix tables
separate the two effects.
Both fixes, user-space instructions per iteration:
case baseline patched change
cmp_int_lt 328 251 -23.5%
cmp_int_eq 602 450 -25.3%
cmp_double_lt 688 544 -20.9%
call_p0 1844 1670 -9.4%
call_p2 2183 2009 -8.0%
call_plocals 2707 2533 -6.4%
call_oo_m0 2603 2359 -9.4%
call_oo_m2 2941 2697 -8.3%
call_fib20 96586710 89053028 -7.8%
str_lt 994 929 -6.5%
str_empty_eq 516 438 -15.1%
Both fixes, wall clock (ns/iteration):
case baseline patched change
cmp_int_lt 35.76 24.86 -30.5%
cmp_int_eq 67.03 48.37 -27.8%
cmp_double_lt 78.43 60.89 -22.4%
call_p0 205.97 186.28 -9.6%
call_p2 244.09 225.70 -7.5%
call_plocals 319.08 297.54 -6.8%
call_oo_m0 300.54 272.40 -9.4%
call_oo_m2 341.56 317.22 -7.1%
call_fib20 5764000.00 5355540.00 -7.1%
str_lt 107.31 96.19 -10.4%
str_empty_eq 57.88 49.48 -14.5%
Fix 1 only (instructions): about 96 fewer instructions per proc call, i.e. three avoided calls. Pure comparison loops are unchanged.
case baseline patched change
cmp_int_lt 328 328 -0.0%
cmp_int_eq 602 602 +0.0%
cmp_double_lt 688 688 -0.0%
call_p0 1844 1748 -5.2%
call_p2 2183 2087 -4.4%
call_plocals 2707 2611 -3.5%
call_oo_m0 2603 2437 -6.4%
call_oo_m2 2941 2775 -5.6%
call_fib20 96581875 92380729 -4.3%
str_lt 994 994 +0.0%
str_empty_eq 516 516 -0.0%
Fix 2 only (instructions): about 77 fewer instructions per numeric
comparison. Proc-call cases improve only through the loop's $i < $n.
case baseline patched change
cmp_int_lt 328 251 -23.5%
cmp_int_eq 603 450 -25.3%
cmp_double_lt 688 543 -21.0%
call_p0 1844 1766 -4.2%
call_p2 2183 2105 -3.6%
call_plocals 2707 2629 -2.9%
call_oo_m0 2603 2525 -3.0%
call_oo_m2 2941 2863 -2.7%
call_fib20 96584690 93255862 -3.4%
str_lt 994 929 -6.5%
str_empty_eq 516 438 -15.2%
Wall-clock tables for each fix on its own are in bench-fix1.txt and
bench-fix2.txt. In the fix 1 run, a few rows (cmp_double_lt,
str_empty_eq) moved by up to 15% in either direction even though their
instruction counts are identical. That is machine noise or code-layout
effects, not a regression from the patch.
Testing
make test on trunk 2f6d1b8032, before and after the patch:
baseline: all.tcl: Total 526669 Passed 520381 Skipped 6288 Failed 0
patched: all.tcl: Total 526669 Passed 520381 Skipped 6288 Failed 0
No new compiler warnings with the default -Wall -Wextra flags.
bench.tcl also asserts a set of comparison results whose behaviour must not
change: "" < 1, [list] == 0, [dict create] < 1, "abc" < 1, NaN, mixed
int/double, and others.
Files
These will be attached to this ticket:
fix1-stackalloc.patch: fix 1 only (fossil diffagainst trunk 2f6d1b8032)fix2-numcompare.patch: fix 2 only (same base)tebc-fastpaths.fossil.diff: both fixes combinedbench.tcl,compare.sh: benchmark and A/B driver (./compare.sh <baseline-build-dir> <patched-build-dir>)
I'm the submitter - filed this as anonymous by accident. Happy to answer questions or rework the patches.