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: Stephen Berman
Subject: Re: Why does using aset sometimes output raw bytes?
Date: Sun, 09 Dec 2018 21:43:50 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

On Sun, 09 Dec 2018 14:20:07 -0500 Stefan Monnier <monnier@iro.umontreal.ca> 
wrote:

>> 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`.

Yes, thanks; I also appreciate this better now after Eli's explanations.

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

Interesting.  I was in fact wondering about just such issues because of
code posted here that permutes strings using split-string and sort,
which prompted me to try some alternatives, one of which was to use a
while-loop instead of sort and another was using a loop and aset instead
of split-string.  I guess this is well-explored and I could probably do
a web search for the most efficient algorithm, but I really just wanted
to see what I could come up with in Emacs Lisp and so bumped into these
multibyte issues.  So it's already been a useful learning experience.

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

Thanks for the feedback.

Steve Berman



reply via email to

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