|Think about Loose Coupling|
The costs of packagesby BrowserUk (Pope)
|on Sep 16, 2003 at 01:36 UTC||Need Help??|
BrowserUk has asked for the
wisdom of the Perl Monks concerning the following question:
I'm working on something which will use lots of different 'types', literally hundreds. To prevent having to load all of the code for all of the types, it would be convenient, to put each type in it's own package.
Each of the packages would consist of an AUTOLOAD sub, which would actually resolve back to a single AUTOLOAD sub in a parent class from which each of the others would be subclassed.
The superclass AUTOLOAD would then take care of generating the actual class upon demand. This would allow the programmer to use the superclass in his code and then just use the types as required and have the code to support them generated upon demand.
In the above, use My::Types; would load (actually, generate) a package for each of the types that would notionally look something like
Which causes my $typeA = new TypeA; to end up calling My::Types::AUTOLOAD with the name of the type being used and that will generate the rest of the package required to support the type before passing control to the constructor (new) for that type and then go on normally from there.
The question is, what is the penalty of having hundreds of minimal packages floating around? In terms of both memory usage and the performance of looking up individual routines within the program?
I'm don't fully understand the how the glob lookup works at runtime. I've played around dumping both the contents of the main package space and the individual package spaces and there appear to be many typeglob hashes created for each package, which I find confusing.
Can anyone with a better understanding have a feel for the effect of having close to 1000 of these minimal packages floating around mostly unused would have upon the program?
The alternative I am considering is to require the programmer to use
Looking around in the debugger, I see keys created in %:: that contain (?) hashes for each package loaded. I guess my fear is that by creating all these little packages I might be creating hundreds of hashes each containing just one key/value pair, most of which would never be used. Would the second form conserve any resources or would it amount to much the same thing?
Thanks for any insights you can offer.
Examine what is said, not who speaks."Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
If I understand your problem, I can solve it! Of course, the same can be said for you.