Inlining Plugin Code
A rather sick piece of perl, that is just
sooo wrong and potentially useful

Take the code that normally gets called seperately, insert in the code in the right place and then just run without any conditional execution/etc. Speeds up plugins dramatically, and allows shrinking things like
UI::*.pm
code removed at the request of the author and copyright holder
Has particular relevance for at least 2 things:
Speeding up plugins dramatically, shrinking things like UI::*.pm
For example given this template:
$helloworldTMPL = template [";"], <<'THIS';
sub { $hello = 1;
$world = 2;
return ($hello,$world);
}
THIS
You can add debugging after every statement thus:
$func = &$helloworldTMPL(';print "Yay!n";');
And then just use the compiled version thus:
&$func()
Whereas the callback approach would require you to add mechanism after every
; to call specific callbacks - incurring lookups etc. Also means that every rule in twiki could be moved into a plugin, without necessarily incurring a major performance hit. It's not as powerful as a perl source filter, but a well known idiom to C++ programmers. The other thing it really allows which is the big boost
Is that you
only compile the code you need to, when you need to ie you only compile plugin code when it's actually needed, and if it's needed (to an extent).
Counterpoint: what sort of difference it would really make? Especially w.r.t inlining. The compile step is performed when a module is used (or maybe that's just partial?). So isn't that advantage defeated by the need to get the reference to the functions' symbol table entry when first building the thunk?
Good question. Maybe someone could try it and find out?
The above code and dialog rewritten from an exchange
on TWikiIRC between MS and CDot. -- MattWilkie
Here is an example that might be easier to understand:
sub template {
my $arg1 = shift; # array of template argument placeholders
my $arg2 = shift; # template of result code
return
sub {
my $result = $arg2;
foreach $arg (@$arg1) {
my $replace = shift;
$result =~ s/$arg/$replace/g;
}
return eval $result;
};
}
sub instrumented {
my( $code, $instrument ) = @_;
return template( [";"], $code );
}
# create a template for an anonymous function that supports
# replacement of all the ";" characters within it
my $fn_template = template( [ "##" ], <<THIS
sub {
print "hurdy "; ##
\$hello .= " gurdy";
return \$hello;
}
THIS
);
# Now instantiate this function template to get a reference to
# the real code
my $thing = &$fn_template(';print "got to __LINE__\n";'),"\n";
# Finally call the code
print &$thing(),"\n";
--
CrawfordCurrie - 24 Feb 2005
Apologies are due to the originating author. I added attribution for the code and general idea.
--
MattWilkie - 26 Feb 2005
I got it wrong - the original code
does work.
--
CrawfordCurrie - 26 Feb 2005
Copied from email (Michael to Matt):
[...]
>>I've added attribution to MS and a link to that time in the irc log.
>> Would you prefer to -- MS ?
I'd rather you deleted it actually [...]
I've removed the original code, but not Crawford's rehash. I figure I've stuck my foot in it enough with other peoples' words already for one week.
--
MattWilkie - 27 Feb 2005