Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-16 05:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found