Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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' => { 'oo.pm' => "oo.pm.slow\n\tperl speedup oo.pm.slow > oo.pm\n"},

Where oo.pm.slow is the code I edit and maintain while oo.pm 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

Title:
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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (7)
    As of 2014-12-26 21:05 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (176 votes), past polls