Skip to content

Commit be80f8d

Browse files
authored
Note performance benefit of using a statically...
typed Str|str for regex literal interpolation.
1 parent c3a93a5 commit be80f8d

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

doc/Language/regexes.pod6

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,10 +1264,15 @@ There are four ways you can interpolate a string into regex as a pattern.
12641264
That is using C«$pattern», C«$($pattern)», C«<$pattern>» or
12651265
C«<{$pattern.method}>».
12661266
1267+
If the variable to be interpolated is statically typed as a C«Str» or C«str»
1268+
(like C«$pattern0» and C«$pattern2» are below) and only interpolated literally
1269+
(like the first example below), than the compiler can optimize that and it runs
1270+
much faster.
1271+
12671272
my Str $text = 'camelia';
12681273
my Str $pattern0 = 'camelia';
1269-
my Str $pattern1 = 'ailemac';
1270-
my Str $pattern2 = '\w+';
1274+
my $pattern1 = 'ailemac';
1275+
my str $pattern2 = '\w+';
12711276
12721277
say $text ~~ / $pattern0 /; # OUTPUT: «「camelia」␤»
12731278
say $text ~~ / $($pattern0) /; # OUTPUT: «「camelia」␤»

0 commit comments

Comments
 (0)