Version 6 of Interning a String

Updated 2006-09-21 00:12:05 by MS

>Strick> In some languages ( e.g. [L1 ] ) you can "intern" a string, which gives you one unique "interned instance" of the string with that value.

Here's a proc to do it in Tcl:

 proc intern s {
        if { ! [info exists ::INTERN($s)] } { set ::INTERN($s) $s }
        set ::INTERN($s)
 }

I used Tcl a dozen years before needing this, but today i wrote a program that used many of the same long strings (file pathnames) in some Tcl lists.

There's two different reasons you might want to intern a string:

  • To make string comparisons as fast as pointer comparisons
  • To save memory, when strings with the same value are used many times

Most often you intern strings for the first reason, but in Tcl that can't work: there are no separate "value" and "address" comparisons. Today I interned for the second reason.

MS remarks that Tcl has a shared pool of literal strings. Any code within proc bodies reuses literals from the shared pool. In a sense, all literal strings are already "interned", and this mechanism is mainly useful for strings that are generated at runtime.