unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [PATCH] * src/eval.c: Stop checking for nvars, and use only CONSP
@ 2021-03-02  2:10 Naoya Yamashita
  2021-03-02  2:48 ` Stefan Monnier
                   ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: Naoya Yamashita @ 2021-03-02  2:10 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: Text/Plain, Size: 802 bytes --]

Hi!

I found src/eval.c (let) has redundant conditions, that compares
the length of the list with the current index and also checks if
the current list is cons.

I remove latter check, and it compiled fine and passed all the
tests I added.

However, in such cases, Lisper prefers to compare that the
current list is cons, rather than comparing indices.  Taking a
`cdr` and checking that it is cons is a Lisp idiom in situations
where `mapc` and `dolist` are not available.

The concern is it may be faster to check the index than CONSP.
This would be fast enough because CONSP in C is a macro, which is
eventually converted to a bitwise operation.  The code is
considered to be easier to read and less prone to bugs than the
current code that includes variable reuse and reassignment.

Regards,
Naoya.

[-- Attachment #2: 0001-src-eval.c-Stop-checking-for-nvars-and-use-only-CONS.patch --]
[-- Type: Text/X-Patch, Size: 2481 bytes --]

From 622c96bdb41bda2727242f8a8078bb8114ef667f Mon Sep 17 00:00:00 2001
From: Naoya Yamashita <conao3@gmail.com>
Date: Tue, 2 Mar 2021 10:17:29 +0900
Subject: [PATCH] * src/eval.c: Stop checking for nvars, and use only CONSP

* src/eval.c (let): Remove checking nvars (length of arglist),
and use only CONSP check.
---
 src/eval.c             |  6 ++----
 test/src/eval-tests.el | 27 +++++++++++++++++++++++++++
 2 files changed, 29 insertions(+), 4 deletions(-)

diff --git a/src/eval.c b/src/eval.c
index 542d7f686e..30783f204a 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -1001,11 +1001,10 @@ DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
   /* Make space to hold the values to give the bound variables.  */
   EMACS_INT varlist_len = list_length (varlist);
   SAFE_ALLOCA_LISP (temps, varlist_len);
-  ptrdiff_t nvars = varlist_len;
 
   /* Compute the values and store them in `temps'.  */
 
-  for (argnum = 0; argnum < nvars && CONSP (varlist); argnum++)
+  for (argnum = 0; CONSP (varlist); argnum++)
     {
       maybe_quit ();
       elt = XCAR (varlist);
@@ -1017,12 +1016,11 @@ DEFUN ("let", Flet, Slet, 1, UNEVALLED, 0,
       else
 	temps[argnum] = eval_sub (Fcar (Fcdr (elt)));
     }
-  nvars = argnum;
 
   lexenv = Vinternal_interpreter_environment;
 
   varlist = XCAR (args);
-  for (argnum = 0; argnum < nvars && CONSP (varlist); argnum++)
+  for (argnum = 0; CONSP (varlist); argnum++)
     {
       Lisp_Object var;
 
diff --git a/test/src/eval-tests.el b/test/src/eval-tests.el
index b2b7dfefda..3576254d7d 100644
--- a/test/src/eval-tests.el
+++ b/test/src/eval-tests.el
@@ -226,4 +226,31 @@ eval-tests/backtrace-in-batch-mode/demoted-errors
       (should (equal (string-trim (buffer-string))
                      "Error: (error \"Boo\")")))))
 
+(ert-deftest eval-tests/let ()
+  (should (equal (let (a)
+                   a)
+                 nil))
+
+  (should (equal (let (a b)
+                   (list a b))
+                 '(nil nil)))
+
+  (should (equal (let ((a 1))
+                   a)
+                 1))
+
+  (should (equal (let ((a 1) b)
+                   (list a b))
+                 '(1 nil)))
+
+  ;; (error "`let' bindings can have only one value-form" a 1 2)
+  (should-error (let ((a 1 2))
+                  a)
+                :type 'error)
+
+  ;; (wrong-type-argument symbolp (a))
+  (should-error (let (((a) 1))
+                  a)
+                :type 'wrong-type-argument))
+
 ;;; eval-tests.el ends here
-- 
2.30.1


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

end of thread, other threads:[~2021-03-02 23:48 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-03-02  2:10 [PATCH] * src/eval.c: Stop checking for nvars, and use only CONSP Naoya Yamashita
2021-03-02  2:48 ` Stefan Monnier
2021-03-02  3:09   ` Naoya Yamashita
2021-03-02 14:31     ` Stefan Monnier
2021-03-02 15:19       ` Pip Cet
2021-03-02 15:48         ` Stefan Monnier
2021-03-02 17:04           ` Pip Cet
2021-03-02 17:44             ` Stefan Monnier
2021-03-02 19:50               ` Pip Cet
2021-03-02 23:48                 ` Stefan Monnier
2021-03-02  5:34 ` Pip Cet
2021-03-02  5:41 ` Eli Zaretskii
2021-03-02  7:14   ` Naoya Yamashita
2021-03-02  7:30     ` Pip Cet
2021-03-02 14:00     ` Eli Zaretskii

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