>From 92eef1a5221dd69dc95a9484eafeacad571246f8 Mon Sep 17 00:00:00 2001 From: Daniel Pettersson Date: Sun, 18 Feb 2024 02:05:46 +0100 Subject: [PATCH] Jsonrpc: improve performance of process filter function `run-at-time' keeps `timer-list' list sorted by inserting each timer based on the timer value. This means that `timer--time-less-p' needs is executed ~ N*N/2 times for each N pending messages. This means that jsonrpc becomes unusable for connections that generate a lot messages at the same time. * lisp/jsonrpc.el (Version): Bump to 1.0.25 (jsonrpc--process-filter): Improve performance --- lisp/jsonrpc.el | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/lisp/jsonrpc.el b/lisp/jsonrpc.el index 14fe0447008..9a52b5a76cf 100644 --- a/lisp/jsonrpc.el +++ b/lisp/jsonrpc.el @@ -4,7 +4,7 @@ ;; Author: João Távora ;; Keywords: processes, languages, extensions -;; Version: 1.0.24 +;; Version: 1.0.25 ;; Package-Requires: ((emacs "25.2")) ;; This is a GNU ELPA :core package. Avoid functionality that is not @@ -782,11 +782,22 @@ jsonrpc--process-filter ;; non-locally (typically the reply to a request), so do ;; this all this processing in top-level loops timer. (cl-loop + with time = (timer-relative-time nil 0) for msg = (pop (process-get proc 'jsonrpc-mqueue)) while msg - do (run-at-time 0 nil - (lambda (m) (with-temp-buffer - (jsonrpc-connection-receive conn m))) - msg))))))) + do (let ((timer (timer-create))) + (timer-set-time timer time) + (timer-set-function timer + (lambda (conn msg) + (with-temp-buffer + (jsonrpc-connection-receive conn msg))) + (list conn msg)) + (setf (timer--triggered timer) nil) + ;; We're bypassing `timer-activate' due to performance + ;; concerns. `timer-activate' iterates through all + ;; timers scheduled to execute before inserting our + ;; callback. While it doesn't maintain the precise + ;; order of timers, we should be fine. + (setf timer-list (cons timer timer-list))))))))) (defun jsonrpc--remove (conn id &optional deferred-spec) "Cancel CONN's continuations for ID, including its timer, if it exists. -- 2.39.3 (Apple Git-145)