.
D 2026-01-08T06:23:53.706
L An\sexperiment\swith\ssmall\sstring\soptimization
N text/x-markdown
P e67771d8c835e465f6c85daf99ee5b2d5a015a804c42a8532c601053de4d307a
U chw
W 3781
This page describes my attempt to implement small string optimization for Tcl. I most likely will not pursue this further for reasons stated below. Just recording for posterity in case someone has similar thoughts in the future where my experience might have some use. The implementation is in the [apn-ss-wip](https://core.tcl-lang.org/tcl/timeline?r=apn-ss-wip) branch.
The basic idea is simple enough. Instead of allocating separate storage for storing the short strings (14 chars or less on 64-bit platforms), they can be stored in the `internalRep` field within the `Tcl_Obj` itself. Saving the extra allocation speeds up operations like `string index` and (small) `string range` by more than 20%. That was significant enough to explore it further.
At the end, I concluded it was not worth pursuing for two reasons - limited applicability and significant test effort.
The limited applicability comes from multiple factors:
- only applies to small strings (obviously!). Nevertheless, small strings are common enough that it could be of benefit and in fact it made a measurable difference in compilation of scripts so it might still have been worthwhile were it not for the issues below.
- Many operations, not even necessarily involving modification, requires allocation for the `bytes` field anyways. For example, some conceptually read-only operations shimmer to Unicode representation which is stored in `internalRep` which means the short string representation has to be allocated separately. So in many cases, short string optimization only postpones the allocation.
- Many "types" shimmer to their internal representation by parsing the string representation. In such cases, the shimmering code has to be aware that the string representation may occupy the same storage area as the type's own representation and therefore allocate separate storage for the `bytes` field first. This again negates the value of the short string optimization, and further is error prone. The `apn-ss-wip` branch has modified all code paths to be aware of this (well, at least the entire test suite passes under valgrind) but it is still not good design to require programmers to be aware of this on a continuing basis.
- The biggest impediment, other than testing, was the treatment of arguments passed into command implementation as objc,objv pairs. Shimmering an argument requires generation of a string for the similar reason as above. This means `Tcl_Obj` arguments passed to commands will not benefit from the optimization which greatly limits their applicability.
The final straw was however the issue with testing. A very large number of tests use shorts string like "abc". Addition of short string objects would mean all these tests need to be duplicated to exercise the long string paths. Just too much work.
Quite possibly the issues above would have been obvious to others up front :-) I did anticipate them to some extent but was hoping for wider applicability of the performance benefits.
------
*On 2026-01-08 06:23:53 UTC chw added:*
What about a slightly different but in consequence equivalent approach?
Make the allocation of a Tcl_Obj somewhat larger to fit a cache line (64 bytes in most cases, instead of 48 (64 bit arch) or 24 (32 bit arch) and use the additional space for small strings.
Ideally the allocation is constrained to align proper on 64 byte boundaries (or whatever the cache line size is).
The only thing to consider (and to test) then is when to use that additional space, and when (not) to free the (not) allocated string buffer. Which might keep the required code changes rather minimal.
32 bit architectures would benefit more, since small strings can use 40 extra bytes in contrast to 16 for 64 architectures.
Z 5ee203ef0c484e07712bcb635bf9efd5