unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Lisp interpreter micro-optimization
@ 2022-07-26 12:36 Robert Pluim
  2022-07-26 13:08 ` Eli Zaretskii
  2022-07-27  2:22 ` Po Lu
  0 siblings, 2 replies; 4+ messages in thread
From: Robert Pluim @ 2022-07-26 12:36 UTC (permalink / raw)
  To: emacs-devel

[-- 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


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: Lisp interpreter micro-optimization
  2022-07-26 12:36 Lisp interpreter micro-optimization Robert Pluim
@ 2022-07-26 13:08 ` Eli Zaretskii
  2022-07-26 13:11   ` Robert Pluim
  2022-07-27  2:22 ` Po Lu
  1 sibling, 1 reply; 4+ messages in thread
From: Eli Zaretskii @ 2022-07-26 13:08 UTC (permalink / raw)
  To: Robert Pluim; +Cc: emacs-devel

> From: Robert Pluim <rpluim@gmail.com>
> Date: Tue, 26 Jul 2022 14:36:33 +0200
> 
> 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.

Using these _no_quit variants makes potentially prolonged operations
non-interruptible, which is bad user-friendliness-wise.

So I'm not sure we should make those changes, certainly not
willy-nilly.



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Lisp interpreter micro-optimization
  2022-07-26 13:08 ` Eli Zaretskii
@ 2022-07-26 13:11   ` Robert Pluim
  0 siblings, 0 replies; 4+ messages in thread
From: Robert Pluim @ 2022-07-26 13:11 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: emacs-devel

>>>>> On Tue, 26 Jul 2022 16:08:13 +0300, Eli Zaretskii <eliz@gnu.org> said:

    >> From: Robert Pluim <rpluim@gmail.com>
    >> Date: Tue, 26 Jul 2022 14:36:33 +0200
    >> 
    >> 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.

    Eli> Using these _no_quit variants makes potentially prolonged operations
    Eli> non-interruptible, which is bad user-friendliness-wise.

    Eli> So I'm not sure we should make those changes, certainly not
    Eli> willy-nilly.

Thatʼs why I only applied them to Vinternal_interpreter_environment,
which is known to be non-circular, but it could be large, I guess.

Robert
-- 



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Lisp interpreter micro-optimization
  2022-07-26 12:36 Lisp interpreter micro-optimization Robert Pluim
  2022-07-26 13:08 ` Eli Zaretskii
@ 2022-07-27  2:22 ` Po Lu
  1 sibling, 0 replies; 4+ messages in thread
From: Po Lu @ 2022-07-27  2:22 UTC (permalink / raw)
  To: Robert Pluim; +Cc: emacs-devel

Robert Pluim <rpluim@gmail.com> writes:

> 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.

FWIW we usually use assq_no_quit in situations where non local exits are
not safe.  It's not supposed to be faster than Fassq itself.



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-07-27  2:22 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-26 12:36 Lisp interpreter micro-optimization Robert Pluim
2022-07-26 13:08 ` Eli Zaretskii
2022-07-26 13:11   ` Robert Pluim
2022-07-27  2:22 ` Po Lu

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).