help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: Why does using aset sometimes output raw bytes?


From: Stefan Monnier
Subject: Re: Why does using aset sometimes output raw bytes?
Date: Sun, 09 Dec 2018 14:20:07 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

> I don't have a use case where using aset like this is indispensable, I
> was just experimenting.  Are your reservations because the
> implementation of aset is brittle, leading to things like the
> observations I reported -- maybe too hard to fix and not worth the
> trouble?

It's not the implementation, but the semantics of unibyte/multibyte
strings presumes that the difference doesn't matter much for ASCII-only
strings, which is mostly true but isn't true in the case of `aset`.

Also you probably expect `aset` to be constant-time, but on multibyte
strings it can take time O(N) where N is the length of the string:
Emacs's multibyte strings are designed for sequential access rather than
random access, and since chars can take a variable amount of space,
replacing one with another can require shifting things around and
allocating a new chunk of memory.

> Or are there other reasons not to use aset as above?

In most cases `aset` results in more complex and more brittle code when
working on strings.  It's not always the case and the code without
`aset` occasionally is a lot worse, admittedly, but as a first rule,
I strongly recommend to stay away with it.

You'll also gain karma points along the way,


        Stefan




reply via email to

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