Skip to content

Commit 335434d

Browse files
committed
Bring back the onlyname optimization
1 parent 97d40c8 commit 335434d

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/core/NQPRoutine.nqp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,10 @@ my knowhow RegexCaptures {
373373
has @!named-capture-names;
374374
has @!named-capture-counts;
375375

376+
# If there's only one name and captured multiple times, then we fast-path it
377+
# in MATCH. If such a case, this holds the only name.
378+
has str $!onlyname;
379+
376380
# Form this data structure from a capnames hash.
377381
method from-capnames(%capnames) {
378382
nqp::create(self).'!from-capnames'(%capnames)
@@ -385,6 +389,8 @@ my knowhow RegexCaptures {
385389
@!named-capture-counts := nqp::list_i();
386390

387391
# Go over the captures and build up the data structure.
392+
my int $num-names := 0;
393+
my str $onlyname := '';
388394
for %capnames {
389395
my $name := nqp::iterkey_s($_);
390396
if $name ne '' {
@@ -396,9 +402,15 @@ my knowhow RegexCaptures {
396402
nqp::push_s(@!named-capture-names, $name);
397403
nqp::push_i(@!named-capture-counts, $count);
398404
}
405+
$num-names++;
406+
if $count >= 2 && nqp::ord($name) != 36 {
407+
$onlyname := $name;
408+
}
399409
}
400410
}
401411

412+
$!onlyname := $num-names == 1 && $onlyname ne '' ?? $onlyname !! '';
413+
402414
self
403415
}
404416

@@ -451,7 +463,7 @@ my knowhow RegexCaptures {
451463
}
452464

453465
# Get the name of the only capture, if there is only one.
454-
method onlyname() { '' }
466+
method onlyname() { $!onlyname }
455467
}
456468

457469

0 commit comments

Comments
 (0)