in reply to Re: Optimizing away calls to functions defined in eval
in thread Optimizing away calls to functions defined in eval

So, instead of using eval, you're using typeglobs and closures. *ponders* What's the benefit here? Does this execute faster than using eval?

(Sidenote: I know I'm usually the "Don't worry about faster" person, but since everything to solve my problem here is going to be arcane and unreadable-to-non-experts anyways, might as well go for speed. *grins*)

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

  • Comment on Re: Re: Optimizing away calls to functions defined in eval

Replies are listed 'Best First'.
Re: Re: Re: Optimizing away calls to functions defined in eval
by chromatic (Archbishop) on Oct 16, 2001 at 20:33 UTC
    It executes faster. Closures are compiled at (wait for it...) compile time. It uses less memory. Instead of making a new sub, it can point to the same code. All perl has to do is install a pointer in the symbol table and attach a new stash to the CV. The code is checked at compile time, not run time, so errors can be found and corrected.

    Besides that, string eval still has a few memory leaks, even in the latest versions of Perl. (That might change in the next week or two, but it doesn't help you right now.)

    Here's another thought. If you're generating these with an AUTOLOAD mechanism, you could potentially poison your method dispatch cache. That'll slow down the next method call. (Or I could be crazy... that sounds right, but I can't find it documented right now... sorry.)

    Update: It's in gv.c, specifically Perl_gv_fetchmeth. I've read scarier things, but don't ask for an annotation. ow! It's not available from user space, so there's no documentation. Modifying @ISA will mess with it, though.

      Umm if you do find the documentation id be interested to know a bit more about this poisened method cache.

      Cheers,

      Yves
      --
      You are not ready to use symrefs unless you already know why they are bad. -- tadmc (CLPM)

Re: Re: Re: Optimizing away calls to functions defined in eval
by suaveant (Parson) on Oct 16, 2001 at 18:02 UTC
    I am not sure about speed benefits, but in general I find that way easier to work with than evals, since you don't need to deal with the quoting and such. I'm not sure if it would help your optimization issues or not. I would guess that it is more efficient, but it might not be enough of a difference to notice :)

                    - Ant
                    - Some of my best work - Fish Dinner

Re: Re: Re: Optimizing away calls to functions defined in eval
by jackdied (Monk) on Oct 17, 2001 at 00:46 UTC
    What you really want is to generate the subs before hand, from some resourcefile (XML works well). You can even have the seutp be part of the package

    sub getset_subs { return [qw/size height weight/]; }

    That can be used by the generator program to take the existing package and add a bunch of set/get subs to the end of it.

    This isn't perfect for all situations, you don't always know what you want before hand. But memory is cheap, IMO always calculate & produce code before hand if you can instead of producing the same structures at runtime again and again. Just make sure to keep the machine generated code machine generated. Editing machine generated code is a maintenance nightmare.

    -jackdied

      Believe it or not, I'm trying to propose an alternate solution to machine generated code. The problem is that the maintainance headaches move from the code to the generator. Since the generator is almost always a reinvention of the wheel, it's much more difficult to maintain. Having that layer of abstraction, I've found, isn't usually worth it.

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

        I worked for a pure-perl company for a couple years, and we used quite a bit of generated code to good effect. You are right in that you have to pick your battles or you can get screwed.

        Things that generate very well are expressable in XML. If you can't express it easilly in static XML, it isn't a good place to generate. Good examples are database schemas - you then generate the perl accessors and also create/alter scripts from one source. Another is lists of static text to pre/append to pages depending on user preferences - like what level of help they require.

        <help page="user-settings"> <tip level="novice"> Do not touch anything, the defaults are fine </tip> <tip level="admin"> Changing the starting day of the week will impact reports and time + sheets </tip> </help>
        The usage in perl would be like $page->print_hints(-hint_level=>$user->setting('hintlevel')); The perl is overly verbose, but you get the idea.

        -jackdied