Skip to content
Tcl Source Code Changes To An experiment with small string optimization
Login

Changes To An experiment with small string optimization

Changes to "An experiment with small string optimization" between 2026-01-06 15:34:49 and 2026-01-08 06:23:53

︙
13
14
15
16
17
18
19













13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32







+
+
+
+
+
+
+
+
+
+
+
+
+
- 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.