Skip to content
Snippets Groups Projects
Commit d4da602e authored by Ludovic Courtès's avatar Ludovic Courtès
Browse files

grafts: Memoize intermediate results in 'cumulative-grafts'.

The time for:

  guix build inkscape -n --no-substitutes

goes down by 30% (in the presence of 3 replacements among all the
packages.)

* guix/grafts.scm (cumulative-grafts): Turn into a monadic procedure in
%STATE-MONAD.  Use the current state as a derivation-to-graft cache.
(graft-derivation): Call 'cumulative-grafts' within 'run-with-state'.
parent fcadd9ff
No related branches found
No related tags found
No related merge requests found
...@@ -217,7 +217,10 @@ (define* (cumulative-grafts store drv grafts ...@@ -217,7 +217,10 @@ (define* (cumulative-grafts store drv grafts
"Augment GRAFTS with additional grafts resulting from the application of "Augment GRAFTS with additional grafts resulting from the application of
GRAFTS to the dependencies of DRV; REFERENCES must be a one-argument procedure GRAFTS to the dependencies of DRV; REFERENCES must be a one-argument procedure
that returns the list of references of the store item it is given. Return the that returns the list of references of the store item it is given. Return the
resulting list of grafts." resulting list of grafts.
This is a monadic procedure in %STATE-MONAD where the state is a vhash mapping
derivations to the corresponding set of grafts."
(define (dependency-grafts item) (define (dependency-grafts item)
(let-values (((drv output) (item->deriver store item))) (let-values (((drv output) (item->deriver store item)))
(if drv (if drv
...@@ -225,23 +228,34 @@ (define (dependency-grafts item) ...@@ -225,23 +228,34 @@ (define (dependency-grafts item)
#:outputs (list output) #:outputs (list output)
#:guile guile #:guile guile
#:system system) #:system system)
grafts))) (state-return grafts))))
(define (return/cache cache value)
(mbegin %store-monad
(set-current-state (vhash-consq drv value cache))
(return value)))
;; TODO: Memoize. (mlet %state-monad ((cache (current-state)))
(match (non-self-references references drv outputs) (match (vhash-assq drv cache)
(() ;no dependencies ((_ . grafts) ;hit
grafts) (return grafts))
(deps ;one or more dependencies (#f ;miss
(let* ((grafts (delete-duplicates (append-map dependency-grafts deps) (match (non-self-references references drv outputs)
eq?)) (() ;no dependencies
(origins (map graft-origin-file-name grafts))) (return/cache cache grafts))
(if (find (cut member <> deps) origins) (deps ;one or more dependencies
(let ((new (graft-derivation/shallow store drv grafts (mlet %state-monad ((grafts (mapm %state-monad dependency-grafts deps))
#:guile guile (cache (current-state)))
#:system system))) (let* ((grafts (delete-duplicates (concatenate grafts) equal?))
(cons (graft (origin drv) (replacement new)) (origins (map graft-origin-file-name grafts)))
grafts)) (if (find (cut member <> deps) origins)
grafts))))) (let* ((new (graft-derivation/shallow store drv grafts
#:guile guile
#:system system))
(grafts (cons (graft (origin drv) (replacement new))
grafts)))
(return/cache cache grafts))
(return/cache cache grafts))))))))))
(define* (graft-derivation store drv grafts (define* (graft-derivation store drv grafts
#:key (guile (%guile-for-build)) #:key (guile (%guile-for-build))
...@@ -256,8 +270,10 @@ (define* (graft-derivation store drv grafts ...@@ -256,8 +270,10 @@ (define* (graft-derivation store drv grafts
(define references (define references
(references-oracle store drv)) (references-oracle store drv))
(match (cumulative-grafts store drv grafts references (match (run-with-state
#:guile guile #:system system) (cumulative-grafts store drv grafts references
#:guile guile #:system system)
vlist-null) ;the initial cache
((first . rest) ((first . rest)
;; If FIRST is not a graft for DRV, it means that GRAFTS are not ;; If FIRST is not a graft for DRV, it means that GRAFTS are not
;; applicable to DRV and nothing needs to be done. ;; applicable to DRV and nothing needs to be done.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment