<i>TEBC: two small fast paths (inline eval-stack alloc; skip empty-string check for numeric compares)</i>.
by
.
D 2026-11-25T06:35:18.801
J assignee nobody
J closer nobody
J cmimetype text/x-markdown
J comment Makes\snumeric\scomparisons\sabout\s20-30%\sfaster\sand\sproc/method\scalls\sabout\s7-10%\sfaster\s(a\srecursive\s`fib\s20`\sruns\sabout\s7%\sfaster),\swith\sno\sbehaviour\schange\sand\sthe\sfull\stest\ssuite\spassing.\r\n\r\nAgainst\strunk\s[2f6d1b8032].\r\n\r\n###\sSummary\r\n\r\nProfiling\sproc\scalls\sand\ssimple\sloops\son\strunk\swith\s`perf`\sshowed\stwo\scheap\r\nhot\sspots\sin\s`generic/tclExecute.c`.\sThis\spatch\sfixes\sboth\swithout\schanging\r\nbehaviour.\sEach\sfix\sis\sattached\sas\sa\sseparate\spatch\sso\sthey\scan\sbe\sreviewed\r\nand\scommitted\sseparately.\r\n\r\n**Fix\s1:\sinline\sthe\sfast\spath\sof\s`GrowEvaluationStack`.**\sEvery\sproc\scall\sdoes\r\nthree\sevaluation-stack\sallocations:\sthe\scall\sframe\s(`TclPushStackFrame`),\sthe\r\ncompiled\slocals\s(`InitArgsAndLocals`)\sand\sthe\sbytecode's\soperand\sstack\r\n(`TclNRExecuteByteCode`).\sEach\sone\sis\san\sout-of-line\scall\sto\r\n`GrowEvaluationStack`,\seven\swhen\sthere\sis\sroom\sand\sthe\sallocation\sis\sjust\sa\r\npointer\sbump\splus\sa\smarker.\sIn\s`perf`,\s`GrowEvaluationStack`\splus\r\n`TclStackAlloc`\stake\sabout\s8%\sof\sthe\sself\stime\sof\san\sempty\sproc\scall.\r\n\r\nThe\spatch\ssplits\sthe\sfunction\sin\stwo:\r\n-\sa\s`static\sinline`\swrapper\sholding\sonly\sthe\s"room\savailable,\sno\smove"\sbranch,\r\n\s\swhich\sis\sthe\ssame\slogic\sas\sthe\sexisting\s`#ifndef\sPURIFY`\sbranch;\r\n-\sthe\sexisting\sfunction,\srenamed\s`GrowEvaluationStackSlow`,\swhich\sis\sotherwise\r\n\s\sunchanged.\r\n\r\nPURIFY\sbuilds\sbehave\sexactly\sas\sbefore.\r\n\r\n**Fix\s2:\sskip\s`TclCheckEmptyString()`\sfor\soperands\sthat\sare\salready\snumbers.**\r\n`INST_EQ/NEQ/LT/GT/LE/GE`\scall\s`TclCheckEmptyString()`\son\sboth\soperands\sbefore\r\ntrying\sa\snumeric\scomparison.\sFor\san\soperand\swith\san\sint\sor\sdouble\sinternal\r\nrep,\sthat\sfunction\schecks\sthe\sbytearray,\slist\sand\sdict\scases\sbefore\sit\sreturns\r\n"unknown".\sSo\severy\s`$i\s<\s$n`\smakes\stwo\sout-of-line\scalls\sthat\scannot\ssucceed.\r\nA\svalue\swith\san\sint\sor\sdouble\sinternal\srep\scan\snever\sbe\sthe\sempty\sstring,\sso\r\nthe\spatch\stests\sfor\sthat\srep\sfirst\s(new\s`IsNumericRep()`\smacro).\sThe\sempty\sand\r\nnon-numeric\scases\sstill\stake\sthe\sexact\ssame\spath\sas\sbefore.\r\n\r\n###\sResults\r\n\r\nBuilt\swith\sthe\sdefault\s`../unix/configure\s&&\smake`\s(gcc\s16.2.1,\sx86_64\sLinux).\r\n`bench.tcl`\sand\s`compare.sh`\sare\sattached.\sInstruction\scounts\scome\sfrom\r\n`perf\sstat\s-e\sinstructions:u`.\sEach\scase\sis\srun\sat\sN\sand\s2N\siterations\sand\sthe\r\ndifference\sis\sused,\sso\sstartup\scost\scancels\sout.\sThese\scounts\sare\r\ndeterministic.\sWall-clock\sis\sthe\sbest\sof\s5\sinterleaved\srounds\son\sa\sdesktop\sthat\r\nwas\snot\sidle,\sso\streat\sit\sas\sindicative.\r\n\r\nEvery\scase\sruns\sinside\sa\s`for\s{...}\s{$i\s<\s$n}\s{incr\si}`\sloop,\sso\sfix\s2\salso\r\nshows\sup\sin\sthe\s`call_*`\srows\sof\sthe\scombined\sresults.\sThe\sper-fix\stables\r\nseparate\sthe\stwo\seffects.\r\n\r\n**Both\sfixes,\suser-space\sinstructions\sper\siteration:**\r\n```\r\ncase\s\s\s\s\s\s\s\s\s\s\s\s\sbaseline\s\s\s\spatched\s\s\schange\r\ncmp_int_lt\s\s\s\s\s\s\s\s\s\s\s\s328\s\s\s\s\s\s\s\s251\s\s\s-23.5%\r\ncmp_int_eq\s\s\s\s\s\s\s\s\s\s\s\s602\s\s\s\s\s\s\s\s450\s\s\s-25.3%\r\ncmp_double_lt\s\s\s\s\s\s\s\s\s688\s\s\s\s\s\s\s\s544\s\s\s-20.9%\r\ncall_p0\s\s\s\s\s\s\s\s\s\s\s\s\s\s1844\s\s\s\s\s\s\s1670\s\s\s\s-9.4%\r\ncall_p2\s\s\s\s\s\s\s\s\s\s\s\s\s\s2183\s\s\s\s\s\s\s2009\s\s\s\s-8.0%\r\ncall_plocals\s\s\s\s\s\s\s\s\s2707\s\s\s\s\s\s\s2533\s\s\s\s-6.4%\r\ncall_oo_m0\s\s\s\s\s\s\s\s\s\s\s2603\s\s\s\s\s\s\s2359\s\s\s\s-9.4%\r\ncall_oo_m2\s\s\s\s\s\s\s\s\s\s\s2941\s\s\s\s\s\s\s2697\s\s\s\s-8.3%\r\ncall_fib20\s\s\s\s\s\s\s96586710\s\s\s89053028\s\s\s\s-7.8%\r\nstr_lt\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s994\s\s\s\s\s\s\s\s929\s\s\s\s-6.5%\r\nstr_empty_eq\s\s\s\s\s\s\s\s\s\s516\s\s\s\s\s\s\s\s438\s\s\s-15.1%\r\n```\r\n\r\n**Both\sfixes,\swall\sclock\s(ns/iteration):**\r\n```\r\ncase\s\s\s\s\s\s\s\s\s\s\s\s\sbaseline\s\s\s\spatched\s\s\schange\r\ncmp_int_lt\s\s\s\s\s\s\s\s\s\s35.76\s\s\s\s\s\s24.86\s\s\s-30.5%\r\ncmp_int_eq\s\s\s\s\s\s\s\s\s\s67.03\s\s\s\s\s\s48.37\s\s\s-27.8%\r\ncmp_double_lt\s\s\s\s\s\s\s78.43\s\s\s\s\s\s60.89\s\s\s-22.4%\r\ncall_p0\s\s\s\s\s\s\s\s\s\s\s\s205.97\s\s\s\s\s186.28\s\s\s\s-9.6%\r\ncall_p2\s\s\s\s\s\s\s\s\s\s\s\s244.09\s\s\s\s\s225.70\s\s\s\s-7.5%\r\ncall_plocals\s\s\s\s\s\s\s319.08\s\s\s\s\s297.54\s\s\s\s-6.8%\r\ncall_oo_m0\s\s\s\s\s\s\s\s\s300.54\s\s\s\s\s272.40\s\s\s\s-9.4%\r\ncall_oo_m2\s\s\s\s\s\s\s\s\s341.56\s\s\s\s\s317.22\s\s\s\s-7.1%\r\ncall_fib20\s\s\s\s\s5764000.00\s5355540.00\s\s\s\s-7.1%\r\nstr_lt\s\s\s\s\s\s\s\s\s\s\s\s\s107.31\s\s\s\s\s\s96.19\s\s\s-10.4%\r\nstr_empty_eq\s\s\s\s\s\s\s\s57.88\s\s\s\s\s\s49.48\s\s\s-14.5%\r\n```\r\n\r\n**Fix\s1\sonly\s(instructions):**\sabout\s96\sfewer\sinstructions\sper\sproc\scall,\si.e.\r\nthree\savoided\scalls.\sPure\scomparison\sloops\sare\sunchanged.\r\n```\r\ncase\s\s\s\s\s\s\s\s\s\s\s\s\sbaseline\s\s\s\spatched\s\s\schange\r\ncmp_int_lt\s\s\s\s\s\s\s\s\s\s\s\s328\s\s\s\s\s\s\s\s328\s\s\s\s-0.0%\r\ncmp_int_eq\s\s\s\s\s\s\s\s\s\s\s\s602\s\s\s\s\s\s\s\s602\s\s\s\s+0.0%\r\ncmp_double_lt\s\s\s\s\s\s\s\s\s688\s\s\s\s\s\s\s\s688\s\s\s\s-0.0%\r\ncall_p0\s\s\s\s\s\s\s\s\s\s\s\s\s\s1844\s\s\s\s\s\s\s1748\s\s\s\s-5.2%\r\ncall_p2\s\s\s\s\s\s\s\s\s\s\s\s\s\s2183\s\s\s\s\s\s\s2087\s\s\s\s-4.4%\r\ncall_plocals\s\s\s\s\s\s\s\s\s2707\s\s\s\s\s\s\s2611\s\s\s\s-3.5%\r\ncall_oo_m0\s\s\s\s\s\s\s\s\s\s\s2603\s\s\s\s\s\s\s2437\s\s\s\s-6.4%\r\ncall_oo_m2\s\s\s\s\s\s\s\s\s\s\s2941\s\s\s\s\s\s\s2775\s\s\s\s-5.6%\r\ncall_fib20\s\s\s\s\s\s\s96581875\s\s\s92380729\s\s\s\s-4.3%\r\nstr_lt\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s994\s\s\s\s\s\s\s\s994\s\s\s\s+0.0%\r\nstr_empty_eq\s\s\s\s\s\s\s\s\s\s516\s\s\s\s\s\s\s\s516\s\s\s\s-0.0%\r\n```\r\n\r\n**Fix\s2\sonly\s(instructions):**\sabout\s77\sfewer\sinstructions\sper\snumeric\r\ncomparison.\sProc-call\scases\simprove\sonly\sthrough\sthe\sloop's\s`$i\s<\s$n`.\r\n```\r\ncase\s\s\s\s\s\s\s\s\s\s\s\s\sbaseline\s\s\s\spatched\s\s\schange\r\ncmp_int_lt\s\s\s\s\s\s\s\s\s\s\s\s328\s\s\s\s\s\s\s\s251\s\s\s-23.5%\r\ncmp_int_eq\s\s\s\s\s\s\s\s\s\s\s\s603\s\s\s\s\s\s\s\s450\s\s\s-25.3%\r\ncmp_double_lt\s\s\s\s\s\s\s\s\s688\s\s\s\s\s\s\s\s543\s\s\s-21.0%\r\ncall_p0\s\s\s\s\s\s\s\s\s\s\s\s\s\s1844\s\s\s\s\s\s\s1766\s\s\s\s-4.2%\r\ncall_p2\s\s\s\s\s\s\s\s\s\s\s\s\s\s2183\s\s\s\s\s\s\s2105\s\s\s\s-3.6%\r\ncall_plocals\s\s\s\s\s\s\s\s\s2707\s\s\s\s\s\s\s2629\s\s\s\s-2.9%\r\ncall_oo_m0\s\s\s\s\s\s\s\s\s\s\s2603\s\s\s\s\s\s\s2525\s\s\s\s-3.0%\r\ncall_oo_m2\s\s\s\s\s\s\s\s\s\s\s2941\s\s\s\s\s\s\s2863\s\s\s\s-2.7%\r\ncall_fib20\s\s\s\s\s\s\s96584690\s\s\s93255862\s\s\s\s-3.4%\r\nstr_lt\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s994\s\s\s\s\s\s\s\s929\s\s\s\s-6.5%\r\nstr_empty_eq\s\s\s\s\s\s\s\s\s\s516\s\s\s\s\s\s\s\s438\s\s\s-15.2%\r\n```\r\n\r\nWall-clock\stables\sfor\seach\sfix\son\sits\sown\sare\sin\s`bench-fix1.txt`\sand\r\n`bench-fix2.txt`.\sIn\sthe\sfix\s1\srun,\sa\sfew\srows\s(`cmp_double_lt`,\r\n`str_empty_eq`)\smoved\sby\sup\sto\s15%\sin\seither\sdirection\seven\sthough\stheir\r\ninstruction\scounts\sare\sidentical.\sThat\sis\smachine\snoise\sor\scode-layout\r\neffects,\snot\sa\sregression\sfrom\sthe\spatch.\r\n\r\n###\sTesting\r\n\r\n`make\stest`\son\strunk\s2f6d1b8032,\sbefore\sand\safter\sthe\spatch:\r\n\r\n```\r\nbaseline:\sall.tcl:\sTotal\s526669\s\sPassed\s520381\s\sSkipped\s6288\s\sFailed\s0\r\npatched:\s\sall.tcl:\sTotal\s526669\s\sPassed\s520381\s\sSkipped\s6288\s\sFailed\s0\r\n```\r\nNo\snew\scompiler\swarnings\swith\sthe\sdefault\s`-Wall\s-Wextra`\sflags.\r\n\r\n`bench.tcl`\salso\sasserts\sa\sset\sof\scomparison\sresults\swhose\sbehaviour\smust\snot\r\nchange:\s`""\s<\s1`,\s`[list]\s==\s0`,\s`[dict\screate]\s<\s1`,\s`"abc"\s<\s1`,\sNaN,\smixed\r\nint/double,\sand\sothers.\r\n\r\n###\sFiles\r\n\r\nThese\swill\sbe\sattached\sto\sthis\sticket:\r\n\r\n-\s`fix1-stackalloc.patch`:\sfix\s1\sonly\s(`fossil\sdiff`\sagainst\strunk\s2f6d1b8032)\r\n-\s`fix2-numcompare.patch`:\sfix\s2\sonly\s(same\sbase)\r\n-\s`tebc-fastpaths.fossil.diff`:\sboth\sfixes\scombined\r\n-\s`bench.tcl`,\s`compare.sh`:\sbenchmark\sand\sA/B\sdriver\r\n\s\s(`./compare.sh\s<baseline-build-dir>\s<patched-build-dir>`)
J foundin 9.1.1\s(trunk\s2f6d1b8032)
J is_private 0
J login anonymous
J priority 5\sMedium
J resolution None
J severity Minor
J status Open
J submitter anonymous
J subsystem 47.\sBytecode\sCompiler
J title TEBC:\stwo\ssmall\sfast\spaths\s(inline\seval-stack\salloc;\sskip\sempty-string\scheck\sfor\snumeric\scompares)
J type Patch
K 2a914bf99e684c882c2252d650bef83160844f5f
U anonymous
Z 746be54634c4bebb24fdf4c5daed67ce