From 78daf9e02e5bc51f91488d8237cab2050cc060cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludovic=20Court=C3=A8s?= Date: Tue, 27 Jul 2021 17:58:40 +0200 Subject: [PATCH] derivations: Make 'coalesce-duplicate-inputs' linear in the number of inputs. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Partly fixes . Reported by Ricardo Wurmus . When running the command: guix environment pigx-scrnaseq --search-paths --no-grafts this change reduces total heap allocations from 1.4GiB to 717MiB (49%) and wall-clock time from 7.5s to 5.7s (24%). Without '--no-grafts', heap allocations go from 2.1GiB to 1.4GiB (33%) and wall-clock time from 12.1s to 10.9s (10%). * guix/derivations.scm (coalesce-duplicate-inputs): Rewrite using a hash table to make it O(N) rather than O(N²). --- guix/derivations.scm | 47 +++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/guix/derivations.scm b/guix/derivations.scm index 2fe684cc18..33f4dc5d9d 100644 --- a/guix/derivations.scm +++ b/guix/derivations.scm @@ -241,32 +241,29 @@ the store." "Return a list of inputs, such that when INPUTS contains the same DRV twice, they are coalesced, with their sub-derivations merged. This is needed because Nix itself keeps only one of them." - (define (find pred lst) ;inlinable copy of 'find' - (let loop ((lst lst)) - (match lst - (() #f) - ((head . tail) - (if (pred head) head (loop tail)))))) + (define table + (make-hash-table 25)) - (fold (lambda (input result) - (match input - (($ (= derivation-file-name path) sub-drvs) - ;; XXX: quadratic - (match (find (match-lambda - (($ (= derivation-file-name p) - s) - (string=? p path))) - result) - (#f - (cons input result)) - ((and dup ($ drv sub-drvs2)) - ;; Merge DUP with INPUT. - (let ((sub-drvs (delete-duplicates - (append sub-drvs sub-drvs2)))) - (cons (make-derivation-input drv (sort sub-drvs string drv sub-drvs2))) + ;; Merge DUP with INPUT. + (let* ((sub-drvs (delete-duplicates + (append sub-drvs sub-drvs2))) + (input + (make-derivation-input drv + (sort sub-drvs string