From: Mattias EngdegÄrd Date: Sun, 12 May 2024 09:22:23 +0000 (+0200) Subject: Safer and faster string sorting X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=45e04484357737ef225a4cff8ee6a8de68cb36c6;p=emacs.git Safer and faster string sorting * lisp/emacs-lisp/cl-seq.el (cl-sort): Don't use string mutation when sorting the characters in a string. This avoids O(n^2) run time and makes it future-safe. (cherry picked from commit 38091e43bee991920db81263fe56da444a123fae) --- diff --git a/lisp/emacs-lisp/cl-seq.el b/lisp/emacs-lisp/cl-seq.el index 42f54603899..60d72d8657d 100644 --- a/lisp/emacs-lisp/cl-seq.el +++ b/lisp/emacs-lisp/cl-seq.el @@ -668,7 +668,10 @@ This is a destructive function; it reuses the storage of SEQ if possible. \nKeywords supported: :key \n(fn SEQ PREDICATE [KEYWORD VALUE]...)" (if (nlistp cl-seq) - (cl-replace cl-seq (apply 'cl-sort (append cl-seq nil) cl-pred cl-keys)) + (if (stringp cl-seq) + (concat (apply #'cl-sort (vconcat cl-seq) cl-pred cl-keys)) + (cl-replace cl-seq + (apply #'cl-sort (append cl-seq nil) cl-pred cl-keys))) (cl--parsing-keywords (:key) () (if (memq cl-key '(nil identity)) (sort cl-seq cl-pred)