From: Robert Pluim <rpluim@gmail.com>
To: emacs-devel@gnu.org
Subject: Lisp interpreter micro-optimization
Date: Tue, 26 Jul 2022 14:36:33 +0200 [thread overview]
Message-ID: <877d4082zy.fsf@gmail.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 241 bytes --]
Hi,
I noticed we have an assq_no_quit, but no memq_no_quit, so came up
with the attached, which Iʼm convinced speeds up elisp by at least
.01% (Iʼm using Larsi-maths 😺), but Iʼm not convinced itʼs
worth pushing.
Robert
--
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-memq_no_quit.patch --]
[-- Type: text/x-diff, Size: 3734 bytes --]
From 6be3aedccd26fd20040be999a62874188f93469e Mon Sep 17 00:00:00 2001
From: Robert Pluim <rpluim@gmail.com>
Date: Tue, 26 Jul 2022 14:24:42 +0200
Subject: [PATCH] Add memq_no_quit
To: emacs-devel@gnu.org
We have an assq_no_quit, but no memq_no_quit. Add it and use it when
accessing Vinternal_interpreter_environment, which is always safe.
* src/fns.c (memq_no_quit): New function, no_quit version of 'memq'.
* src/lisp.h: Add 'memq_no_quit' prototype.
* src/eval.c (Fsetq): Use 'assq_no_quit'.
(FletX, Flet): Use 'memq_no_quit'.
(eval_sub): Use 'assq_no_quit'.
---
src/eval.c | 8 ++++----
src/fns.c | 12 ++++++++++++
src/lisp.h | 1 +
3 files changed, 17 insertions(+), 4 deletions(-)
diff --git a/src/eval.c b/src/eval.c
index 141d2546f0..0477346d42 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -501,7 +501,7 @@ DEFUN ("setq", Fsetq, Ssetq, 0, UNEVALLED, 0,
Lisp_Object lex_binding
= ((!NILP (Vinternal_interpreter_environment) /* Mere optimization! */
&& SYMBOLP (sym))
- ? Fassq (sym, Vinternal_interpreter_environment)
+ ? assq_no_quit (sym, Vinternal_interpreter_environment)
: Qnil);
if (!NILP (lex_binding))
XSETCDR (lex_binding, val); /* SYM is lexically bound. */
@@ -947,7 +947,7 @@ DEFUN ("let*", FletX, SletX, 1, UNEVALLED, 0,
if (!NILP (lexenv) && SYMBOLP (var)
&& !XSYMBOL (var)->u.s.declared_special
- && NILP (Fmemq (var, Vinternal_interpreter_environment)))
+ && NILP (memq_no_quit (var, Vinternal_interpreter_environment)))
/* Lexically bind VAR by adding it to the interpreter's binding
alist. */
{
@@ -1022,7 +1022,7 @@ DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
if (!NILP (lexenv) && SYMBOLP (var)
&& !XSYMBOL (var)->u.s.declared_special
- && NILP (Fmemq (var, Vinternal_interpreter_environment)))
+ && NILP (memq_no_quit (var, Vinternal_interpreter_environment)))
/* Lexically bind VAR by adding it to the lexenv alist. */
lexenv = Fcons (Fcons (var, tem), lexenv);
else
@@ -2354,7 +2354,7 @@ eval_sub (Lisp_Object form)
already did that when let-binding the variable. */
Lisp_Object lex_binding
= (!NILP (Vinternal_interpreter_environment) /* Mere optimization! */
- ? Fassq (form, Vinternal_interpreter_environment)
+ ? assq_no_quit (form, Vinternal_interpreter_environment)
: Qnil);
return !NILP (lex_binding) ? XCDR (lex_binding) : Fsymbol_value (form);
}
diff --git a/src/fns.c b/src/fns.c
index 7e78bba3a0..fe3a01c2de 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -1785,6 +1785,18 @@ DEFUN ("memq", Fmemq, Smemq, 2, 2, 0,
return Qnil;
}
+/* Like Fmemq but never report an error and do not allow quits.
+ Use only on objects known to be non-circular lists. */
+
+Lisp_Object
+memq_no_quit (Lisp_Object elt, Lisp_Object list)
+{
+ for (; ! NILP (list); list = XCDR (list))
+ if (EQ (XCAR (list), elt))
+ return list;
+ return Qnil;
+}
+
DEFUN ("memql", Fmemql, Smemql, 2, 2, 0,
doc: /* Return non-nil if ELT is an element of LIST. Comparison done with `eql'.
The value is actually the tail of LIST whose car is ELT. */)
diff --git a/src/lisp.h b/src/lisp.h
index 8fcc9b6e75..e92540c258 100644
--- a/src/lisp.h
+++ b/src/lisp.h
@@ -4033,6 +4033,7 @@ #define CONS_TO_INTEGER(cons, type, var) \
extern Lisp_Object nconc2 (Lisp_Object, Lisp_Object);
extern Lisp_Object assq_no_quit (Lisp_Object, Lisp_Object);
extern Lisp_Object assoc_no_quit (Lisp_Object, Lisp_Object);
+extern Lisp_Object memq_no_quit (Lisp_Object, Lisp_Object);
extern void clear_string_char_byte_cache (void);
extern ptrdiff_t string_char_to_byte (Lisp_Object, ptrdiff_t);
extern ptrdiff_t string_byte_to_char (Lisp_Object, ptrdiff_t);
--
2.37.1.116.g9dd64cb4d3
next reply other threads:[~2022-07-26 12:36 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-26 12:36 Robert Pluim [this message]
2022-07-26 13:08 ` Lisp interpreter micro-optimization Eli Zaretskii
2022-07-26 13:11 ` Robert Pluim
2022-07-27 2:22 ` Po Lu
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
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=877d4082zy.fsf@gmail.com \
--to=rpluim@gmail.com \
--cc=emacs-devel@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 public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).