From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Raffael Stocker Newsgroups: gmane.emacs.bugs Subject: bug#67536: 29.1; Calc mode's math-read-preprocess-string conses unnecessarily Date: Fri, 01 Dec 2023 18:34:10 +0100 Message-ID: References: <83h6l367zx.fsf@gnu.org> <83jzpzaw9s.fsf@gnu.org> 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="11097"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: mu4e 1.10.8; emacs 29.1 Cc: 67536@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Fri Dec 01 18:41:12 2023 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 1r97Vo-0002iT-BO for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 01 Dec 2023 18:41:12 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r97VY-0008Gs-C6; Fri, 01 Dec 2023 12:40:56 -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 1r97VV-0008G3-Lw for bug-gnu-emacs@gnu.org; Fri, 01 Dec 2023 12:40:54 -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 1r97VV-0000Hs-Ch for bug-gnu-emacs@gnu.org; Fri, 01 Dec 2023 12:40:53 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1r97Vd-0004VW-NZ for bug-gnu-emacs@gnu.org; Fri, 01 Dec 2023 12:41:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Raffael Stocker Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 01 Dec 2023 17:41:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 67536 X-GNU-PR-Package: emacs Original-Received: via spool by 67536-submit@debbugs.gnu.org id=B67536.170145244117290 (code B ref 67536); Fri, 01 Dec 2023 17:41:01 +0000 Original-Received: (at 67536) by debbugs.gnu.org; 1 Dec 2023 17:40:41 +0000 Original-Received: from localhost ([127.0.0.1]:56052 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1r97VJ-0004Uo-DC for submit@debbugs.gnu.org; Fri, 01 Dec 2023 12:40:41 -0500 Original-Received: from mail-out.m-online.net ([212.18.0.10]:45102) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1r97VE-0004Uc-55 for 67536@debbugs.gnu.org; Fri, 01 Dec 2023 12:40:39 -0500 Original-Received: from frontend01.mail.m-online.net (unknown [192.168.8.182]) by mail-out.m-online.net (Postfix) with ESMTP id 4ShgLx3yshz1sB7f; Fri, 1 Dec 2023 18:40:25 +0100 (CET) Original-Received: from localhost (dynscan1.mnet-online.de [192.168.6.68]) by mail.m-online.net (Postfix) with ESMTP id 4ShgLx1YfMz1qqlS; Fri, 1 Dec 2023 18:40:25 +0100 (CET) X-Virus-Scanned: amavis at mnet-online.de Original-Received: from mail.mnet-online.de ([192.168.8.182]) by localhost (dynscan1.mail.m-online.net [192.168.6.68]) (amavis, port 10024) with ESMTP id mi8sfg2S79N7; Fri, 1 Dec 2023 18:40:24 +0100 (CET) X-Auth-Info: GBzBkj2/5SXMbsh9fLi642dVuwGbgj5vMwcDQGIzSc8eLLg8JTOSDKXb+RdKV5sn Original-Received: from Whiteflame (ppp-212-114-182-156.dynamic.mnet-online.de [212.114.182.156]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mail.mnet-online.de (Postfix) with ESMTPSA; Fri, 1 Dec 2023 18:40:24 +0100 (CET) In-reply-to: <83jzpzaw9s.fsf@gnu.org> 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:275336 Archived-At: --=-=-= Content-Type: text/plain Eli Zaretskii writes: > Thanks, sounds like a good optimization to me. Splendid! I cleaned it up a bit and ran a few tests against the original function using an empty string, a string without anything to replace and a string requiring many replacements. It seems to behave just as the original. I use cl-flet and (eval-when-compile (require 'cl-lib)). I hope that is ok. Regards, Raffael --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-lisp-calc-calc-aent.el-math-read-preprocess-string-c.patch Content-Description: optimized math-read-preprocess-string >From b3c055cfb42f45ffe662f4fa24ce7b0104949eb2 Mon Sep 17 00:00:00 2001 From: Raffael Stocker Date: Fri, 1 Dec 2023 18:24:59 +0100 Subject: [PATCH] * lisp/calc/calc-aent.el (math-read-preprocess-string): cons less (bug#67536) Use a temp buffer instead of working on a string. This function is called by calc-eval, which in turn is called repeatedly when re-calculating org-mode tables. This function is one of the main bottlenecks there. --- lisp/calc/calc-aent.el | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/lisp/calc/calc-aent.el b/lisp/calc/calc-aent.el index 66ede3295ae..84b580a7033 100644 --- a/lisp/calc/calc-aent.el +++ b/lisp/calc/calc-aent.el @@ -27,6 +27,7 @@ (require 'calc) (require 'calc-macs) +(eval-when-compile (require 'cl-lib)) ;; Declare functions which are defined elsewhere. (declare-function calc-digit-start-entry "calc" ()) @@ -550,19 +551,17 @@ math-read-subscripts ;;;###autoload (defun math-read-preprocess-string (str) "Replace some substrings of STR by Calc equivalents." - (setq str - (replace-regexp-in-string (concat "[" math-read-superscripts "]+") - "^(\\&)" str)) - (setq str - (replace-regexp-in-string (concat "[" math-read-subscripts "]+") - "_(\\&)" str)) - (let ((rep-list math-read-replacement-list)) - (while rep-list - (setq str - (replace-regexp-in-string (nth 0 (car rep-list)) - (nth 1 (car rep-list)) str)) - (setq rep-list (cdr rep-list)))) - str) + (with-temp-buffer + (cl-flet ((replace-all (regexp replacement) + (goto-char 0) + (while (re-search-forward regexp nil t) + (replace-match replacement)))) + (insert str) + (replace-all (concat "[" math-read-superscripts "]+") "^(\\&)") + (replace-all (concat "[" math-read-subscripts "]+") "_(\\&)") + (dolist (rep-elem math-read-replacement-list) + (replace-all (car rep-elem) (cadr rep-elem))) + (buffer-string)))) ;; The next few variables are local to math-read-exprs (and math-read-expr ;; in calc-ext.el), but are set in functions they call. -- 2.43.0 --=-=-=--