Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

This is half a question (how can this technique be improved?) and half craft (but with too many comments to fit in the craft mold!)

I thought you might one day need this crude-but-efficient (albeit dangerous) technique to speed-up OO modules without compromising the cleaness of the code.

I usually define accessor methods to every attribute of my objects, even those which are implemented as simple hash fields. Thus I have usually a field and a set_field method for each field. I use those accessor methods even within the module, just in case one day I change the implementation and I move the attribute from a simple field to... something else.

Then in order to get back the speed lost in those method calls, when I install the module I pre-process it and replace the methods by hash access through a simple script:

#!/bin/perl -p # basic substitution # processing the field method is easy s/((\$\w+)->field\b/$1\->\{field\}/g; # the set_field method has an argument, capture it s/s/(\$\w+)->set_field\(([^)]*)\)/$1\->\{field\}= $2/g

The actual code I use is improved to process several fields at once, to allow the object to be an array element (hash element is left as an exercice for the reader), and to perform the replacement only if no other argument is passed to the field method:

#!/bin/perl -p BEGIN { $FIELD="field1|field2|field3"; } s/(\$\w+(?:\[\d\])?)->($FIELD)\b(?!\()/$1\->\{$2\}/g; s/(\$\w+(?:\[\d\])?)->set_($FIELD)\(([^)]*)\)/$1\->\{$2\}= $3/g;

Then I just add a depend item in Makefile.PL;

    'depend' => { '' => "\n\tperl speedup >\n"},

Where is the code I edit and maintain while is the module that gets installed.

One caveat is that you can't use parenthesis in an argument to the set_field method, although this could be fixed using the latest regexp extensions.

Once that script is written you can also use it to speedup scripts that use the module, with the important drawback that when the implementation changes you will have to re-run the speedup script on all such scripts.

In reply to Speeding-up OO accessor methods by mirod

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    [Discipulus]: sorry, not able to help erix.. ;=(
    [erix]: I guess I'll stare it down in the end but I'm already staring for a while... :)
    [erix]: btw, I had a typo in there, so reload (if anyone is looking)

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (8)
    As of 2018-01-19 08:15 GMT
    Find Nodes?
      Voting Booth?
      How did you see in the new year?

      Results (216 votes). Check out past polls.