Mutation

Scott L. Burson gyro@zeta-soft.com
Wed, 14 May 1997 10:13:15 -0700 (PDT)


   From: hbaker@netcom.com (Henry G. Baker)
   Date: Wed, 14 May 1997 07:11:29 -0700 (PDT)

   One of the most important kinds of functional arrays are functional
   character strings.  In particular, character strings used as 'pnames'
   ('print-names').  One of the biggest performance problems with CL is
   that some 'interrogation functions' -- i.e., 'symbol-name' (I think
   that that is the name of the function!) -- are forced to _always_ copy
   the string, because they can't guarantee that the requestor won't
   mutate it.  With an immutable character string, however, symbol-name
   can simply and efficiently return the actual character string, safe in
   the knowledge that it can't be hacked.

I kinda thought `symbol-name' would have to copy the string as you suggest,
but one day just a few weeks ago I typed the following at Allegro/Unix:

  (eq (symbol-name 'foo) (symbol-name 'foo))

and got back T.  "Odd", sez I, "what happens if I modify the string?"  Well,
to make a long story short, it eventually became clear that Allegro has some
way of detecting modifications of strings that have been returned from
`symbol-name', and what it does when it detects one is to make a new copy of
the symbol's name.  Thus:

  > (setq *str* (symbol-name 'foo))
  "FOO"
  > (setf (char *str* 0) #\G)
  #\G
  > *str*
  "GOO"
  > (symbol-name 'foo)
  "FOO"
  > (eq * *str*)
  NIL

I'm not arguing against the introduction of immutable things, but I thought
this was a cute hack.

-- Scott