Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8767609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:51:34+00:00 2026-06-13T16:51:34+00:00

I want to implement the vim commandT plugin in emacs. This code is mostly

  • 0

I want to implement the vim commandT plugin in emacs. This code is mostly a translation from the matcher.

I’ve got some elisp here that’s still too slow to use on my netbook –
how can I speed it up?

(eval-when-compile (require 'cl))
(defun commandT-fuzzy-match (choices search-string)
  (sort (loop for choice in choices
              for score = (commandT-fuzzy-score choice search-string (commandT-max-score-per-char choice search-string))
              if (> score 0.0) collect (list score choice))
        #'(lambda (a b) (> (first a) (first b)))
        ))

(defun* commandT-fuzzy-score (choice search-string &optional (score-per-char (commandT-max-score-per-char choice search-string)) (choice-pointer 0) (last-found nil))
  (condition-case error
      (loop for search-char across search-string
            sum (loop until (char-equal search-char (elt choice choice-pointer))
                      do (incf choice-pointer)
                      finally return (let ((factor (cond (last-found (* 0.75 (/ 1.0 (- choice-pointer last-found))))
                                                         (t 1.0))))
                                       (setq last-found choice-pointer)
                                       (max (commandT-fuzzy-score choice search-string score-per-char (1+ choice-pointer) last-found)
                                            (* factor score-per-char)))))
    (args-out-of-range 0.0)   ; end of string hit without match found.
    ))

(defun commandT-max-score-per-char (choice search-string)
  (/ (+ (/ 1.0 (length choice)) (/ 1.0 (length search-string))) 2))

Be sure to compile that part, as that already helps a lot.
And a benchmark:

(let ((choices (split-string (shell-command-to-string "curl http://sprunge.us/FcEL") "\n")))
  (benchmark-run-compiled 10
      (commandT-fuzzy-match choices "az")))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:51:35+00:00Added an answer on June 13, 2026 at 4:51 pm

    Here are some micro optimizations you can try:

    • Use car-less-than-car instead of your lambda expression. This has no visible effect since the time is not spent in sort but in commandT-fuzzy-score.
    • Use defun instead of defun*: those optional arguments with a non-nil default have a non-negligible hidden cost. This reduces the GC cost by almost half (and you started with more than 10% of the time spent in the GC).
    • (* 0.75 (/ 1.0 XXX)) is equal to (/ 0.75 XXX).
    • use eq instead of char-equal (that changes the behavior to always be case-sensitive, tho). This makes a fairly large difference.
    • use aref instead of elt.
    • I don’t understand why you pass last-found in your recursive call, so I obviously don’t fully understand what your algorithm is doing. But assuming that was an error, you can turn it into a local variable instead of passing it as an argument. This saves you time.
    • I don’t understand why you make a recursive call for every search-char that you find, instead of only for the first one. Another way to look at this is that your max compares a “single-char score” with a “whole search-string score” which seems rather odd. If you change your code to do the max outside of the two loops with the recursive call on (1+ first-found), that speeds it up by a factor of 4 in my test case.
    • The multiplication by score-per-char can be moved outside of the loop (this doesn’t seem to be true for your original algorithm).

    Also, the Elisp as implemented in Emacs is pretty slow, so you’re often better off using “big primitives” so as to spend less time interpreting Elisp (byte-)code and more time running C code. Here is for example an alternative implementation (not of your original algorithm but of the one I got after moving the max outside of the loops), using regexp pattern maching to do the inner loop:

    (defun commandT-fuzzy-match-re (choices search-string)
      (let ((search-re (regexp-quote (substring search-string 0 1)))
            (i 1))
        (while (< i (length search-string))
          (setq search-re (concat search-re
                                  (let ((c (aref search-string i)))
                                    (format "[^%c]*\\(%s\\)"
                                            c (regexp-quote (string c))))))
          (setq i (1+ i)))
    
        (sort
         (delq nil
               (mapcar (lambda (choice)
                         (let ((start 0)
                               (best 0.0))
                           (while (string-match search-re choice start)
                             (let ((last-found (match-beginning 0)))
                               (setq start (1+ last-found))
                               (let ((score 1.0)
                                     (i 1)
                                     (choice-pointer nil))
                                 (while (setq choice-pointer (match-beginning i))
                                   (setq i (1+ i))
                                   (setq score (+ score (/ 0.75 (- choice-pointer last-found))))
                                   (setq last-found choice-pointer))
                                 (setq best (max best score)))))
                           (when (> best 0.0)
                             (list (* (commandT-max-score-per-char
                                       choice search-string)
                                      best)
                                   choice))))
                       choices))
         #'car-less-than-car)))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've recently moved from vim to Emacs because I want to use org-mode .
i starter in jqgrid, i want implement inline edit in jqgrid i have this
given this class definition: public class Frame { IFrameStream CapturedFrom; } I want implement
I want to implement a simple script to do some boring housekeeping on my
I have WSDL and XSD files and want implement a webservice based on this
I want to implement some animation in CSS3 which has an effect like its
I'm currently using Vim to edit PHP files and would like to implement code
i want implement geolocation notification like the app reminders. this is what i have
I want to implement caching of a list pulled from the db. I've had
I want to implement SQLite database for iPhone using PhoneGap. I know some basics

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.