Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Journal of IlyaM (2933)

Saturday May 11, 2002
02:34 PM

Black magic (support for inlined subs in Perl)

[ #4856 ]
For some time I have idea of Perl module which adds support for inlined subs in Perl. Such module can be useful in some applications like pure Perl parsers where overall cost of subroutine calls is very high.

I've been thinking about possible solutions. My first idea was going with source filters. Use source filter to find place where subroutine is called, find source code of the subroutine, replace subroutine call with source code of the subroutine. However it is hard to do properly. Inlined subrotine should preserve context in which it have been defined (current package, closures, etc). After more thoughts on this subject I started to think that proper solution should operate not on the level of source code but on the level of OP trees. I.e. find in OP tree place where subroutine called, find OP tree of the subroutine, insert subroutine call with copy of subroutine OP tree. Sounds easy but how can I do it in Perl? I haven't programmed in C for very long time and I have relatively limited knoledge of Perl internals. So I suspended this project for some time.

But recently Simon Cozens published his article Where Wizards Fear To Tread. "Wow!" - I though after reading that article - "B::Utils and B::Generate is all I need. Give me couple of hours and this project will be done." How could I be so naive? I've downloaded these modules and as first excerise tried to write code which finds calls to one subroutine and replaces them with calls to another. Sounds easy but it took me several hours. Damn, to finish with this I had to recall C, reread 'perldoc perlguts/xs/api/hack' and even patch XS in B::Generate.

Well, outcome is following: I have slighly better understanding of Perl internals now and I have some idea how I can write Perl module for subroutine inlining support. Given some free time there are some chances I will finish with this project.

P.S. Self-reminder. Do not forget to send patch for B::Generate.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.