Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re^3: inside-out objects using arrays?

by adrianh (Chancellor)
on Sep 18, 2005 at 00:37 UTC ( #492943=note: print w/replies, xml ) Need Help??

in reply to Re^2: inside-out objects using arrays?
in thread inside-out objects using arrays?

I was thinking the following, which would lead to undef array elements once $obj->DESTROY gets called. Still, I like it ;-)

The point borisz was making is that with a solution like this  @name always increases in length as objects are created and deleted, which will lead to you running out of memory in the long term.

Replies are listed 'Best First'.
Re^4: inside-out objects using arrays?
by fishbot_v2 (Chaplain) on Sep 18, 2005 at 00:56 UTC

    You could avoid that problem by assigning the index to the first undef slot, rather than straight incrementing:

    my $instance_index = List::Util::first { !defined $_ } @name;

    You end up, though, with an O(n) constructor. But perhaps the savings in access make up for this.

      If you really want to save memory, you may want to avoid pinning your inside-out array at the high-water mark. Store a stack of reclaimed indices and push to it in the destructor and shift from it when generating the reference for a new object. Then you can shrink the array if necessary.

      (When you ask "Wait, won't it stay at the high-water mark even if there were 1000 elements and are only two now?", I'll sigh and talk about compacting garbage collectors and double-indirection the other flaws in this scheme. Still, it's a fun idea.)

        Hey, that's neat. I'll have to think about that. Thanks!
      Why not just convert @name to %name. You can keep using the numerical index. That changes the array into a sparse array. The time cost is quite minimal.
Re^4: inside-out objects using arrays?
by rvosa (Curate) on Sep 18, 2005 at 01:31 UTC
    I know, I was sort of making that point also. Then I started thinking about how to regain the empty slots, but as fishbot_v2 mentions, that's O(N). Conceivably, you might want to take that performance hit of you subsequently do lots (and lots and lots and lots) of lookups...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://492943]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2018-06-19 12:17 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (113 votes). Check out past polls.