Subject: [bug#45893] Hint for package name: full matrix iteration

From: zimoun <[email-masked]>
Date: Wed, 20 Jan 2021 10:49:47 +0100

Hi,

On Wed, 20 Jan 2021 at 00:59, zimoun <[email-masked]> wrote:

> However, the first improvement is to speed up ’string-distance’.  Well,
> the naive recursive implementation is well-known to be really slow.

For the interested reader, the patch implements the naive recursion
version.  And just to compare, I have quickly compared to the iterative
with full matrix.  See [1].

Roughly speaking, it is a factor of 5x.

--8<---------------cut here---------------start------------->8---
scheme@(guix-user)> ,time (define x (read-the-cache "macs-mgit"))
;; 3.425513s real time, 4.524607s run time.  1.619604s spent in GC.
scheme@(guix-user)> ,time (define x (compute-distance2 "macs-mgit"))
;; 0.870167s real time, 1.056089s run time.  0.271307s spent in GC.
scheme@(guix-user)> ,time (define x (compute-distance "macs-mgit"))
;; 3.637917s real time, 5.601863s run time.  2.847500s spent in GC.
--8<---------------cut here---------------end--------------->8---

The naive recursion version seems fast enough for options and commands
–– because there are few.  The key advantage of recursion implementation
is the readibility, IMHO.  Compare with the iteration with full matrix
below.

I am in favor to merge this naive recursion version for now.  And
postpone improvements.  Because to be competitive for package hints,
instead of plain “edit distance“ which scales poorly, there is 2
directions:

 - implement ’string-distance’ at the C level (in the standard library?)
 - pre-process the package names at package cache build time; with
   suffix tree or n-gram or <name-it> –– in the scope of “guix search”
   improvements.

Both are piece of works and I am not convinced the package name hint is
worth.
 
Cheers,
simon

1: <https://en.wikipedia.org/wiki/Levenshtein_distance#Computing_Levenshtein_distance>

--8<---------------cut here---------------start------------->8---
(define (edit-distance s1 s2)
  (let* ((as (string->list s1))
         (bs (string->list s2))
         (s (list->vector as))
         (t (list->vector bs))
         (m (length as))
         (n (length bs))
         (d (make-typed-array 'u32 0 (1+ m) (1+ n))))

    (do ((i 1 (1+ i))) ((> i m))
      (array-set! d i i 0))

    (do ((j 1 (1+ j))) ((> j n))
      (array-set! d j 0 j))

    (do ((j 1 (1+ j))) ((> j n))
      (do ((i 1 (1+ i))) ((> i m))
        (let* ((c1 (vector-ref s (1- i)))
               (c2 (vector-ref t (1- j)))
               (cost (if (char=? c1 c2)
                         0 1))
               (deletion  (1+ (array-ref d (1- i) j)))
               (insertion (1+ (array-ref d i (1- j))))
               (substitution (+ cost
                                (array-ref d (1- i) (1- j))))
               (v (min deletion
                       insertion
                       substitution)))
          (array-set! d v i j))))
    (array-ref d m n)))
--8<---------------cut here---------------end--------------->8---