[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

`aset` on strings, changing the size in bytes

From: Stefan Monnier
Subject: `aset` on strings, changing the size in bytes
Date: Fri, 07 Sep 2018 15:52:57 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

[ As some of you may know, I like my strings to be immutable.
  But having tried it, my conclusion is that making Elisp strings
  immutable doesn't bring significant benefits because, while strings are
  rarely modified in-place w.r.t their sequence of characters, they are
  often modified in terms of the text-properties (although the
  `propertize` function has reduced the occurrence of such modifications
  to some extent).  ]

One of the ugliest part of string mutation is that the `aset` operation
on a string can take time proportional to the size of the string instead
of being a constant-time operation.

There are two causes:
- conversion between char-positions and byte-positions may need to scan
  the string (for strings which contain non-ASCII chars).
- the `aset` operation may change the size of the strings in bytes, so
  it may require allocating a whole new chunk of memory, copying the old
  string's bytes there, placing the new char at its proper position.

This second cause is rather hypothetical: it occurs very very rarely.
But it has far reaching consequences in the implementation of strings,
making it necessary to be able to relocate a string's bytes and hence
requiring an additional indirection.

Currently, this indirection comes "for free" since we use that same
indirection to let the GC compact the set of string-data-bytes objects
to try and reduce memory fragmentation.  But I think we should not have
our high-level API impose such an indirection at the lower level,
especially since this (mis)feature is virtually never used.

So here's my request: could we declare that we deprecate the use `aset`
on strings when it causes the string's length in bytes to change?  In my
experience, all the code I found which could trigger this behavior was
easily changed without loss of efficiency (e.g. by asking
subst-char-in-string not to work in-place, or by using a vector instead
of a string and converting the vector into a string once all the
modifications are done, ...).

This means, it's still perfectly OK to use `aset` to replace an ASCII
char with another ASCII char, and to use `aset` on any unibyte string.

Of course, such a backward incompatible change would need to be
introduced gradually, especially since it's virtually impossible to find
offending chunks of code other than by runtime testing.  First we'd
declare the practice deprecated; then we'd start emitting warnings when
it happens, conditional on a flag that's disabled by default; then we'd
change the default of the flag.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]