unofficial mirror of guix-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [PATCH] Optimize package-transitive-supported-systems
@ 2014-12-21 21:43 Mark H Weaver
  2014-12-21 23:37 ` Ludovic Courtès
  0 siblings, 1 reply; 2+ messages in thread
From: Mark H Weaver @ 2014-12-21 21:43 UTC (permalink / raw)
  To: guix-devel

[-- Attachment #1: Type: text/plain, Size: 506 bytes --]

When hydra evaluates a jobset, 'package-transitive-supported-systems' is
called for every package+system combination.  Each of these calls
traverses the tree of inputs, but without eliminating duplicate
transitive-inputs.  In other words, the amount of time spent is
proportional not to the number of transitive-inputs, but the number of
_paths_ to all transitive-inputs.

This patch memoizes 'package-transitive-supported-systems', so that the
total time to apply it to all packages is O(N).

     Mark



[-- Attachment #2: [PATCH] Optimize package-transitive-supported-systems --]
[-- Type: text/x-patch, Size: 3742 bytes --]

From 90541f6c7e2a9e2f8a7b412532b4b5a56a10e481 Mon Sep 17 00:00:00 2001
From: Mark H Weaver <mhw@netris.org>
Date: Sun, 21 Dec 2014 16:21:02 -0500
Subject: [PATCH] Optimize package-transitive-supported-systems.

* guix/packages.scm (first-value): Remove.
  (define-memoized/v): New macro.
  (package-transitive-supported-systems): Rewrite.
---
 guix/packages.scm | 61 +++++++++++++++++++++++++++----------------------------
 1 file changed, 30 insertions(+), 31 deletions(-)

diff --git a/guix/packages.scm b/guix/packages.scm
index 07f6d0c..2a9a55e 100644
--- a/guix/packages.scm
+++ b/guix/packages.scm
@@ -1,5 +1,6 @@
 ;;; GNU Guix --- Functional package management for GNU
 ;;; Copyright © 2012, 2013, 2014 Ludovic Courtès <ludo@gnu.org>
+;;; Copyright © 2014 Mark H Weaver <mhw@netris.org>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -543,40 +544,38 @@ for the host system (\"native inputs\"), and not target inputs."
 recursively."
   (transitive-inputs (package-propagated-inputs package)))
 
-(define-syntax-rule (first-value exp)
-  "Truncate all but the first value returned by EXP."
-  (call-with-values (lambda () exp)
-    (lambda (result . _)
-      result)))
+(define-syntax define-memoized/v
+  (lambda (form)
+    "Define a memoized single-valued unary procedure with docstring.
+The procedure argument is compared to cached keys using `eqv?'."
+    (syntax-case form ()
+      ((_ (proc arg) docstring body body* ...)
+       (string? (syntax->datum #'docstring))
+       #'(define proc
+           (let ((cache (make-hash-table)))
+             (define (proc arg)
+               docstring
+               (match (hashv-get-handle cache arg)
+                 ((_ . value)
+                  value)
+                 (_
+                  (let ((result (let () body body* ...)))
+                    (hashv-set! cache arg result)
+                    result))))
+             proc))))))
 
-(define (package-transitive-supported-systems package)
+(define-memoized/v (package-transitive-supported-systems package)
   "Return the intersection of the systems supported by PACKAGE and those
 supported by its dependencies."
-  (first-value
-   (let loop ((package package)
-              (systems (package-supported-systems package))
-              (visited vlist-null))
-     (match (vhash-assq package visited)
-       ((_ . result)
-        (values (lset-intersection string=? systems result)
-                visited))
-       (#f
-        (call-with-values
-            (lambda ()
-              (fold2 (lambda (input systems visited)
-                       (match input
-                         ((label (? package? package) . _)
-                          (loop package systems visited))
-                         (_
-                          (values systems visited))))
-                     (lset-intersection string=?
-                                        systems
-                                        (package-supported-systems package))
-                     visited
-                     (package-direct-inputs package)))
-          (lambda (systems visited)
-            (values systems
-                    (vhash-consq package systems visited)))))))))
+  (fold (lambda (input systems)
+          (match input
+            ((label (? package? p) . _)
+             (lset-intersection
+              string=? systems (package-transitive-supported-systems p)))
+            (_
+             systems)))
+        (package-supported-systems package)
+        (package-direct-inputs package)))
 
 (define (bag-transitive-inputs bag)
   "Same as 'package-transitive-inputs', but applied to a bag."
-- 
2.1.2


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] Optimize package-transitive-supported-systems
  2014-12-21 21:43 [PATCH] Optimize package-transitive-supported-systems Mark H Weaver
@ 2014-12-21 23:37 ` Ludovic Courtès
  0 siblings, 0 replies; 2+ messages in thread
From: Ludovic Courtès @ 2014-12-21 23:37 UTC (permalink / raw)
  To: Mark H Weaver; +Cc: guix-devel

Mark H Weaver <mhw@netris.org> skribis:

> When hydra evaluates a jobset, 'package-transitive-supported-systems' is
> called for every package+system combination.  Each of these calls
> traverses the tree of inputs, but without eliminating duplicate
> transitive-inputs.  In other words, the amount of time spent is
> proportional not to the number of transitive-inputs, but the number of
> _paths_ to all transitive-inputs.

Oops!  Though it’s not clear to me that it explains the problems we’re
seeing on Hydra currently, because running
build-aux/hydra/gnu-system.scm on my laptop takes ~1mn.

> This patch memoizes 'package-transitive-supported-systems', so that the
> total time to apply it to all packages is O(N).

Much better.

> From 90541f6c7e2a9e2f8a7b412532b4b5a56a10e481 Mon Sep 17 00:00:00 2001
> From: Mark H Weaver <mhw@netris.org>
> Date: Sun, 21 Dec 2014 16:21:02 -0500
> Subject: [PATCH] Optimize package-transitive-supported-systems.
>
> * guix/packages.scm (first-value): Remove.
>   (define-memoized/v): New macro.
>   (package-transitive-supported-systems): Rewrite.

LGTM, thank you!

Ludo’.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2014-12-21 23:38 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-12-21 21:43 [PATCH] Optimize package-transitive-supported-systems Mark H Weaver
2014-12-21 23:37 ` Ludovic Courtès

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).