packages: Fix and optimize memoization of `package-derivation'.

* guix/packages.scm (%derivation-cache): Pass an initial size of 100.
  (cache): Use `hashq-set!', and use a SYSTEM/DRV pair as the value.
  (cached-derivation): Update accordingly.
This commit is contained in:
Ludovic Courtès 2012-10-08 22:07:19 +02:00
parent ead1f1086d
commit e4588af969

View file

@ -206,16 +206,23 @@ (define (package-transitive-propagated-inputs package)
(define %derivation-cache (define %derivation-cache
;; Package to derivation-path mapping. ;; Package to derivation-path mapping.
(make-weak-key-hash-table)) (make-weak-key-hash-table 100))
(define (cache package system drv) (define (cache package system drv)
"Memoize DRV as the derivation of PACKAGE on SYSTEM." "Memoize DRV as the derivation of PACKAGE on SYSTEM."
(hash-set! %derivation-cache (cons package system) drv)
;; Use `hashq-set!' instead of `hash-set!' because `hash' returns the
;; same value for all structs (as of Guile 2.0.6), and because pointer
;; equality is sufficient in practice.
(hashq-set! %derivation-cache package `((,system . ,drv)))
drv) drv)
(define (cached-derivation package system) (define (cached-derivation package system)
"Return the cached derivation path of PACKAGE for SYSTEM, or #f." "Return the cached derivation path of PACKAGE for SYSTEM, or #f."
(hash-ref %derivation-cache (cons package system))) (match (hashq-ref %derivation-cache package)
((alist ...)
(assoc-ref alist system))
(#f #f)))
(define* (package-derivation store package (define* (package-derivation store package
#:optional (system (%current-system))) #:optional (system (%current-system)))