Skip to content
Tcl Source Code Update of ”An experiment with small string optimization”
Login

Update of ”An experiment with small string optimization”

Overview

Artifact ID: e67771d8c835e465f6c85daf99ee5b2d5a015a804c42a8532c601053de4d307a
Page Name:An experiment with small string optimization
Date: 2026-01-06 15:34:49
Original User: apnadkarni
Mimetype:text/x-markdown
Next 7c4db68fa3890493496e233aaeaa56a5390a974d73084bd4462fefae339b62ff
Content

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 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:

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.