Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: Doubt about fly-weight objects.

by Fletch (Chancellor)
on May 17, 2005 at 10:49 UTC ( #457730=note: print w/replies, xml ) Need Help??

in reply to Doubt about fly-weight objects.

IIRC the point of flyweights is when you have a boatload of instances of a class and having each instance carry around the entire state would take a huge amount of memory. The example given in the Gang Of Four book is an OO text editing widget in which each individual character is represented by a character object.

Replies are listed 'Best First'.
Re^2: Doubt about fly-weight objects.
by etcshadow (Priest) on May 17, 2005 at 16:52 UTC
    IIRC the point of flyweights is when you have a boatload of instances of a class and having each instance carry around the entire state would take a huge amount of memory.

    No, all of the data still exists, it is just all stored in one big heap in the class, rather than piecemeal with each obect instance. Saying that this saves memory usage is kind of like saying that dropping something heavy from your hands will reduce the total mass of the universe. Flyweight objects are so named because they, themselves, consume next-to-no memory, but the total memory used is not reduced; it's just used in a different place (within the same process).

    ------------ :Wq Not an editor command: Wq

      Erm, (using the GoF example of flyweights for characters) if you compare several thousand individual instances of class Letter { char myLetter; } scattered all over the heap versus having several thousand Letter * pointers to the same set of 128/256/n instances living inside the flyweight class it most certainly will use less memory.

        <whacks self on side of head... rattling ensues>

        I still don't get it. Let me make sure I understand what you are saying (please correct me where/if I got something screwed up): You're saying that you've got, let's say: a million instances of class Letter. The instance data of such a class is basically: a single character.

        Now, if you used non-fly-weight objects, you'd have each instance of the class be a reference to a scalar containing a character. So that's one million times a reference + one million times a character. Or, basically, one million times a platform-dependent, but small, number of bytes.

        Let's compare to fly-weights. With fly-weight-objects, you'd have one million times a reference to nothing (probably)... that is a scalar reference pointing to undef, I'd guess. Plus you'd have a hash mapping one million stringifications of a scalar reference to a character... so that hash contains one million characters, plus one million hash keys. That totals to one million times (a scalar reference + a character + a stringified scalar reference). That's considerably more than if you didn't use fly-weight objects (because a 20-odd character long string is pretty big compared to a scalar reference and a single character).

        If you'd chosen some sort of instance data that were actually larger (as opposed to a single character, which is about as small as you can get), then the difference between one reference and a reference plus a hash-key (stringification of that reference) would actually be inconsequential. Your example, though, actually makes fly-weights take up way more than twice as much space (I think).

        So, that's how I interpretted the situation you were laying out. I assume I just misintrpretted it... but how exactly did I?

        ------------ :Wq Not an editor command: Wq

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://457730]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2019-06-25 20:44 GMT
Find Nodes?
    Voting Booth?
    Is there a future for codeless software?

    Results (107 votes). Check out past polls.