Subject: Re: bug#72802: 31.0.50; Crash in (equal sub-char-table-a sub-char-table-b)

From: Eli Zaretskii <[email-masked]>
Date: Sun, 25 Aug 2024 18:39:57 +0300

> Date: Sun, 25 Aug 2024 15:15:14 +0000
> From: Pip Cet <[email-masked]>
> Cc: [email-masked]
> 
> > Not even on master, I think, unless we see a bug related to it that is not
> > caused by a specially-concocted Lisp program or GDB command.
> 
> It's a clear bug, whether or not the Lisp programs that cause it are
> "specially-concocted" (what's that supposed to mean, anyway?  We can't
> just delay fixing what are clearly bugs until they pop up on random
> users' machines!)

We certainly can, and do.

Is there any code out there that compares char-tables and is affected
by this?

> So I think it's important to fix this soon (not "now") on the master
> branch.

I disagree, at least for now, sorry.  char-tables have been stable for
decades, so any change there should ideally be part of some
significant enhancement that justifies the potential destabilization.
Elimination of pure space or some similar change fits the bill.

> > If this is required for the no-pure-space branch, I think you should
> > install this on that branch.  Then it will be merged together with the
> > branch.
> 
> It's not, it just popped up during work on that branch.

Popped up how?

In any case, there's nothing wrong with fixing that as part of a much
larger changeset, which gives us significant gains.

> > I'm not sure we want to pay this cost.  What bothers me is mainly the
> > run-time cost of extracting integers from Lisp objects.
> 
> That might be noticeable on 32-bit machines with EMACS_WIDE_INT, I
> suppose, or on very old machines where memory isn't so much slower than
> register manipulation.
> 
> > char-table is
> > supposed to be very efficient, both memory-wise and CPU-wise, and I
> > think the performance here trumps simplicity.
> 
> How about using an "ordinary" pseudovector with its non-Lisp elements at
> the end?

Not even that, I think.  Some of the char-table are accessed in the
inner-most loops of the display code, and were at the time optimized
especially for that purpose.  I'm not interested in making changes
there for minor simplifications.

> >> BTW, I'm surprised this code returns nil; I think that should be
> >> documented.
> >>
> >> (setq a (make-char-table nil))
> >> (setq b (make-char-table nil))
> >> (aset a 1 nil)
> >> (dotimes (i (max-char))
> >>   (unless (equal (aref a i) (aref b i))
> >>     (error "i = %S" i)))
> >> (equal a b)
> >
> > Why are you surprised?  Setting a single cell of a char-table changes
> > its structure, usually in quite a radical way.  'aref' does some very
> > special things for char-tables; the semantics of accessing an element
> > of a vector is only correct superficially, not in the details.
> 
> Indeed, and I expected (and still expect) 'equal' to ignore such
> details.

No, 'equal' compares components literally.

> > The internals of a char-table are not really documented in the ELisp
> > manual; the description there is mostly phenomenological, without any
> > details.
> 
> I don't think 'equal' behavior is part of those "internals"

Indeed, it isn't.  But if the internals are described in enough
detail, people will be able to understand why 'equal' returns nil in
your case.

> > If you want to document the internals, I think the proper
> 
> Not the internals, just how 'equal' works.

Why is it important?  The doc string of 'equal' already says

  Return t if two Lisp objects have similar structure and contents.

The two char-tables you are comparing have different structure, so
'equal' returns nil.  What else would you like to document there?