From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Daniel Pettersson Newsgroups: gmane.emacs.bugs Subject: bug#69241: [PATCH] Jsonrpc: improve performance of process filter function Date: Sun, 18 Feb 2024 02:53:01 +0100 Message-ID: Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="6464"; mail-complaints-to="usenet@ciao.gmane.io" To: 69241@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Feb 18 19:25:20 2024 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1rblqq-0001UF-Gd for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 18 Feb 2024 19:25:20 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rblqL-00025E-8N; Sun, 18 Feb 2024 13:24:49 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rblqJ-0001z2-An for bug-gnu-emacs@gnu.org; Sun, 18 Feb 2024 13:24:47 -0500 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rblqJ-0003Bd-2F for bug-gnu-emacs@gnu.org; Sun, 18 Feb 2024 13:24:47 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rblqd-0003VB-Je for bug-gnu-emacs@gnu.org; Sun, 18 Feb 2024 13:25:07 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Daniel Pettersson Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 18 Feb 2024 18:25:07 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 69241 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.170828070313347 (code B ref -1); Sun, 18 Feb 2024 18:25:07 +0000 Original-Received: (at submit) by debbugs.gnu.org; 18 Feb 2024 18:25:03 +0000 Original-Received: from localhost ([127.0.0.1]:37429 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rblqZ-0003Sy-45 for submit@debbugs.gnu.org; Sun, 18 Feb 2024 13:25:03 -0500 Original-Received: from lists.gnu.org ([209.51.188.17]:44974) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rblNj-0001bb-T0 for submit@debbugs.gnu.org; Sun, 18 Feb 2024 12:55:16 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rbWMd-00087O-9H for bug-gnu-emacs@gnu.org; Sat, 17 Feb 2024 20:53:07 -0500 Original-Received: from mail-lj1-f174.google.com ([209.85.208.174]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rbWMa-0006zc-U4 for bug-gnu-emacs@gnu.org; Sat, 17 Feb 2024 20:53:06 -0500 Original-Received: by mail-lj1-f174.google.com with SMTP id 38308e7fff4ca-2d10ad265d5so27777661fa.0 for ; Sat, 17 Feb 2024 17:53:04 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708221182; x=1708825982; h=mime-version:message-id:date:subject:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=A1iDhSOPUhTKsXKHlUuLnJ2UCro9NsKfr5qT66UqHO8=; b=sj42aG/rIasHCIkk7onH/GLgZk8U6RAzFHTFUSqqtYDKlu9EsphM7lmxWoI5qh/+c/ iy4XoBZW3M7fNYg6UjnTu4nw9x3as9fG0Ugjqm8BUisAAUIagN7vtvdGtYlSBv9M2tfK EEju6zV+l/ZpyoZPRlnor3FXgLYrYHfE3oO3NLx+3vboFUWgrdoQJHrBaFhAXZUnfsmM ZbR+g4W38B+PAVO9lN0lOrsC5uOfvOaEzApAJuZf+kG5YJzqRl8mq8HfT6KDgGfbdUiw loQ4DAVPu3o/2oprkub3d1RGOp/H5TjdKOtMDqM6SZ7+pFgQSm2KXXA+sMGSIGvWFj2Q f58Q== X-Gm-Message-State: AOJu0Yz+Cvcuiz1uhd1nlTVQXXUDPA6OK0TyuncoXUZD1tMXAPSda1VT NzuY5IYwHVtkGnmEcWAHtiVR+o1+rXtAxzPBdqzdYYhDyEVClFSYaOhDBNyb X-Google-Smtp-Source: AGHT+IHBFgJyX6OIJgTaoMXCxR1e2c0DTflOIXhwBFZvShveM9chgl1DJvjBpa5Wijalq0gu1VUhoQ== X-Received: by 2002:a2e:9217:0:b0:2d0:dfa6:2b3f with SMTP id k23-20020a2e9217000000b002d0dfa62b3fmr6509093ljg.1.1708221182368; Sat, 17 Feb 2024 17:53:02 -0800 (PST) Original-Received: from Daniels-Air (c-72cde455.027-357-6d6c6d4.bbcust.telenor.se. [85.228.205.114]) by smtp.gmail.com with ESMTPSA id v8-20020a2e9248000000b002d06c31cf6esm506104ljg.124.2024.02.17.17.53.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 17 Feb 2024 17:53:01 -0800 (PST) Received-SPF: pass client-ip=209.85.208.174; envelope-from=daniel.dpettersson.net@gmail.com; helo=mail-lj1-f174.google.com X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FORGED_FROMDOMAIN=0.001, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.25, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:280187 Archived-At: --=-=-= Content-Type: text/plain Tags: patch This was issue was discovered during elpa package dape's development, where an adapter was sending 72000 notifications on startup which leads to emacs looping over timer--time-less-p for > 50 seconds and after the fix for less then 1 second. The "integrity" of timer order are messed with but as timer_check runs all of the ripe timers in the same while loop it only becomes an question of execution order. This change uses timer.el internal api `timer--triggered', but this might be fine as it's tightly coupled with keyboard. In GNU Emacs 30.0.50 (build 1, aarch64-apple-darwin23.1.0, NS appkit-2487.20 Version 14.1.1 (Build 23B81)) of 2023-12-20 built on Daniels-Air Repository revision: 281be72422f42fcc84d43f50723a3e91b7d03cbc Repository branch: master Windowing system distributor 'Apple', version 10.3.2487 System Description: macOS 14.1.1 --=-=-= Content-Type: text/patch; charset=utf-8 Content-Disposition: attachment; filename=0001-Jsonrpc-improve-performance-of-process-filter-functi.patch Content-Transfer-Encoding: quoted-printable >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 @@ =20 ;; Author: Jo=C3=A3o T=C3=A1vora ;; Keywords: processes, languages, extensions -;; Version: 1.0.24 +;; Version: 1.0.25 ;; Package-Requires: ((emacs "25.2")) =20 ;; 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 =3D (timer-relative-time nil 0) for msg =3D (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 m= sg))) + (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))))))))) =20 (defun jsonrpc--remove (conn id &optional deferred-spec) "Cancel CONN's continuations for ID, including its timer, if it exists. --=20 2.39.3 (Apple Git-145) --=-=-=--