From patchwork Tue Jan 26 11:30:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ludovic_Court=C3=A8s?= X-Patchwork-Id: 26651 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 7419327BC1B; Tue, 26 Jan 2021 11:45:56 +0000 (GMT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id C6CF927BC1A for ; Tue, 26 Jan 2021 11:45:55 +0000 (GMT) Received: from localhost ([::1]:50268 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l4MnC-000559-O1 for patchwork@mira.cbaines.net; Tue, 26 Jan 2021 06:45:54 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:38210) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l4MYp-0008VJ-94 for guix-patches@gnu.org; Tue, 26 Jan 2021 06:31:03 -0500 Received: from debbugs.gnu.org ([209.51.188.43]:57575) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1l4MYn-0004gR-TX for guix-patches@gnu.org; Tue, 26 Jan 2021 06:31:02 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1l4MYn-0005Z2-Pf for guix-patches@gnu.org; Tue, 26 Jan 2021 06:31: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 11:31: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.161166064921358 (code B ref 46100); Tue, 26 Jan 2021 11:31:01 +0000 Received: (at 46100) by debbugs.gnu.org; 26 Jan 2021 11:30:49 +0000 Received: from localhost ([127.0.0.1]:40887 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1l4MYa-0005YP-Kf for submit@debbugs.gnu.org; Tue, 26 Jan 2021 06:30:48 -0500 Received: from eggs.gnu.org ([209.51.188.92]:48464) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1l4MYW-0005Y9-Mj for 46100@debbugs.gnu.org; Tue, 26 Jan 2021 06:30:46 -0500 Received: from fencepost.gnu.org ([2001:470:142:3::e]:46161) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l4MYQ-0004Tq-9u; Tue, 26 Jan 2021 06:30:38 -0500 Received: from [2a01:e0a:1d:7270:af76:b9b:ca24:c465] (port=35696 helo=ribbon) by fencepost.gnu.org with esmtpsa (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.82) (envelope-from ) id 1l4MYP-0004IL-0E; Tue, 26 Jan 2021 06:30:37 -0500 From: Ludovic =?utf-8?q?Court=C3=A8s?= References: <20210125133738.15609-1-rekado@elephly.net> <20210125133738.15609-2-rekado@elephly.net> <878s8g0yln.fsf_-_@gnu.org> Date: Tue, 26 Jan 2021 12:30:34 +0100 In-Reply-To: <878s8g0yln.fsf_-_@gnu.org> ("Ludovic =?utf-8?q?Court=C3=A8s?= "'s message of "Tue, 26 Jan 2021 11:41:24 +0100") Message-ID: <874kj40wbp.fsf_-_@gnu.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 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+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" X-getmail-retrieved-from-mailbox: Patches Ludovic Courtès skribis: > There’s a catch here: OUTPUT should be taken into account. > > Also it’s better to use eq?-ness but… I realized > ‘inferior-package-inputs’ & co. do not preserve eq?-ness. I think I went overboard here: given that is a simple flat record type, using ‘equal?’/‘hash-ref’ is reasonable and that way we avoid the troubles of building an ID-to-package table. All in all it’s slightly more efficient. WDYT? Ludo’. diff --git a/guix/inferior.scm b/guix/inferior.scm index 2fe91beaab..d813b3b918 100644 --- a/guix/inferior.scm +++ b/guix/inferior.scm @@ -642,29 +642,41 @@ 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)) + (key (cons package output))) + (or (hash-ref cache key) + (let ((result (compute))) + (hash-set! cache key 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 +762,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: