aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMattias EngdegÄrd2022-03-16 15:17:19 +0100
committerMattias EngdegÄrd2022-04-04 09:49:31 +0200
commit16ee9fa138817c061d00cf9a59d2b3f559eebfe1 (patch)
tree7c3530dca438a692ece9ec9062931ac6da939c7a /src
parent85fb2341f82d2ba687cefd21ec84d46d06834f57 (diff)
downloademacs-16ee9fa138817c061d00cf9a59d2b3f559eebfe1.tar.gz
emacs-16ee9fa138817c061d00cf9a59d2b3f559eebfe1.zip
Faster `string-lessp` for unibyte arguments
Since this function is commonly used as a sorting predicate where it is time-critical, this is a useful optimisation. * src/fns.c (Fstring_lessp): Add fast path for the common case when both arguments are unibyte. * test/src/fns-tests.el (fns-tests--string-lessp-cases) (fns-tests-string-lessp): New test.
Diffstat (limited to 'src')
-rw-r--r--src/fns.c17
1 files changed, 13 insertions, 4 deletions
diff --git a/src/fns.c b/src/fns.c
index 8ec23c4e3a8..ee4e80b5069 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -441,15 +441,24 @@ Symbols are also allowed; their print names are used instead. */)
441{ 441{
442 if (SYMBOLP (string1)) 442 if (SYMBOLP (string1))
443 string1 = SYMBOL_NAME (string1); 443 string1 = SYMBOL_NAME (string1);
444 else
445 CHECK_STRING (string1);
444 if (SYMBOLP (string2)) 446 if (SYMBOLP (string2))
445 string2 = SYMBOL_NAME (string2); 447 string2 = SYMBOL_NAME (string2);
446 CHECK_STRING (string1); 448 else
447 CHECK_STRING (string2); 449 CHECK_STRING (string2);
450
451 ptrdiff_t n = min (SCHARS (string1), SCHARS (string2));
452 if (!STRING_MULTIBYTE (string1) && !STRING_MULTIBYTE (string2))
453 {
454 /* Both arguments are unibyte (hot path). */
455 int d = memcmp (SSDATA (string1), SSDATA (string2), n);
456 return d < 0 || (d == 0 && n < SCHARS (string2)) ? Qt : Qnil;
457 }
448 458
449 ptrdiff_t i1 = 0, i1_byte = 0, i2 = 0, i2_byte = 0; 459 ptrdiff_t i1 = 0, i1_byte = 0, i2 = 0, i2_byte = 0;
450 ptrdiff_t end = min (SCHARS (string1), SCHARS (string2));
451 460
452 while (i1 < end) 461 while (i1 < n)
453 { 462 {
454 /* When we find a mismatch, we must compare the 463 /* When we find a mismatch, we must compare the
455 characters, not just the bytes. */ 464 characters, not just the bytes. */