Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Well, I guess I don't.

If minimal memory consumption is part of the "spec" that you are attempting to deliver (i.e. a non-functional requirement), then I'd call it core and wouldn't worry about an extra scalar and code to implement the larger savings. If it isn't part of your spec, then this may be a case of premature optimization.

On the weak reference idea, it was really just equivalent to your #1 -- but using an extra (weak) reference instead of just copying the reference from the first value in the hash. No better and perhaps even worse.

# simplified example use Scalar::Util qw/weaken/; my $weak_class_ref; sub BUILD { my ($self, $ident, $args_ref) = @_; $weak_class_ref ||= {}; $example_hash_of{$ident} = $weak_class_ref; weaken( $weak_class_ref ); }

The other idea I had was to have a separate singleton class that you assign into every object during initialization. But that still just moves the problem of cleaning up when no "live" objects exist into the singleton class.

You want common data to spring into existence when you have objects and spring back out of existence when there are no objects. I don't see any alternative to keeping a count of objects or copying the reference over during construction (and thus letting Perl keep the reference count for you).

-xdg

Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.


In reply to Re^3: Garbage collected class attributes with inside out classes. by xdg
in thread Garbage collected class attributes with inside out classes. by kyle

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 pondering the Monastery: (6)
As of 2024-04-18 13:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found