From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id 7mcFARfyD2BidwAA0tVLHw (envelope-from ) for ; Tue, 26 Jan 2021 10:42:31 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2 with LMTPS id Uu3uNxbyD2CNVwAAB5/wlQ (envelope-from ) for ; Tue, 26 Jan 2021 10:42:30 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 5B5559401C0 for ; Tue, 26 Jan 2021 10:42:30 +0000 (UTC) Received: from localhost ([::1]:32868 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l4Lnj-0002Kb-S2 for larch@yhetil.org; Tue, 26 Jan 2021 05:42:23 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:55086) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l4LnO-0002KJ-4b for guix-patches@gnu.org; Tue, 26 Jan 2021 05:42:02 -0500 Received: from debbugs.gnu.org ([209.51.188.43]:57499) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1l4LnN-0004Gh-Tf for guix-patches@gnu.org; Tue, 26 Jan 2021 05:42:01 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1l4LnN-0004A9-Rt for guix-patches@gnu.org; Tue, 26 Jan 2021 05:42:01 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#46100] [PATCH 0/4] Memoize inferior package access. Resent-From: Ludovic =?UTF-8?Q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Tue, 26 Jan 2021 10:42:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 46100 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: Ricardo Wurmus Received: via spool by 46100-submit@debbugs.gnu.org id=B46100.161165769715966 (code B ref 46100); Tue, 26 Jan 2021 10:42:01 +0000 Received: (at 46100) by debbugs.gnu.org; 26 Jan 2021 10:41:37 +0000 Received: from localhost ([127.0.0.1]:40812 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1l4Lmy-00049R-MO for submit@debbugs.gnu.org; Tue, 26 Jan 2021 05:41:37 -0500 Received: from eggs.gnu.org ([209.51.188.92]:37150) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1l4Lmw-00049A-Tm for 46100@debbugs.gnu.org; Tue, 26 Jan 2021 05:41:35 -0500 Received: from fencepost.gnu.org ([2001:470:142:3::e]:45264) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l4Lmr-00043B-BH; Tue, 26 Jan 2021 05:41:29 -0500 Received: from [2a01:e0a:1d:7270:af76:b9b:ca24:c465] (port=34806 helo=ribbon) by fencepost.gnu.org with esmtpsa (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.82) (envelope-from ) id 1l4Lmn-0001iy-SF; Tue, 26 Jan 2021 05:41:27 -0500 From: Ludovic =?UTF-8?Q?Court=C3=A8s?= References: <20210125133738.15609-1-rekado@elephly.net> <20210125133738.15609-2-rekado@elephly.net> Date: Tue, 26 Jan 2021 11:41:24 +0100 In-Reply-To: <20210125133738.15609-2-rekado@elephly.net> (Ricardo Wurmus's message of "Mon, 25 Jan 2021 14:37:36 +0100") Message-ID: <878s8g0yln.fsf_-_@gnu.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: 46100@debbugs.gnu.org Errors-To: guix-patches-bounces+larch=yhetil.org@gnu.org Sender: "Guix-patches" X-Migadu-Flow: FLOW_IN X-Migadu-Spam-Score: -2.85 Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=pass (policy=none) header.from=gnu.org; spf=pass (aspmx1.migadu.com: domain of guix-patches-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=guix-patches-bounces@gnu.org X-Migadu-Queue-Id: 5B5559401C0 X-Spam-Score: -2.85 X-Migadu-Scanner: scn0.migadu.com X-TUID: IRFKCSQ0XhW/ --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Hi! Thanks for digging into this! Ricardo Wurmus skribis: > +(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=3D> (vhash-assoc package results) cdr) There=E2=80=99s a catch here: OUTPUT should be taken into account. Also it=E2=80=99s better to use eq?-ness but=E2=80=A6 I realized =E2=80=98inferior-package-inputs=E2=80=99 & co. do not preserve eq?-ness. So I came up with the attached patch, which addresses these two issues. For me the =E2=80=98packages->manifest=E2=80=99 phase goes from 13s to 2.5s= (19s to 4.6s for the whole script), which is still a lot, but that was without the other patches. Thoughts? Ludo=E2=80=99. --=-=-= Content-Type: text/x-patch Content-Disposition: inline diff --git a/guix/inferior.scm b/guix/inferior.scm index 2fe91beaab..91bbb5aa70 100644 --- a/guix/inferior.scm +++ b/guix/inferior.scm @@ -109,13 +109,14 @@ ;; Inferior Guix process. (define-record-type - (inferior pid socket close version packages table) + (inferior pid socket close version packages id-table table) inferior? (pid inferior-pid) (socket inferior-socket) (close inferior-close-socket) ;procedure (version inferior-version) ;REPL protocol version (packages inferior-package-promise) ;promise of inferior packages + (id-table inferior-package-id-table) ;promise of vhash (table inferior-package-table)) ;promise of vhash (define* (inferior-pipe directory command error-port) @@ -160,6 +161,7 @@ inferior." (('repl-version 0 rest ...) (letrec ((result (inferior 'pipe pipe close (cons 0 rest) (delay (%inferior-packages result)) + (delay (%inferior-package-id-table result)) (delay (%inferior-package-table result))))) ;; For protocol (0 1) and later, send the protocol version we support. @@ -295,6 +297,18 @@ Raise '&inferior-exception' when an exception is read from PORT." (inferior-package inferior name version id))) result))) +(define (%inferior-package-id-table inferior) + (fold (lambda (package table) + (vhash-consv (inferior-package-id package) package + table)) + vlist-null + (inferior-packages inferior))) + +(define (lookup-inferior-package-by-id inferior id) + (match (vhash-assv id (force (inferior-package-id-table inferior))) + (#f #f) + ((_ . package) package))) + (define (inferior-packages inferior) "Return the list of packages known to INFERIOR." (force (inferior-package-promise inferior))) @@ -412,8 +426,10 @@ inferior package." (map (match-lambda ((label ('package id name version) . rest) - ;; XXX: eq?-ness of inferior packages is not preserved here. - `(,label ,(inferior-package inferior name version id) + ;; XXX: eq?-ness of inferior packages is preserved, unless the + ;; package is not public. + `(,label ,(or (lookup-inferior-package-by-id inferior id) + (inferior-package inferior name version id)) ,@rest)) (x x)) inputs)) @@ -642,29 +658,50 @@ failing when GUIX is too old and lacks the 'guix repl' command." (define* (inferior-package->manifest-entry package #:optional (output "out") - #:key (parent (delay #f)) - (properties '())) + #:key (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 cache + (make-hash-table)) + + (define-syntax-rule (memoized package output exp) + (let ((compute (lambda () exp))) + (match (hashq-ref cache package) + (#f + (let ((result (compute))) + (hashq-set! cache package `((,output . ,result))) + result)) + (alist + (match (assoc-ref alist output) + (#f + (let ((result (compute))) + (hashq-set! cache package + `((, output . ,result) ,@alist)) + result)) + (result + result)))))) + + (let loop ((package package) + (output output) + (parent (delay #f))) + (memoized package output + (letrec* ((deps (map (match-lambda + ((label package) + (loop package "out" (delay entry))) + ((label package output) + (loop package output (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)))) ;;; @@ -750,3 +787,7 @@ This is a convenience procedure that people may use in manifests passed to #:cache-directory cache-directory #:ttl ttl))) (open-inferior cached)) + +;;; Local Variables: +;;; eval: (put 'memoized 'scheme-indent-function 1) +;;; End: diff --git a/tests/inferior.scm b/tests/inferior.scm index 7c3d730d0c..ddfae8236d 100644 --- a/tests/inferior.scm +++ b/tests/inferior.scm @@ -195,6 +195,25 @@ (close-inferior inferior) result)) +(test-assert "inferior-package-inputs & pointer identity" + (let* ((inferior (open-inferior %top-builddir + #:command "scripts/guix")) + (lookup (lambda (name) + (first (lookup-inferior-packages inferior name)))) + (guile-gcrypt (lookup "guile-gcrypt")) + (libgcrypt (lookup "libgcrypt")) + (pkg-config (lookup "pkg-config"))) + (define (input name) + (match (assoc name (inferior-package-inputs guile-gcrypt)) + ((label package . _) package))) + + (and (eq? libgcrypt + (car (assoc-ref (inferior-package-inputs guile-gcrypt) + "libgcrypt"))) + (eq? pkg-config + (car (assoc-ref (inferior-package-native-inputs guile-gcrypt) + "pkg-config")))))) + (test-equal "inferior-package-search-paths" (package-native-search-paths guile-3.0) (let* ((inferior (open-inferior %top-builddir --=-=-=--