Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: RFC: Inline::Blocks or inline as a keyword?

by tobyink (Abbot)
on Jul 30, 2018 at 10:29 UTC ( #1219465=note: print w/replies, xml ) Need Help??


in reply to RFC: Inline::Blocks or inline as a keyword?

I'm the author of Type::Tiny which internally does a lot of inlining. I mean, seriously a lot.

As an example, there is code defined for Int to check a value is an integer, and code defined for ArrayRef to check a value is an arrayref. A lot of Type::Tiny is about efficiently being able to compile those into checks like ArrayRef | Int (check a value is an ArrayRef or an integer), ArrayRef[Int] (check a value is an arrayref of integers), and Int | ArrayRef[Int] (check a value is an integer or an arrayref of integers). If the arrayref is long, you really want to avoid calling a sub to do the integer check for every single element.

So here are my thoughts on what you've said:

Your benchmark is a little too simple. You'll notice the "plain" case and the "do" case run at the same speed. This is because Perl is able to optimize the do case to the plain case. If your do block did something like load a lexically scoped pragma or declare lexical variables, it could not be optimized this way and would run slower than the plain case. Though still a lot faster than a sub call. If a do block can be avoided, you'll get better performance.

Your module implementation is mostly good, but I don't like how you configure it with global variables. What if two modules by different authors try to alter $declmatch? It would be better to allow people to pass these as parameters to import.

You don't seem to do anything to cover the case where the inlined sub closes over variables which are declared later than when it is called.

I have a module called Sub::Block which is similar in aim to your module, though takes a pretty different approach. You may be interested in borrowing the _check_coderef function though. Given a coderef, it inspects the optree for the sub body, searching for any use of return or wantarray and throws an error if it finds them. As noted in your documentation, using return in inlined code is bad. (You may also want to note wantarray and caller in your documentation as also being promlematic.

Replies are listed 'Best First'.
Re^2: RFC: Inline::Blocks or inline as a keyword?
by shmem (Chancellor) on Jul 30, 2018 at 15:05 UTC
    Your benchmark is a little too simple. You'll notice the "plain" case and the "do" case run at the same speed.

    Of course, but there's that drop for the "do_vars" sub in which the arguments passed to a subroutine are assigned as lexicals inside the "do" block. In that case optimizing away the scope isn't possible, which of course happens with loading pragmas.

    Your module implementation is mostly good, but I don't like how you configure it with global variables. What if two modules by different authors try to alter $declmatch? It would be better to allow people to pass these as parameters to import.

    Thank you. - Indeed that's work to be done yet (and easy enough): making the package globals into package private lexicals as defaults, which can be overridden by named import parameters. This definitely makes more sense. update: done in the OP.

    You don't seem to do anything to cover the case where the inlined sub closes over variables which are declared later than when it is called.

    This is on purpose, because that would get me into trouble, into the egg vs. hen problem: I would need to check for outer frames and inspect the nature and existence of variables in those, which are used inside the inlined block. But only constants in closures are suitable for inlining, dynamic closures aren't. So I would need to compile the whole stuff first to munge the source after the fact... no. This module is purely for textual replacement, think cpp.

    As a related note, perl4 had the -P command line switch, which caused the source being run through cpp. When did that go away in perl 5? and why?

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

      The -P switch was deprecated in 5.10 and removed in 5.12. (This was before the current two-stable-versions deprecation cycle.)

      It was replaced with Filter::cpp (though it's only on CPAN, not in core).

Re^2: RFC: Inline::Blocks or inline as a keyword?
by LanX (Bishop) on Sep 08, 2018 at 01:46 UTC
    Hi Toby

    > I'm the author of Type::Tiny which internally does a lot of inlining. I mean, seriously a lot.

    May I ask how you solved this?

    > This is because Perl is able to optimize the do case to the plain case.

    Are you sure?

    DB<17> sub test { print do { 5+4 } } DB<18> print B::Deparse->new("-p")->coderef2text(\&test) { use feature 'current_sub', 'say' ... # yadda shortened print(do { # still here 9 }); }

    update

    this is the mentioned do-case

    { use warnings; use strict; my($total); foreach $_ (1 .. $end) { ($total += do { (1 / $_) }); } $total; }

    DB<27> print $] 5.024001

    hence the do-opcode is still present.

    I suppose you meant to say that it has no performance penalty.(?)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      Each type constraint knows how to generate a string of Perl code for validating a value, given a variable name for the value.

      For example:

      $ perl -MDevel::Hide=Ref::Util::XS,Type::Tiny::XS -MTypes::Standard=-t +ypes -E'say HashRef->inline_check(q/$myvar/)' Devel::Hide hides Ref/Util/XS.pm, Type/Tiny/XS.pm (ref($myvar) eq 'HASH')

      So the ArrayRef[HashRef] type constraint can use that when generating a longer bit of code to validate an arrayref of hashrefs:

      $ perl -MDevel::Hide=Ref::Util::XS,Type::Tiny::XS -MTypes::Standard=-t +ypes -E'say ArrayRef->of(HashRef)->inline_check(q/$myvar/)' Devel::Hide hides Ref/Util/XS.pm, Type/Tiny/XS.pm do { (ref($myvar) eq 'ARRAY') and do { my $ok = 1; for my $i (@{$myvar +}) { ($ok = 0, last) unless (ref($i) eq 'HASH') }; $ok } }
        Are you saying you are dynamically generating code for your type checks which is eval'ed?

        Not sure if that's the meaning of "inlining" in this thread.

        Probably I'm missing your point...

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1219465]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2018-10-15 09:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...














    Results (82 votes). Check out past polls.

    Notices?