[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
04/10: services: 'fold-services' memoizes service values.
From: |
guix-commits |
Subject: |
04/10: services: 'fold-services' memoizes service values. |
Date: |
Sat, 9 Nov 2019 17:06:44 -0500 (EST) |
civodul pushed a commit to branch master
in repository guix.
commit 2a4309de43011f87b3a5044c3d956c261ae151e4
Author: Ludovic Courtès <address@hidden>
Date: Sat Nov 9 21:32:41 2019 +0100
services: 'fold-services' memoizes service values.
Previously 'fold-services' could end up traversing the same services in
the graph several times, which is what this change addresses.
The hit rate on the 'add-data-to-store' cache goves from 9% to 8% on
"guix system build desktop.tmpl -nd", and the number of lookups in that
cache goes from 4458 to 4383.
* gnu/services.scm (fold-services): Turn 'loop' into a monadic procedure
in %STATE-MONAD and use it to memoize values of visited services.
---
gnu/services.scm | 40 ++++++++++++++++++++++++++++------------
1 file changed, 28 insertions(+), 12 deletions(-)
diff --git a/gnu/services.scm b/gnu/services.scm
index 6ee05d4..394470b 100644
--- a/gnu/services.scm
+++ b/gnu/services.scm
@@ -775,18 +775,34 @@ TARGET-TYPE; return the root service adjusted
accordingly."
(eq? (service-kind service) target-type))
services)
((sink)
- (let loop ((sink sink))
- (let* ((dependents (map loop (dependents sink)))
- (extensions (map (apply-extension sink) dependents))
- (extend (service-type-extend (service-kind sink)))
- (compose (service-type-compose (service-kind sink)))
- (params (service-value sink)))
- ;; We distinguish COMPOSE and EXTEND because PARAMS typically has a
- ;; different type than the elements of EXTENSIONS.
- (if extend
- (service (service-kind sink)
- (extend params (compose extensions)))
- sink))))
+ ;; Use the state monad to keep track of already-visited services in the
+ ;; graph and to memoize their value once folded.
+ (run-with-state
+ (let loop ((sink sink))
+ (mlet %state-monad ((visited (current-state)))
+ (match (vhash-assq sink visited)
+ (#f
+ (mlet* %state-monad
+ ((dependents (mapm %state-monad loop (dependents sink)))
+ (visited (current-state))
+ (extensions -> (map (apply-extension sink) dependents))
+ (extend -> (service-type-extend (service-kind sink)))
+ (compose -> (service-type-compose (service-kind sink)))
+ (params -> (service-value sink))
+ (service
+ ->
+ ;; Distinguish COMPOSE and EXTEND because PARAMS
typically
+ ;; has a different type than the elements of EXTENSIONS.
+ (if extend
+ (service (service-kind sink)
+ (extend params (compose extensions)))
+ sink)))
+ (mbegin %state-monad
+ (set-current-state (vhash-consq sink service visited))
+ (return service))))
+ ((_ . service) ;SINK was already visited
+ (return service)))))
+ vlist-null))
(()
(raise
(condition (&missing-target-service-error
- branch master updated (bbfdaaa -> c2cda7d), guix-commits, 2019/11/09
- 01/10: services: hpcguix-web: Explicitly import (guix build utils)., guix-commits, 2019/11/09
- 03/10: tests: Update references to the now-deprecated 'guile-json' variable., guix-commits, 2019/11/09
- 04/10: services: 'fold-services' memoizes service values.,
guix-commits <=
- 02/10: services: hpcguix-web: Delete lock files during activation., guix-commits, 2019/11/09
- 07/10: doc: Mention value /var to localstatedir option., guix-commits, 2019/11/09
- 05/10: gnu: make-bootstrap: Memoize GCC variant., guix-commits, 2019/11/09
- 08/10: gnu: Add matcha-theme., guix-commits, 2019/11/09
- 06/10: gnu: make-bootstrap: Slightly clarify 'package-with-relocatable-glibc'., guix-commits, 2019/11/09
- 10/10: gnu: youtube-dl: Update to 2019.11.05., guix-commits, 2019/11/09
- 09/10: gnu: Add artyfx, guix-commits, 2019/11/09