emacs-diffs
[Top][All Lists]
Advanced

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

master 530f163a7f 3/4: Speed up comparisons between 2 fixnums


From: Mattias Engdegård
Subject: master 530f163a7f 3/4: Speed up comparisons between 2 fixnums
Date: Mon, 4 Apr 2022 04:52:28 -0400 (EDT)

branch: master
commit 530f163a7f4f1f0ead119b8d3c3dd9fa882af9b2
Author: Mattias Engdegård <mattiase@acm.org>
Commit: Mattias Engdegård <mattiase@acm.org>

    Speed up comparisons between 2 fixnums
    
    Since <, <=, > and >= have their own byte-ops, the corresponding
    functions are mostly used as arguments to higher-order functions.
    This optimisation is particularly beneficial for sorting, where the
    comparison function is time-critical.
    
    * src/data.c (Flss, Fgtr, Fleq, Fgeq):
    * src/fileio.c (Fcar_less_than_car):
    Fast path for calls with 2 fixnum arguments.
---
 src/data.c   | 12 ++++++++++++
 src/fileio.c |  5 ++++-
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/src/data.c b/src/data.c
index 5894340aba..f06b561dcc 100644
--- a/src/data.c
+++ b/src/data.c
@@ -2817,6 +2817,9 @@ DEFUN ("<", Flss, Slss, 1, MANY, 0,
 usage: (< NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS)  */)
   (ptrdiff_t nargs, Lisp_Object *args)
 {
+  if (nargs == 2 && FIXNUMP (args[0]) && FIXNUMP (args[1]))
+    return XFIXNUM (args[0]) < XFIXNUM (args[1]) ? Qt : Qnil;
+
   return arithcompare_driver (nargs, args, ARITH_LESS);
 }
 
@@ -2825,6 +2828,9 @@ DEFUN (">", Fgtr, Sgtr, 1, MANY, 0,
 usage: (> NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS)  */)
   (ptrdiff_t nargs, Lisp_Object *args)
 {
+  if (nargs == 2 && FIXNUMP (args[0]) && FIXNUMP (args[1]))
+    return XFIXNUM (args[0]) > XFIXNUM (args[1]) ? Qt : Qnil;
+
   return arithcompare_driver (nargs, args, ARITH_GRTR);
 }
 
@@ -2833,6 +2839,9 @@ DEFUN ("<=", Fleq, Sleq, 1, MANY, 0,
 usage: (<= NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS)  */)
   (ptrdiff_t nargs, Lisp_Object *args)
 {
+  if (nargs == 2 && FIXNUMP (args[0]) && FIXNUMP (args[1]))
+    return XFIXNUM (args[0]) <= XFIXNUM (args[1]) ? Qt : Qnil;
+
   return arithcompare_driver (nargs, args, ARITH_LESS_OR_EQUAL);
 }
 
@@ -2841,6 +2850,9 @@ DEFUN (">=", Fgeq, Sgeq, 1, MANY, 0,
 usage: (>= NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS)  */)
   (ptrdiff_t nargs, Lisp_Object *args)
 {
+  if (nargs == 2 && FIXNUMP (args[0]) && FIXNUMP (args[1]))
+    return XFIXNUM (args[0]) >= XFIXNUM (args[1]) ? Qt : Qnil;
+
   return arithcompare_driver (nargs, args, ARITH_GRTR_OR_EQUAL);
 }
 
diff --git a/src/fileio.c b/src/fileio.c
index 5d66a93ac6..c418036fc6 100644
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -5523,7 +5523,10 @@ DEFUN ("car-less-than-car", Fcar_less_than_car, 
Scar_less_than_car, 2, 2, 0,
        doc: /* Return t if (car A) is numerically less than (car B).  */)
   (Lisp_Object a, Lisp_Object b)
 {
-  return arithcompare (Fcar (a), Fcar (b), ARITH_LESS);
+  Lisp_Object ca = Fcar (a), cb = Fcar (b);
+  if (FIXNUMP (ca) && FIXNUMP (cb))
+    return XFIXNUM (ca) < XFIXNUM (cb) ? Qt : Qnil;
+  return arithcompare (ca, cb, ARITH_LESS);
 }
 
 /* Build the complete list of annotations appropriate for writing out



reply via email to

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