Subject: Re: bug#79361: minor jsonrpc optimisation suggestions

From: Mattias Engdegård <[email-masked]>
Date: Fri, 5 Sep 2025 17:16:20 +0200

5 sep. 2025 kl. 12.48 skrev Eli Zaretskii <[email-masked]>:

> What about jsonrpc-event-hook?  It runs in the buffer returned by
> jsonrpc-events-buffer -- is that affected by these changes?

No, that buffer is not affected. It's a pure text buffer, used as a debug log.

However, this made me look at that events buffer. It does seem that eglot and/or jsonrpc logs way too much. It seems unlikely that this wouldn't affect performance. Although the user can turn off much of it (setting :size in `eglot-events-buffer-config` to 0), this should probably be the default.

In fact, the way eglot keeps the events buffer below its maximum size isn't wonderful either:

1076             (save-excursion
1077               (goto-char (point-min))
1078               (while (> (buffer-size) max)
1079                 (delete-region (point) (progn (forward-line 1)
1080                                               (forward-sexp 1)
1081                                               (forward-line 2)
1082                                               (point)))))))))))

So it deletes a small chunk at the very beginning of the buffer. Surely that will ping-pong the gap between the beginning and the end of the buffer every couple of logged items, ie a memmove of all the contents each time? We could do better here.

What about changing this to chopping off a constant proportion of the buffer instead, say 1/3? At least that gives an amortised cost that is independent of the buffer size. It doesn't help with the occasional latency spike though, so again, the default events buffer :size should probably be 0; formatting log items is also costly. Proposed patch attached.

However, even with the :size set to 0, we still get forwarded messages from the stderr buffer in the events buffer, and some language servers seem to be quite chatty. These log entries do not honour the max buffer size at all, which is probably a bug. I didn't do anything about that.