Subject: [bug#46102] [PATCH 2/4] inferior: Memoize inferior-package->manifest-entry.

From: Ricardo Wurmus <[email-masked]>
Date: Mon, 25 Jan 2021 14:37:36 +0100

* guix/inferior.scm (inferior-package->manifest-entry): Memoize.
---
 guix/inferior.scm | 55 ++++++++++++++++++++++++++---------------------
 1 file changed, 30 insertions(+), 25 deletions(-)

diff --git a/guix/inferior.scm b/guix/inferior.scm
index da6983d9a6..7bfce5d810 100644
--- a/guix/inferior.scm
+++ b/guix/inferior.scm
@@ -1,5 +1,6 @@
 ;;; GNU Guix --- Functional package management for GNU
 ;;; Copyright © 2018, 2019, 2020 Ludovic Courtès <[email-masked]>
+;;; Copyright © 2021 Ricardo Wurmus <[email-masked]>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -639,31 +640,35 @@ failing when GUIX is too old and lacks the 'guix repl' command."
 ;;; Manifest entries.
 ;;;
 
-(define* (inferior-package->manifest-entry package
-                                           #:optional (output "out")
-                                           #:key (parent (delay #f))
-                                           (properties '()))
-  "Return a manifest entry for the OUTPUT of package PACKAGE."
-  ;; For each dependency, keep a promise pointing to its "parent" entry.
-  (letrec* ((deps  (map (match-lambda
-                          ((label package)
-                           (inferior-package->manifest-entry package
-                                                             #:parent (delay entry)))
-                          ((label package output)
-                           (inferior-package->manifest-entry package output
-                                                             #:parent (delay entry))))
-                        (inferior-package-propagated-inputs package)))
-            (entry (manifest-entry
-                     (name (inferior-package-name package))
-                     (version (inferior-package-version package))
-                     (output output)
-                     (item package)
-                     (dependencies (delete-duplicates deps))
-                     (search-paths
-                      (inferior-package-transitive-native-search-paths package))
-                     (parent parent)
-                     (properties properties))))
-    entry))
+(define inferior-package->manifest-entry
+  (let ((results vlist-null))
+    (lambda* (package #:optional (output "out")
+                      #:key (parent (delay #f))
+                      (properties '()))
+      "Return a manifest entry for the OUTPUT of package PACKAGE."
+      (or (and=> (vhash-assoc package results) cdr)
+          ;; For each dependency, keep a promise pointing to its "parent" entry.
+          (letrec* ((deps  (map (match-lambda
+                                  ((label package)
+                                   (inferior-package->manifest-entry package
+                                                                     #:parent (delay entry)))
+                                  ((label package output)
+                                   (inferior-package->manifest-entry package output
+                                                                     #:parent (delay entry))))
+                                (inferior-package-propagated-inputs package)))
+                    (entry (manifest-entry
+                             (name (inferior-package-name package))
+                             (version (inferior-package-version package))
+                             (output output)
+                             (item package)
+                             (dependencies (delete-duplicates deps))
+                             (search-paths
+                              (inferior-package-transitive-native-search-paths package))
+                             (parent parent)
+                             (properties properties))))
+            (begin
+              (set! results (vhash-cons package entry results))
+              entry))))))
 
 
 ;;;
-- 
2.29.2