Speed up viacode
authorKarl Williamson <khw@khw-desktop.(none)>
Sat, 3 Jul 2010 18:31:54 +0000 (12:31 -0600)
committerJesse Vincent <jesse@bestpractical.com>
Sun, 4 Jul 2010 20:43:44 +0000 (21:43 +0100)
Capturing parentheses greatly slow down regexes, at least here.
On my machine, viacode took 27 seconds for the 22K Unicode names without
capturing parens; 45s with.

lib/charnames.pm

index f3c9b23..3a0c67b 100644 (file)
@@ -699,9 +699,13 @@ sub viacode {
     # Return the official name, if exists.  It's unclear to me (khw) at
     # this juncture if it is better to return a user-defined override, so
     # leaving it as is for now.
-    if ($txt =~ m/^$hex\t\t(.+)/m) {
-        $viacode{$hex} = $1;
-        return $1;
+    if ($txt =~ m/^$hex\t\t/m) {
+
+       # The name starts with the next character and goes up to the
+       # next new-line.  Using capturing parentheses above instead of
+       # @$+ more than doubles the execution time in Perl 5.13
+        $viacode{$hex} = substr($txt, $+[0], index($txt, "\n", $+[0]) - $+[0]);
+        return $viacode{$hex};
     }
   }