Subject: Re: bug#79200: 31.0.50; Duplicated elements for '#<marker at' in buffer-undo-list

From: Gerd Möllmann <[email-masked]>
Date: Sat, 09 Aug 2025 12:18:15 +0200

Gerd Möllmann <[email-masked]> writes:

> Pip Cet via "Bug reports for GNU Emacs, the Swiss army knife of text
> editors" <[email-masked]> writes:
>
>> Well, compact_undo_list is a nop with HAVE_MPS:
>>
>> static Lisp_Object
>> compact_undo_list (Lisp_Object list)
>> {
>> #ifndef HAVE_MPS
>>   Lisp_Object tail, *prev = &list;
>>
>>   for (tail = list; CONSP (tail); tail = XCDR (tail))
>>     {
>>       if (CONSP (XCAR (tail))
>> 	  && MARKERP (XCAR (XCAR (tail)))
>> 	  && !vectorlike_marked_p (&XMARKER (XCAR (XCAR (tail)))->header))
>> 	*prev = XCDR (tail);
>>       else
>> 	prev = xcdr_addr (tail);
>>     }
>> #endif
>>   return list;
>> }
>>
>> We still truncate undo lists, but we don't "compact" them, i.e. treat
>> them as weak structures and remove markers that are referred to only in
>> undo lists. Correct?
>
> I think you are correct. buffer_step -> compact_buffer ->
> truncate_undo_list, but no compact.
>
>> The "obvious" fix would be to use a weak hash table to associate undo
>> information with markers rather than keeping them in the undo
>> list. However, that sounds quite hard to do, and it might make undo a
>> lot more expensive.
>
> Hm, yes that sounds hard. Hm.
>
> Do you think we could, in compact, locally keep track of markers already
> seen, and coalesce their entries somehow? My memory of the undo stuff is
> pretty hazy, alas, and probably outdated, too.

This is in primtive-undo for markers

simple.el:
 3758              ;; Adjust the valid marker adjustments
 3759              (dolist (adj valid-marker-adjustments)
 3760                ;; Insert might have invalidated some of the markers
 3761                ;; via modification hooks.  Update only the currently
 3762                ;; valid ones (bug#25599).
 3763                (if (marker-buffer (car adj))
 3764                    (set-marker (car adj)
 3765                                (- (car adj) (cdr adj)))))))

So, it would seem that one could do that. Famous last words :-).