From: Raffael Stocker <r.stocker@mnet-mail.de>
To: Eli Zaretskii <eliz@gnu.org>
Cc: 67536@debbugs.gnu.org
Subject: bug#67536: 29.1; Calc mode's math-read-preprocess-string conses unnecessarily
Date: Fri, 01 Dec 2023 18:34:10 +0100 [thread overview]
Message-ID: <yplm8r6dvn23.fsf@mnet-mail.de> (raw)
In-Reply-To: <83jzpzaw9s.fsf@gnu.org>
[-- Attachment #1: Type: text/plain, Size: 413 bytes --]
Eli Zaretskii <eliz@gnu.org> 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
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: optimized math-read-preprocess-string --]
[-- Type: text/x-patch, Size: 2285 bytes --]
From b3c055cfb42f45ffe662f4fa24ce7b0104949eb2 Mon Sep 17 00:00:00 2001
From: Raffael Stocker <r.stocker@mnet-mail.de>
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
next prev parent reply other threads:[~2023-12-01 17:34 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-29 21:29 bug#67536: 29.1; Calc mode's math-read-preprocess-string conses unnecessarily Raffael Stocker
2023-11-30 7:00 ` Eli Zaretskii
2023-11-30 18:28 ` Raffael Stocker
2023-11-30 19:14 ` Eli Zaretskii
2023-12-01 17:34 ` Raffael Stocker [this message]
2023-12-01 18:12 ` Eli Zaretskii
2023-12-01 21:10 ` Raffael Stocker
2023-12-02 8:03 ` Eli Zaretskii
2023-12-02 14:56 ` Mattias Engdegård
2023-12-02 19:26 ` Raffael Stocker
2023-12-03 10:43 ` Mattias Engdegård
2023-12-03 11:13 ` Raffael Stocker
2023-12-03 11:58 ` Mattias Engdegård
2023-12-05 18:14 ` Raffael Stocker
2023-12-16 9:40 ` Eli Zaretskii
2023-12-18 10:55 ` Mattias Engdegård
2023-12-18 11:39 ` Raffael Stocker
2023-12-19 16:16 ` Mattias Engdegård
2023-12-19 17:09 ` Raffael Stocker
2023-12-19 18:15 ` Mattias Engdegård
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=yplm8r6dvn23.fsf@mnet-mail.de \
--to=r.stocker@mnet-mail.de \
--cc=67536@debbugs.gnu.org \
--cc=eliz@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.