Subject: [bug#68271] [PATCH 3/3] guix: packages: Speed up deduplicating inputs.

From: Christopher Baines <[email-masked]>
Date: Fri, 5 Jan 2024 20:53:22 +0000

Use delete-duplicates/sort rather than delete-duplicates, as this seems to
perform a little better, at least when testing by computing derivations
targeting i586-pc-gnu for all packages.

* guix/packages.scm (input<?, deduplicate-inputs): New procedures.
(bag-derivation, bag->cross-derivation): Use deduplicate-inputs.

Change-Id: Ic47b50aa52f11d701e5aefa2a095219e3a98cfd1
---
 guix/packages.scm | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)

diff --git a/guix/packages.scm b/guix/packages.scm
index 930b1a3b0e..09dc88e2af 100644
--- a/guix/packages.scm
+++ b/guix/packages.scm
@@ -1889,6 +1889,21 @@ (define (input=? input1 input2)
                       (derivation=? obj1 obj2))
                  (equal? obj1 obj2))))))))
 
+(define (input<? input1 input2)
+  (let ((label1 (first input1))
+        (label2 (first input2)))
+    (if (string=? label1 label2)
+        (let ((obj1 (second input1))
+              (obj2 (second input2)))
+          (if (and (derivation? obj1) (derivation? obj2))
+              (string<? (derivation-file-name obj1)
+                        (derivation-file-name obj2))
+              #f))
+        (string<? label1 label2))))
+
+(define-inlinable (deduplicate-inputs inputs)
+  (delete-duplicates/sort inputs input<? input=?))
+
 (define* (bag->derivation bag #:optional context)
   "Return the derivation to build BAG for SYSTEM.  Optionally, CONTEXT can be
 a package object describing the context in which the call occurs, for improved
@@ -1911,7 +1926,7 @@ (define* (bag->derivation bag #:optional context)
         ;; that lead to the same derivation.  Delete those duplicates to avoid
         ;; issues down the road, such as duplicate entries in '%build-inputs'.
         (apply (bag-build bag) (bag-name bag)
-               (delete-duplicates input-drvs input=?)
+               (deduplicate-inputs input-drvs)
                #:search-paths paths
                #:outputs (bag-outputs bag) #:system system
                (bag-arguments bag)))))
@@ -1951,9 +1966,9 @@ (define* (bag->cross-derivation bag #:optional context)
                                                  all))))
 
     (apply (bag-build bag) (bag-name bag)
-           #:build-inputs (delete-duplicates build-drvs input=?)
-           #:host-inputs (delete-duplicates host-drvs input=?)
-           #:target-inputs (delete-duplicates target-drvs input=?)
+           #:build-inputs (deduplicate-inputs build-drvs)
+           #:host-inputs (deduplicate-inputs host-drvs)
+           #:target-inputs (deduplicate-inputs target-drvs)
            #:search-paths paths
            #:native-search-paths npaths
            #:outputs (bag-outputs bag)
-- 
2.41.0