emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Internationalize Emacs's messages (swahili)


From: Lars Ingebrigtsen
Subject: Re: Internationalize Emacs's messages (swahili)
Date: Sun, 27 Dec 2020 01:34:51 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

Lars Ingebrigtsen <larsi@gnus.org> writes:

> I whipped one up quickly.

With this totally realistic benchmark, this is what we have today:

(let ((list (make-list 10000 nil)))
  (benchmark-run 1000000 (< (length list) 2)))
=> (10.826215101 0 0.0)

The same, with length<:

(let ((list (make-list 10000 nil)))
  (benchmark-run 1000000 (length< list 2)))
=> (0.05590014099999999 0 0.0)

If we avoid tortoise/hare for small LENGTHs, then it's about 20% faster
than that again (if we're doing (length< list 200)).

diff --git a/src/fns.c b/src/fns.c
index 646c3ed083..2557f41637 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -145,6 +145,37 @@ DEFUN ("safe-length", Fsafe_length, Ssafe_length, 1, 1, 0,
   return make_fixnum (len);
 }
 
+DEFUN ("length<", Flength_less, Slength_less, 2, 2, 0,
+       doc: /* Return non-nil if SEQUENCE is shorter than LENGTH.
+See `length' for allowed values of SEQUENCE.  */)
+  (Lisp_Object sequence, Lisp_Object length)
+{
+  CHECK_FIXNUM (length);
+  EMACS_INT len = XFIXNUM (length);
+
+  if (CONSP (sequence))
+    {
+      EMACS_INT i = 0;
+      /* If LENGTH is short, use a fast loop that doesn't care about
+        whether SEQUENCE is circular or not. */
+      if (len < 0xffff)
+       while (CONSP (sequence))
+         {
+           if (++i >= len)
+             return Qnil;
+           sequence = XCDR (sequence);
+         }
+      /* Signal an error on circular lists. */
+      else
+       FOR_EACH_TAIL (sequence)
+         if (++i >= len)
+           return Qnil;
+      return Qt;
+    }
+  else
+    return XFIXNUM (Flength (sequence)) < len? Qt: Qnil;
+}
+
 DEFUN ("proper-list-p", Fproper_list_p, Sproper_list_p, 1, 1, 0,
        doc: /* Return OBJECT's length if it is a proper list, nil otherwise.
 A proper list is neither circular nor dotted (i.e., its last cdr is nil).  */
@@ -5721,6 +5752,7 @@ syms_of_fns (void)
   defsubr (&Srandom);
   defsubr (&Slength);
   defsubr (&Ssafe_length);
+  defsubr (&Slength_less);
   defsubr (&Sproper_list_p);
   defsubr (&Sstring_bytes);
   defsubr (&Sstring_distance);


-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no



reply via email to

[Prev in Thread] Current Thread [Next in Thread]