user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@yhbt.net>
To: meta@public-inbox.org
Subject: [PATCH 1/3] eml: reduce RE captures and possible side effects
Date: Sat,  9 May 2020 08:27:36 +0000	[thread overview]
Message-ID: <20200509082738.23602-2-e@yhbt.net> (raw)
In-Reply-To: <20200509082738.23602-1-e@yhbt.net>

Since Perl 5.6, the `@-' (aka @LAST_MATCH_START) and `@+' (aka
@LAST_MATCH_END) arrays provides integer offsets for every match
as documented in perlvar(1), regardless of regexp modifiers.

We can avoid relying on $1 in the epilogue scan, entirely.

So use these instead of relying on m//g and pos(), since the `g'
modifier can be affected by m//g matches performed in other
places.

Unrelated, but while we're in the area: remove some unnecessary
use of (?:...), too.
---
 lib/PublicInbox/Eml.pm | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/lib/PublicInbox/Eml.pm b/lib/PublicInbox/Eml.pm
index 4508bd84..80e7c1af 100644
--- a/lib/PublicInbox/Eml.pm
+++ b/lib/PublicInbox/Eml.pm
@@ -71,11 +71,11 @@ sub re_memo ($) {
 # compatible with our uses of Email::MIME
 sub new {
 	my $ref = ref($_[1]) ? $_[1] : \(my $cpy = $_[1]);
-	if ($$ref =~ /(?:\r?\n(\r?\n))/gs) { # likely
+	if ($$ref =~ /\r?\n(\r?\n)/s) { # likely
 		# This can modify $$ref in-place and to avoid memcpy/memmove
 		# on a potentially large $$ref.  It does need to make a
 		# copy for $hdr, though.  Idea stolen from Email::Simple
-		my $hdr = substr($$ref, 0, pos($$ref), ''); # sv_chop on $$ref
+		my $hdr = substr($$ref, 0, $+[0], ''); # sv_chop on $$ref
 		substr($hdr, -(length($1))) = ''; # lower SvCUR
 		bless { hdr => \$hdr, crlf => $1, bdy => $ref }, __PACKAGE__;
 	} elsif ($$ref =~ /^[a-z0-9-]+[ \t]*:/ims && $$ref =~ /(\r?\n)\z/s) {
@@ -90,8 +90,8 @@ sub new {
 sub new_sub {
 	my (undef, $ref) = @_;
 	# special case for messages like <85k5su9k59.fsf_-_@lola.goethe.zz>
-	$$ref =~ /\A(?:(\r?\n))/gs or goto &new;
-	my $hdr = substr($$ref, 0, pos($$ref), ''); # sv_chop on $$ref
+	$$ref =~ /\A(\r?\n)/s or goto &new;
+	my $hdr = substr($$ref, 0, $+[0], ''); # sv_chop on $$ref
 	bless { hdr => \$hdr, crlf => $1, bdy => $ref }, __PACKAGE__;
 }
 
@@ -132,8 +132,8 @@ sub mp_descend ($$) {
 	# *sigh* just the regexp match alone seems to bump RSS by
 	# length($$bdy) on a ~30M string:
 	my $epilogue_missing;
-	if ($$bdy =~ /((?:\r?\n)?^--$bnd--[ \t]*\r?$)/gsm) {
-		substr($$bdy, pos($$bdy) - length($1)) = '';
+	if ($$bdy =~ /(?:\r?\n)?^--$bnd--[ \t]*\r?$/sm) {
+		substr($$bdy, $-[0]) = '';
 	} else {
 		$epilogue_missing = 1;
 	}

  reply	other threads:[~2020-05-09  8:27 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-09  8:27 [PATCH 0/3] eml: some fixes and speedups Eric Wong
2020-05-09  8:27 ` Eric Wong [this message]
2020-05-09  8:27 ` [PATCH 2/3] eml: speed up common LF-only emails Eric Wong
2020-05-09  8:27 ` [PATCH 3/3] emlcontentfoo: quiet warning on missing attributes Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200509082738.23602-2-e@yhbt.net \
    --to=e@yhbt.net \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).