Article View: comp.lang.tcl
Article #2Experiment: inline bytecode for [lrange] with variable indices (written by Claude)
From: et99
Date: Wed, 07 Oct 2026 01:19
Date: Wed, 07 Oct 2026 01:19
43 lines
5312 bytes
5312 bytes
Hi all:
A note first: in the last few months my Claude account (a Pro subscription, not the free tier) has gained the ability to build and run code in its own Linux sandbox. Before that, Claude could only hand me code and I had to do all the testing on my own machine. I don't know whether this is just my account or a general rollout.
This experiment started as a side effect of a tool Claude and I wrote to scan every proc in every package of a Magicsplat install, looking for bytecode problems. I extended it to count which commands are called through the command table (invokeStk) instead of being compiled inline. Most of what came up does real work per call (format, regsub, lsort, ...), so call overhead is a small part of the cost. The one cheap command that stood out was [lrange] with a non-literal index: [lrange $l 1 3] already compiles to listRangeImm, but [lrange $l $i $j] and [lrange $l 1 end-$n] fall back to a full command call.
I asked Claude (Anthropic's AI assistant), working in its sandbox with access to the GitHub mirror of the Tcl source, to add a new listRange instruction that always takes exactly three values from the stack: the list and both indices. It took about 20 minutes to get a complete patch with tests, after many build and test runs in the sandbox, including AddressSanitizer, Tcl's memory-debug build and the TCL_COMPILE_DEBUG build. I copied the changed files into my own tree, built, and ran the tests and a benchmark on two of my machines. Claude ran the same benchmark in its sandbox, which is the third column below.
Base: Tcl main, commit 78dedf7 (2026-10-05, GitHub mirror).
What changed (66 lines of C added, 16 removed; 161 lines of tests added):
generic/tclCompile.h +1 new opcode INST_LIST_RANGE
generic/tclCompile.c +7 instruction table entry
generic/tclExecute.c +34 the instruction
generic/tclCompCmdsGR.c +23 -16 compile non-literal indices to it
generic/tclAssembly.c +1 tcl::unsupported::assemble can use it
tests/lrange.test +85 lrange-6.1 .. 6.14
tests/compile.test +38 compile-22.1 .. 22.5
tests/assemble.test +38 assemble-7.47 .. 7.50
The instruction does what Tcl_LrangeObjCmd does, in the same order, so error messages and error precedence are the same as for the command. A test compares the compiled form against the command for 6 lists and 20 index forms each way, with no differences.
Tests: the full suite gives 509,285 tests (23 more than before), the same 8 failures before and after (httpProxy.test, which needs network access). disassemble and getbytecode show the new instruction.
Timing (ns per call, 10-element list, best of 7, before -> after):
machine 1 machine 2 Claude's sandbox
lrange $l $i $j 193 -> 126 117 -> 72 152 -> 98
lrange $l $i end 232 -> 165 130 -> 93 172 -> 123
lrange $l 1 end-$n 368 -> 291 200 -> 161 270 -> 212
lrange $l 1 3 (literal) 38 -> 39 24 -> 23 32 -> 34
The sandbox is a small shared VM, so its numbers are noisier than mine. The literal case compiles to identical bytecode in both builds, and its difference changes direction between machines, so I take it to be build-layout noise. The end-$n gain is smaller because the string "end-N" is still built and parsed on every call.
Limits: Linux only. I have not built it on Windows or macOS, so MSVC has not seen it. I have not tried an integer fast path for end-$n.
I am posting this as a data point more than as a proposal. If anyone on the core team thinks it is worth a ticket, I will file one with the patch. The patch, the test results and the benchmark script are here: https://github.com/rocketship88/lrange-inline .
On a side note, I have also found the same assistant good at drafting documentation for my own projects, especially when using the AI to build the projects. It needs a human to check the facts, but it makes a decent first draft. The Readme at github was also written by Claude.
Eric
Technical Details
Message-ID:
<11a46mp$2f14b$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailShow Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: et99 <et99@rocketship1.me> Newsgroups: comp.lang.tcl Subject: Experiment: inline bytecode for [lrange] with variable indices (written by Claude) Date: Tue, 6 Oct 2026 18:19:19 -0700 Organization: A noiseless patient Spider Lines: 43 Message-ID: <11a46mp$2f14b$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Wed, 07 Oct 2026 01:19:22 +0000 (UTC) Injection-Info: dont-email.me; logging-data="2589835"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+7SMiwmfJa5YMyZSkb1Gsu"; posting-host="c368b75c749776c669dd453f26deb3d8" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:DdBE8mIYzZUSu2ikO1BnVfCfhqA= sha256:/rWkKRyuRavDxbvpjZfh+FBjLhGN8AsqQ3tauwncG44= sha1:8c52V7f/oZsNEO/HEv5tDPpNUmk= sha256:vszdL9Qg+6FFZs3GNBDjCP1jnWYr363++Pa6bC3B4z8= Content-Language: en-US