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

Examine codebase for subs that can be is pured #1992

Open
zoffixznet opened this issue Jun 29, 2018 · 2 comments
Open

Examine codebase for subs that can be is pured #1992

zoffixznet opened this issue Jun 29, 2018 · 2 comments

Comments

@zoffixznet
Copy link
Contributor

zoffixznet commented Jun 29, 2018

Note: routines that appear too early in the setting are affected by R#1566. If resolving this Issue before that one, ensure what you purify still ~~ Callable.

We probably have a bunch of stuff that is currently missing is pure trait.

m: say .?is-pure for &list, &hash, &slip, &so, &substr
rakudo-moar 84f60adad: OUTPUT: «Nil␤Nil␤Nil␤Nil␤Nil␤»

My understanding is is pure "means that the result is only dependent on the parameters and nothing else. It doesn't have to memoize or anything like that, but it wouldn't be wrong for it to". In other words, if all the args are known at compile time, the routine can be executed right there and then and its result used in the place of the call.

Note that currently static optimizer expects the is pure to be applied to the proto of multi subs and will not see it if you add it only to some candidates.

@zoffixznet
Copy link
Contributor Author

zoffixznet commented Jun 29, 2018

FYI: http://colabti.org/irclogger/irclogger_log/perl6?date=2018-06-29#l748


14:53 | timotimo | i have a cautionary tale for you
-- | -- | --
14:53 | timotimo | we made x "is pure" at some point, and that caused performance to drastically worsen for very long resulting strings
14:54 | Zoffix | timotimo: because it flattened the ropes?
14:54 | timotimo | because  the object it was creating had been serialized before the execution  started, so we ende up turning the string that was "this short thing  repeated 1000 times" in memory (i.e. implemented with a strand) into  actually many kilobytes (or megabytes in this case) of string data
14:54 | timotimo | yes

That problem was worked around with this piece of code in the optimizer:

rakudo/src/Perl6/Optimizer.nqp

Lines 1636 to 1647 in 84f60ad

# Don't constant fold the 'x' operator if the resulting string would be too big.
# 1024 is just a heuristic, measuring might show a bigger value would be fine.
if $all_args_known && self.op_eq_core($op, '&infix:<x>') {
my int $survived := 0;
my int $size;
try {
$size := @args[0].chars * @args[1];
$survived := 1;
}
return $op if $survived && $size > 1024;
}

So possibly stuff that works with strings would either need to remain unpure or would require similar treatments (if they're worth it). This also makes me think this work, if available by then, should not go into 2018.07 Rakudo Star.

@zoffixznet
Copy link
Contributor Author

Spotted one op that currently isn't constant folded, but probably should be:

$ perl6 --target=optimize -e 'say ’a‘ <=> ’b‘'
[...]
- QAST::Op(callstatic &say) <sunk> :statement_id<1> say ’a‘ <=> ’b‘
                  - QAST::Op(callstatic &infix:«<=>») <wanted> <=>
                    - QAST::Want <wanted> a
                      - QAST::WVal(Str) 
                      - Ss
                      - QAST::SVal(a) 
                    - QAST::Want <wanted> b
                      - QAST::WVal(Str) 
                      - Ss
                      - QAST::SVal(b) 

zoffixznet added a commit to Raku/roast that referenced this issue Jul 15, 2018
Otherwise constant folding will blow up the test file, once
R#1992 rakudo/rakudo#1992 is fixed

Orig: bb4d3ebff
ugexe pushed a commit to Raku/roast that referenced this issue Dec 15, 2018
Otherwise constant folding will blow up the test file, once
R#1992 rakudo/rakudo#1992 is fixed

Orig: bb4d3ebff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant