packages: Cache the result of `package-derivation'.

* guix/packages.scm (%derivation-cache): New variable.
  (cache, cached-derivation): New procedures.
  (package-derivation): Use them.
This commit is contained in:
Ludovic Courtès 2012-09-01 19:21:06 +02:00
parent 8bb9f66fc6
commit a2ebaddda7

View file

@ -217,9 +217,28 @@ (define (package-transitive-inputs package)
((input rest ...)
(loop rest (cons input result))))))
;;;
;;; Package derivations.
;;;
(define %derivation-cache
;; Package to derivation-path mapping.
(make-weak-key-hash-table))
(define (cache package system drv)
"Memoize DRV as the derivation of PACKAGE on SYSTEM."
(hash-set! %derivation-cache (cons package system) drv)
drv)
(define (cached-derivation package system)
"Return the cached derivation path of PACKAGE for SYSTEM, or #f."
(hash-ref %derivation-cache (cons package system)))
(define* (package-derivation store package
#:optional (system (%current-system)))
"Return the derivation of PACKAGE for SYSTEM."
(or (cached-derivation package system)
(match package
(($ <package> name version source (= build-system-builder builder)
args inputs propagated-inputs native-inputs self-native-input?
@ -227,9 +246,9 @@ (define* (package-derivation store package
;; TODO: For `search-paths', add a builder prologue that calls
;; `set-path-environment-variable'.
(let ((inputs (map (match-lambda
(((? string? name) (and package ($ <package>)))
(((? string? name) (? package? package))
(list name (package-derivation store package)))
(((? string? name) (and package ($ <package>))
(((? string? name) (? package? package)
(? string? sub-drv))
(list name (package-derivation store package)
sub-drv))
@ -249,6 +268,12 @@ (define* (package-derivation store package
(package package)
(input x))))))
(package-transitive-inputs package))))
;; Compute the derivation and cache the result. Caching is
;; important because some derivations, such as the implicit inputs
;; of the GNU build system, will be queried many, many times in a
;; row.
(cache package system
(apply builder
store (string-append name "-" version)
(package-source-derivation store source)
@ -256,7 +281,7 @@ (define* (package-derivation store package
#:outputs outputs #:system system
(if (procedure? args)
(args system)
args))))))
args))))))))
(define* (package-cross-derivation store package)
;; TODO