[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

[lintian] 01/01: L::Util: Use hack to optimise {,r}strip



This is an automated email from the git hooks/post-receive script.

nthykier pushed a commit to branch master
in repository lintian.

commit c363511e00be78b14ac196eaedaed6b4eb979a5d
Author: Niels Thykier <niels@thykier.net>
Date:   Sun Feb 16 14:22:57 2014 +0100

    L::Util: Use hack to optimise {,r}strip
    
    It is "well-known" that the expression s/^\s++|\s++$/g is slower than
    splitting the expression into to separate ones (namely, s/^\s++// and
    s/\s++$//)[1].
    
    During profiling, it turns out that the s/\s++$// part is the slowest
    part.  This can "fortunately" be replaced by the completely unreadable
    alternative "unpack('A*', $string)", which sadly appears to be at least
    a factor 10 better than s/\s++$//.
    
    Kudos to Ansgar Burchardt for the suggestion of using unpack.
    
    [1] http://www.perlmonks.org/?node_id=865344
    
    Signed-off-by: Niels Thykier <niels@thykier.net>
---
 lib/Lintian/Util.pm | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/lib/Lintian/Util.pm b/lib/Lintian/Util.pm
index 7eed1b3..10cf506 100644
--- a/lib/Lintian/Util.pm
+++ b/lib/Lintian/Util.pm
@@ -1105,10 +1105,12 @@ sub strip (_) { ## no critic (Subroutines::RequireFinalReturn)
     if (defined wantarray) {
         # perl 5.14 s///r would have been useful here.
         my ($arg) = @_;
-        $arg =~ s/^\s++|\s++$//g;
-        return $arg;
+        $arg =~ s/^\s++//;
+        # unpack 'A*' is faster than s/\s++$//
+        return unpack('A*', $arg);
     }
-    $_[0] =~ s/^\s++|\s++$//g;
+    $_[0] =~ s/^\s++//;
+    $_[0] = unpack('A*', $_[0]);
     # void context, so no return needed here.
 }
 
@@ -1127,12 +1129,10 @@ sub lstrip (_) { ## no critic (Subroutines::RequireFinalReturn)
 # prototype for default to $_
 sub rstrip (_) {  ## no critic (Subroutines::RequireFinalReturn)
     if (defined wantarray) {
-        # perl 5.14 s///r would have been useful here.
-        my ($arg) = @_;
-        $arg =~ s/\s++$//g;
-        return $arg;
+        # unpack 'A*' is faster than s/\s++$//
+        return unpack('A*', $_[0]);
     }
-    $_[0] =~ s/\s++$//;
+    $_[0] = unpack('A*', $_[0]);
     # void context, so no return needed here.
 }
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/lintian/lintian.git


Reply to: