Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<$subrule> does not cache the compiled subrule (Or bogus test?) #3852

Open
p6rt opened this issue May 3, 2015 · 3 comments
Open

<$subrule> does not cache the compiled subrule (Or bogus test?) #3852

p6rt opened this issue May 3, 2015 · 3 comments
Labels

Comments

@p6rt
Copy link

p6rt commented May 3, 2015

Migrated from rt.perl.org#124524 (status was 'open')

Searchable as RT124524$

@p6rt
Copy link
Author

p6rt commented May 10, 2016

From @smls

The <$subrule> syntax is in fact implemented now in Rakudo.

The test failure is (supposed to be) for not caching the compiled form of the string $subrule between calls. This is the relevant excerpt from S05​:

  In any case of regex interpolation, if the value already happens
  to be a Regex object, it is not recompiled. If it is a string,
  the compiled form is cached with the string so that it is not
  recompiled next time you use it unless the string changes. (Any
  external lexical variable names must be rebound each time though.)

However, the test seems suspect to me. This is a golfed version of what it does​:

  my $counter = 0;
  my $subrule = '{$counter++}.';
  'a' ~~ /<$subrule>/;
  'b' ~~ /<$subrule>/;
  is($counter, 1, 'string value was cached');

It fails because $counter ends up being 2 instead of 1.
But why should it be 1?
The expression `$counter++` is not executed when the subrule is compiled, but each time it is called.

I'm not sure how a correct test for the caching feature quoted from S05 above, would even look like.
It seems to be intended as a pure performance optimization, rather than something that would change the behavior compared to not doing the caching.

@p6rt
Copy link
Author

p6rt commented May 10, 2016

The RT System itself - Status changed from 'new' to 'open'

@p6rt
Copy link
Author

p6rt commented May 10, 2016

From @smls

Ok, I came up with the following test to replace the existing one​:

  my $counter = 0;
  my $subrule = (class :​: is Cool { method Str { $counter++; '<alnum>' } }).new;
  'a' ~~ /<$subrule>/;
  'b' ~~ /<$subrule>/;

  is $counter, 1, 'string value was cached';

It passes on current Rakudo, so I guess it already implements the correct caching behavior.

@p6rt p6rt added the Bug label Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant