diff mbox series

[bug#39258,1/4] ui: Cut off search early if any regexp does not match.

Message ID 20200601000030.7443-2-arunisaac@systemreboot.net
State Work in progress
Headers show
Series Optimize guix search | expand

Checks

Context Check Description
cbaines/comparison success View comparision
cbaines/git branch success View Git branch
cbaines/applying patch success View Laminar job
cbaines/comparison success View comparision
cbaines/git branch success View Git branch
cbaines/applying patch success View Laminar job

Commit Message

Arun Isaac June 1, 2020, midnight UTC
* guix/ui.scm (relevance): When one of the regexps does not match, cut off
early and return 0. Do not try to match the remaining regexps.
---
 guix/ui.scm | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

Comments

Ludovic Courtès June 9, 2020, 8:29 a.m. UTC | #1
Hi Arun,

Arun Isaac <arunisaac@systemreboot.net> skribis:

> * guix/ui.scm (relevance): When one of the regexps does not match, cut off
> early and return 0. Do not try to match the remaining regexps.

Good catch, LGTM!

> diff --git a/guix/ui.scm b/guix/ui.scm
> index ea5f460865..4a22358963 100644
> --- a/guix/ui.scm
> +++ b/guix/ui.scm
> @@ -1519,11 +1519,16 @@ score, the more relevant OBJ is to REGEXPS."
>                      (+ relevance (* weight (apply + (map score-regexp lst)))))))))
>              0 metrics)))
>  
> -  (let ((scores (map regexp->score regexps)))
> -    ;; Return zero if one of REGEXPS doesn't match.
> -    (if (any zero? scores)
> -        0
> -        (reduce + 0 scores))))
> +  (let loop ((regexps regexps)
> +             (total-score 0))
> +    (match regexps
> +      ((head . tail)
> +       (let ((score (regexp->score head)))
> +         ;; Return zero if one of PATTERNS doesn't match.
> +         (cond
> +          ((zero? score) 0)
> +          (else (loop tail (+ total-score score))))))

You can use ‘if’ since there are only two arms.

Thanks,
Ludo’.
diff mbox series

Patch

diff --git a/guix/ui.scm b/guix/ui.scm
index ea5f460865..4a22358963 100644
--- a/guix/ui.scm
+++ b/guix/ui.scm
@@ -1519,11 +1519,16 @@  score, the more relevant OBJ is to REGEXPS."
                     (+ relevance (* weight (apply + (map score-regexp lst)))))))))
             0 metrics)))
 
-  (let ((scores (map regexp->score regexps)))
-    ;; Return zero if one of REGEXPS doesn't match.
-    (if (any zero? scores)
-        0
-        (reduce + 0 scores))))
+  (let loop ((regexps regexps)
+             (total-score 0))
+    (match regexps
+      ((head . tail)
+       (let ((score (regexp->score head)))
+         ;; Return zero if one of PATTERNS doesn't match.
+         (cond
+          ((zero? score) 0)
+          (else (loop tail (+ total-score score))))))
+      (() total-score))))
 
 (define %package-metrics
   ;; Metrics used to compute the "relevance score" of a package against a set