From patchwork Sat Oct 28 14:35:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christopher Baines X-Patchwork-Id: 55465 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 3C8E027BBE2; Sat, 28 Oct 2023 15:36:42 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00,MAILING_LIST_MULTI, SPF_HELO_PASS autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 29A9227BBE9 for ; Sat, 28 Oct 2023 15:36:41 +0100 (BST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qwkQT-0005NK-G7; Sat, 28 Oct 2023 10:36:33 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qwkQR-0005NC-9F for guix-patches@gnu.org; Sat, 28 Oct 2023 10:36:31 -0400 Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qwkQQ-0004zI-Iy; Sat, 28 Oct 2023 10:36:31 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qwkQv-0002PZ-QB; Sat, 28 Oct 2023 10:37:01 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#66796] [PATCH] lint: Speed up the formatting linter. Resent-From: Christopher Baines Original-Sender: "Debbugs-submit" Resent-CC: guix@cbaines.net, dev@jpoiret.xyz, ludo@gnu.org, othacehe@gnu.org, rekado@elephly.net, zimon.toutoune@gmail.com, me@tobias.gr, guix-patches@gnu.org Resent-Date: Sat, 28 Oct 2023 14:37:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 66796 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 66796@debbugs.gnu.org Cc: Christopher Baines , Josselin Poiret , Ludovic =?utf-8?q?Court=C3=A8s?= , Mathieu Othacehe , Ricardo Wurmus , Simon Tournier , Tobias Geerinckx-Rice X-Debbugs-Original-To: guix-patches@gnu.org X-Debbugs-Original-Xcc: Christopher Baines , Josselin Poiret , Ludovic =?utf-8?q?Court=C3=A8s?= , Mathieu Othacehe , Ricardo Wurmus , Simon Tournier , Tobias Geerinckx-Rice Received: via spool by submit@debbugs.gnu.org id=B.16985037689201 (code B ref -1); Sat, 28 Oct 2023 14:37:01 +0000 Received: (at submit) by debbugs.gnu.org; 28 Oct 2023 14:36:08 +0000 Received: from localhost ([127.0.0.1]:39337 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qwkQ4-0002OK-34 for submit@debbugs.gnu.org; Sat, 28 Oct 2023 10:36:08 -0400 Received: from lists.gnu.org ([2001:470:142::17]:42544) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qwkQ1-0002Np-BU for submit@debbugs.gnu.org; Sat, 28 Oct 2023 10:36:07 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qwkPN-0005Fr-Sl for guix-patches@gnu.org; Sat, 28 Oct 2023 10:35:25 -0400 Received: from mira.cbaines.net ([212.71.252.8]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qwkPH-0004kv-UO for guix-patches@gnu.org; Sat, 28 Oct 2023 10:35:23 -0400 Received: from localhost (unknown [193.38.40.31]) by mira.cbaines.net (Postfix) with ESMTPSA id 910CD27BBE2 for ; Sat, 28 Oct 2023 15:35:15 +0100 (BST) Received: from localhost (localhost [local]) by localhost (OpenSMTPD) with ESMTPA id f5469498 for ; Sat, 28 Oct 2023 14:35:14 +0000 (UTC) From: Christopher Baines Date: Sat, 28 Oct 2023 15:35:14 +0100 Message-ID: <4499b0c65aa2b2578b1d2efd17cd9f91d97fd2a0.1698503714.git.mail@cbaines.net> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 Received-SPF: pass client-ip=212.71.252.8; envelope-from=mail@cbaines.net; helo=mira.cbaines.net X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, UNPARSEABLE_RELAY=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action 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: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches By storing the bytes to seek to for the start of each line the first time you want to check a package in a file, rather than figuring out where the package starts each time. This cuts down the time to run guix lint -c formatting from 450 seconds to 13 seconds. * guix/lint.scm (report-formatting-issues): If %check-formatting-seek-lookup is a hash table, store vlist's in it to map from a line number to a byte to seek to. (%check-formatting-seek-lookup): New parameter. * guix/scripts/lint.scm (guix-lint): Enable faster formatting linting, when linting all packages. Change-Id: I34e4d3acfbb1e14e026d2e7f712ba8d22b56c147 --- guix/lint.scm | 44 ++++++++++++++++++++++++++++++++++++++++++- guix/scripts/lint.scm | 3 +++ 2 files changed, 46 insertions(+), 1 deletion(-) base-commit: c3cf04d05b452fee549bb84b323d056fd30cef45 diff --git a/guix/lint.scm b/guix/lint.scm index 7ccf52dec1..d94b4026c6 100644 --- a/guix/lint.scm +++ b/guix/lint.scm @@ -68,6 +68,7 @@ (define-module (guix lint) svn-multi-reference-user-name svn-multi-reference-password) #:use-module (guix import stackage) + #:use-module (ice-9 vlist) #:use-module (ice-9 match) #:use-module (ice-9 regex) #:use-module (ice-9 format) @@ -109,6 +110,7 @@ (define-module (guix lint) check-license check-vulnerabilities check-for-updates + %check-formatting-seek-lookup check-formatting check-archival check-profile-collisions @@ -1839,6 +1841,40 @@ (define* (report-formatting-issues package file starting-line #:key (reporters %formatting-reporters)) "Report white-space issues in FILE starting from STARTING-LINE, and report them for PACKAGE." + (define (seek-to-line port line) + (let ((offset + (vlist-ref + (or (hash-ref (%check-formatting-seek-lookup) file) + (call-with-input-file file + (lambda (port) + (let* ((buf-length 80) + (buf (make-string buf-length))) + (let loop ((byte-lookup-list '(0))) + (let* ((rv (%read-delimited! "\n" buf #t port)) + (terminator (car rv)) + (nchars (cdr rv))) + (cond + ((eof-object? terminator) + (let ((byte-lookup-vlist + (list->vlist + (reverse byte-lookup-list)))) + (hash-set! (%check-formatting-seek-lookup) + file + byte-lookup-vlist) + byte-lookup-vlist)) + + ((not terminator) + (loop byte-lookup-list)) + + (nchars + (loop (cons + (ftell port) + byte-lookup-list)))))))))) + (- line 1)))) + (set-port-line! port line) + (seek port offset SEEK_SET) + line)) + (define (sexp-last-line port) ;; Return the last line of the sexp read from PORT or an estimate thereof. (define &failure (list 'failure)) @@ -1857,7 +1893,10 @@ (define* (report-formatting-issues package file starting-line (call-with-input-file file (lambda (port) - (let loop ((line-number 1) + (let loop ((line-number + (if (%check-formatting-seek-lookup) + (seek-to-line port starting-line) + 1)) (last-line #f) (warnings '())) (let ((line (read-line port))) @@ -1879,6 +1918,9 @@ (define* (report-formatting-issues package file starting-line (report package line line-number)) reporters))))))))))) +(define %check-formatting-seek-lookup + (make-parameter #f)) + (define (check-formatting package) "Check the formatting of the source code of PACKAGE." (let ((location (package-location package))) diff --git a/guix/scripts/lint.scm b/guix/scripts/lint.scm index ee3de51fb1..219c3b91be 100644 --- a/guix/scripts/lint.scm +++ b/guix/scripts/lint.scm @@ -222,6 +222,9 @@ (define-command (guix-lint . args) (lambda (store) (cond ((null? args) + ;; Enable fast seeking to lines for the check-formatting linter + (%check-formatting-seek-lookup (make-hash-table)) + (fold-packages (lambda (p r) (run-checkers p checkers #:store store)) '())) (else