From patchwork Thu Sep 21 14:46:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Wurmus X-Patchwork-Id: 54139 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 B95F627BBE2; Thu, 21 Sep 2023 15:47:14 +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.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,SPF_HELO_PASS,URIBL_BLOCKED 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 7C51227BBE9 for ; Thu, 21 Sep 2023 15:47:10 +0100 (BST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qjKxC-0001xQ-My; Thu, 21 Sep 2023 10:46:54 -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 1qjKxA-0001wy-MI for guix-patches@gnu.org; Thu, 21 Sep 2023 10:46:52 -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 1qjKxA-0000iM-BH for guix-patches@gnu.org; Thu, 21 Sep 2023 10:46:52 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qjKxK-0007Ju-Ai for guix-patches@gnu.org; Thu, 21 Sep 2023 10:47:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#66138] [PATCH 4/4] etc/committer: Speed up surrounding-sexp. Resent-From: Ricardo Wurmus Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Thu, 21 Sep 2023 14:47:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 66138 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 66138@debbugs.gnu.org Cc: Ricardo Wurmus Received: via spool by 66138-submit@debbugs.gnu.org id=B66138.169530761628112 (code B ref 66138); Thu, 21 Sep 2023 14:47:02 +0000 Received: (at 66138) by debbugs.gnu.org; 21 Sep 2023 14:46:56 +0000 Received: from localhost ([127.0.0.1]:34609 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qjKxE-0007JM-6R for submit@debbugs.gnu.org; Thu, 21 Sep 2023 10:46:56 -0400 Received: from sender4-of-o51.zoho.com ([136.143.188.51]:21144) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qjKx8-0007Ix-Qi for 66138@debbugs.gnu.org; Thu, 21 Sep 2023 10:46:54 -0400 Delivered-To: rekado@elephly.net ARC-Seal: i=1; a=rsa-sha256; t=1695307591; cv=none; d=zohomail.com; s=zohoarc; b=kUayLwpi8+HdOq5xgLUv0waH/gA/LlhBOPQ0fIFvEehh02YbtfyH9MXRfWkfNDlqJhPRbnND55CaqsrkLHkZIGYgHC/5M5VCpnp5GSHFU+2Qp+arHczpLvMvGQSoh9ayaw52n/tvLbcBD+dWfsSE329s1W8hyHu4RUp2nWuYvnM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695307591; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=C9hWs40LFEpYNDLxgjSVTCMJWBUd6tW8XqpJ1zGs0WY=; b=Z/QHi1Xn52T7okjaf8TifJY+DgrZr+vwlirf7aI22fKBHvDKoDPywggrdDHezWewpnOealwKRNWBFm8XpD9y+DnBhSM/OKZkawYCl/oM5GNNSVSFYt56zUOP38nzYwzM5jwgrspoWg+jD4n3p76GlmiO0/BWiPr7+UvN2+JumMk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=elephly.net; spf=pass smtp.mailfrom=rekado@elephly.net; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1695307591; s=zoho; d=elephly.net; i=rekado@elephly.net; h=From:From:To:To:Cc:Cc:Message-ID:Subject:Subject:Date:Date:In-Reply-To:References:MIME-Version:Content-Transfer-Encoding:Content-Type:Message-Id:Reply-To; bh=C9hWs40LFEpYNDLxgjSVTCMJWBUd6tW8XqpJ1zGs0WY=; b=F+orDq/FST1Xj5mrZf9DcI5xE+QiJCnqhE9iHHPu2R8c2t7StR5JSLGy+HUncq7I ilHZFpMgCx8qIIewQyTt/8Vo+BsbuJqhcjpaY53dBOlBVdjCW2Rh4GyYtuD1vxck4kM DUdtKAkif151cQn3xgcg/tIZQawsm5U2EIZ+lnrQ= Received: from localhost (141.80.245.246 [141.80.245.246]) by mx.zohomail.com with SMTPS id 169530758900326.33740265246354; Thu, 21 Sep 2023 07:46:29 -0700 (PDT) From: Ricardo Wurmus Message-ID: <2bba46036e61fa5899b59372756ac9c5a7403762.1695305549.git.rekado@elephly.net> Date: Thu, 21 Sep 2023 16:46:17 +0200 X-Mailer: git-send-email 2.41.0 In-Reply-To: <0ed6325c8784aacd4b46003bbde8542dd95e08aa.1695305549.git.rekado@elephly.net> References: <0ed6325c8784aacd4b46003bbde8542dd95e08aa.1695305549.git.rekado@elephly.net> MIME-Version: 1.0 X-ZohoMailClient: External 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 The old surrounding-sexp procedure would read all S-expressions from the beginning of the file up to the given line number and then return the last encountered S-expression. This is quite wasteful. Instead we can record all lines that begin with an S-expression and jump straight to the offset closest to the desired line number to read the S-expression there. * etc/committer.scm.in (lines+offsets-with-opening-parens): New procedure. (surrounding-sexp): Use it. --- etc/committer.scm.in | 46 ++++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/etc/committer.scm.in b/etc/committer.scm.in index eb8865513e..0705b29fd9 100755 --- a/etc/committer.scm.in +++ b/etc/committer.scm.in @@ -85,21 +85,39 @@ (define (read-excursion port) (seek port start SEEK_SET) result)) -(define (surrounding-sexp port line-no) +(define (lines+offsets-with-opening-parens port) + "Record all line numbers (and their offsets) where an opening parenthesis is +found in column 0. The resulting list is in reverse order." + (let loop ((acc '()) + (number 0)) + (let ((line (read-line port))) + (cond + ((eof-object? line) acc) + ((string-prefix? "(" line) + (loop (cons (cons number ;line number + (- (ftell port) + (string-length line) 1)) ;offset + acc) + (1+ number))) + (else (loop acc (1+ number))))))) + +(define (surrounding-sexp port target-line-no) "Return the top-level S-expression surrounding the change at line number -LINE-NO in PORT." - (let loop ((i (1- line-no)) - (last-top-level-sexp #f)) - (if (zero? i) - last-top-level-sexp - (match (peek-char port) - (#\( - (let ((sexp (read-excursion port))) - (read-line port) - (loop (1- i) sexp))) - (_ - (read-line port) - (loop (1- i) last-top-level-sexp)))))) +TARGET-LINE-NO in PORT." + (let* ((line-numbers+offsets + (lines+offsets-with-opening-parens port)) + (closest-offset + (or (and=> (list-index (match-lambda + ((line-number . offset) + (< line-number target-line-no))) + line-numbers+offsets) + (lambda (index) + (match (list-ref line-numbers+offsets index) + ((line-number . offset) offset)))) + (error "Could not find surrounding S-expression for line" + target-line-no)))) + (seek port closest-offset SEEK_SET) + (read port))) ;;; Whether the hunk contains a newly added package (definition), a removed ;;; package (removal) or something else (#false).