From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.2 required=3.0 tests=ALL_TRUSTED,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, T_SCC_BODY_TEXT_LINE shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id B90B11F62A for ; Sat, 10 Sep 2022 08:18:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1662797934; bh=vA4Q1YtKpgio3xxAeE6HMcVSBInHwDTGKjdhA26XAKc=; h=From:To:Subject:Date:In-Reply-To:References:From; b=mz6yzrL6Gk8tJynyFN0XOEJi97UZMbRBUjVaIDJrkN9RAmgJ+Xspctm5T0k26X6BT iEPinEOB67D6h9WfmtEJO9Mtu8ZfKjDanLKssmL/MJ5MMcNhhCrMT4k8RmdTI/lQV9 lXwmQKZR9c6elbSmwdm65MwjtKDkfZwc4rIs7Foc= From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 17/38] viewdiff: reuse existing string in diff_before_or_after Date: Sat, 10 Sep 2022 08:17:08 +0000 Message-Id: <20220910081729.2011934-18-e@80x24.org> In-Reply-To: <20220910081729.2011934-1-e@80x24.org> References: <20220910081729.2011934-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: Instead of appending to an ever-growing {obuf}, we'll reuse the existing string (which already has pre-allocated memory). --- lib/PublicInbox/ViewDiff.pm | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/lib/PublicInbox/ViewDiff.pm b/lib/PublicInbox/ViewDiff.pm index 349ffec8..b9194ed4 100644 --- a/lib/PublicInbox/ViewDiff.pm +++ b/lib/PublicInbox/ViewDiff.pm @@ -168,28 +168,26 @@ sub diff_header ($$$) { sub diff_before_or_after ($$) { my ($ctx, $x) = @_; - my $linkify = $ctx->{-linkify}; - my $dst = $ctx->{obuf}; if (exists $ctx->{-anchors} && $$x =~ /\A(.*?) # likely "---\n" # diffstat lines: ((?:^\x20(?:[^\n]+?)(?:\x20+\|\x20[^\n]*\n))+) (\x20[0-9]+\x20files?\x20)changed,([^\n]+\n) (.*?)\z/msx) { # notes, commit message, etc - undef $$x; my @x = ($5, $4, $3, $2, $1); - $$dst .= $linkify->to_html(pop @x); # uninteresting prefix + my $lnk = $ctx->{-linkify}; + $$x = $lnk->to_html(pop @x); # uninteresting prefix for my $l (split(/^/m, pop(@x))) { # per-file diffstat lines $l =~ /^ (.+)( +\| .*\z)/s and - anchor0($dst, $ctx, $1, $2) and next; - $$dst .= $linkify->to_html($l); + anchor0($x, $ctx, $1, $2) and next; + $$x .= $lnk->to_html($l); } - $$dst .= $x[2]; # $3 /^ \d+ files? / + $$x .= pop @x; # $3 /^ \d+ files? / my $ch = $ctx->{changed_href} // '#related'; - $$dst .= qq(changed,); - $$dst .= ascii_html($x[1]); # $4: insertions/deletions - $$dst .= $linkify->to_html($x[0]); # notes, commit message, etc + $$x .= qq(changed,); + $$x .= ascii_html(pop @x); # $4: insertions/deletions + $$x .= $lnk->to_html(pop @x); # notes, commit message, etc } else { - $$dst .= $linkify->to_html($$x); + $ctx->{-linkify}->to_html($$x); } } @@ -247,9 +245,9 @@ sub flush_diff ($$) { $$dst .= $linkify->to_html($s); } } - diff_before_or_after($ctx, \$after) unless $dctx; + $$dst .= diff_before_or_after($ctx, \$after) if !$dctx; } else { - diff_before_or_after($ctx, \$x); + $$dst .= diff_before_or_after($ctx, \$x); } } }