Yet another PR feedback: avoid hash-ref when possible

This commit is contained in:
Sorawee Porncharoenwase 2020-02-28 11:17:50 -08:00 committed by Jay McCarthy
parent f32e2c752b
commit ee467b6095

View File

@ -290,23 +290,23 @@
;; to do this at package save time, but this will do for now. ;; to do this at package save time, but this will do for now.
(pkg->searchable-text pkg))))) (pkg->searchable-text pkg)))))
(define (package-search text tags) ;; sort-package-names/priority :: (listof string?) (listof (cons/c symbol? package?))
(define res (map (lambda (r) (regexp (regexp-quote r #f))) (string-split text))) ;; Rank packages by favoring those whose name prefixes or contains search strings
(define packages (manager-rpc 'packages)) ;; and whose description contains search strings
(define (sort-package-names/priority text-list packages)
(define (sort-package-names/priority names)
;; A key is a pair of a priority and a package name ;; A key is a pair of a priority and a package name
;; where higher priority means it's more relevant to the search text ;; where higher priority means it's more relevant to the search text
;; Note that the tombstone packages are filtered already, ;; Note that the tombstone packages are filtered already,
;; so it's safe to use (@ pkg ...) ;; so it's safe to use (@ pkg ...)
(define (name->key name) (define (package-pair->key package-pair)
(define pkg (hash-ref packages name)) (define pkg (cdr package-pair))
(define pkg-name (@ pkg name)) (define pkg-name (@ pkg name))
(define pkg-desc (@ pkg description)) (define pkg-desc (@ pkg description))
(define priority (define priority
(for/sum ([text (in-list (remove-duplicates (string-split text)))]) (for/sum ([text (in-list text-list)])
(cond (cond
[(string=? pkg-name text) 1000] ;; NOTE: the exact match will be the first prefix lexicographically
;; so there's no need to consider it
[(string-prefix? pkg-name text) 100] [(string-prefix? pkg-name text) 100]
[(string-contains? pkg-name text) 10] [(string-contains? pkg-name text) 10]
[(and pkg-desc (string-contains? pkg-desc text)) 1] [(and pkg-desc (string-contains? pkg-desc text)) 1]
@ -318,13 +318,20 @@
[(= (car a) (car b)) (string-ci<? (cdr a) (cdr b))] [(= (car a) (car b)) (string-ci<? (cdr a) (cdr b))]
[else (> (car a) (car b))])) [else (> (car a) (car b))]))
(sort names key< #:key name->key #:cache-keys? #t)) (define sorted (sort packages key< #:key package-pair->key #:cache-keys? #t))
(map car sorted))
(define (package-search text tags)
(define text-list (remove-duplicates (string-split text)))
(define res (map (lambda (r) (regexp (regexp-quote r #f))) text-list))
(define packages (manager-rpc 'packages))
(sort-package-names/priority (sort-package-names/priority
(filter (lambda (package-name) text-list
(define pkg (hash-ref packages package-name)) (filter (lambda (package-pair)
(define pkg (cdr package-pair))
(andmap (package-text-matches? pkg) res)) (andmap (package-text-matches? pkg) res))
(hash-keys (hash->list
(for/fold ((ps packages)) ((tag-spec tags)) (for/fold ((ps packages)) ((tag-spec tags))
(match-define (list tag-name include?) tag-spec) (match-define (list tag-name include?) tag-spec)
(for/hash (((package-name pkg) (in-hash ps)) (for/hash (((package-name pkg) (in-hash ps))