Re: Usage for weak-maps
I have used them in two ways in practical application. A rather odd but quite interesting use case is short lived caches. Consider a recursive static function such as the Fibonacci sequence. You could hold a cache of the previous computations and allow them to be collected at the GC's leisure. Another use case is the ability to prevent memory leaks in situations where you need to maintain a list of all the instances of an object to create "class based" storage for Javascript Objects (generally through a constructor). Cheers, Bradley On Fri, Oct 29, 2010 at 10:33 AM, P T Withington wrote: > On 2010-10-29, at 04:50, Peter van der Zee wrote: > > > What's the use case for weak maps? What would you do with it that > currently > > impossible and why is the workaround (if any) problematic enough to > warrant > > a weak map implementation? > > Another use case was mentioned in the "New topic regarding Proxies: > intercession for ===" thread. If you want to write a value type, the > constructor needs to return the same object for identical parameters for === > to work, which means it needs a table of all the objects it has ever made, > but it doesn't need (or want) to hang on to objects that are no longer in > use. [The alternative being discussed in the proxy thread is to be able to > customize === to give the same illusion.] > ___ > es-discuss mailing list > es-discuss@mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
RE: Usage for weak-maps
Weak maps are used to create an association (based upon object identity) between an object (the key) and some arbitrary information. They are necessary if you need to maintain such an association and you cannot modify the key object to directly record the association. Without some sort of weak reference mechanism, the data structure needed to maintain such an associations is inherently leaky because the use of an object as a key creates a reference to the object that prevents its and its associated value from being garbage collected even if the key reference is becomes the sole reference. There are many use cases for such object identify based maps. A basic one is a memoizing cache. Assume that there is some expensive computation (for example a database access) based upon some object and you want to memoize the result of the computation. A weak map can be used for that purpose without create a memory leak. Allen > -Original Message- > From: es-discuss-boun...@mozilla.org [mailto:es-discuss- > boun...@mozilla.org] On Behalf Of P T Withington > Sent: Friday, October 29, 2010 8:34 AM > To: Peter van der Zee > Cc: es-discuss > Subject: Re: Usage for weak-maps > > On 2010-10-29, at 04:50, Peter van der Zee wrote: > > > What's the use case for weak maps? What would you do with it that > > currently impossible and why is the workaround (if any) problematic > > enough to warrant a weak map implementation? > > Another use case was mentioned in the "New topic regarding Proxies: > intercession for ===" thread. If you want to write a value type, the > constructor > needs to return the same object for identical parameters for === to work, > which > means it needs a table of all the objects it has ever made, but it doesn't > need (or > want) to hang on to objects that are no longer in use. [The alternative being > discussed in the proxy thread is to be able to customize === to give the same > illusion.] ___ > es-discuss mailing list > es-discuss@mozilla.org > https://mail.mozilla.org/listinfo/es-discuss ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Usage for weak-maps
On 2010-10-29, at 04:50, Peter van der Zee wrote: > What's the use case for weak maps? What would you do with it that currently > impossible and why is the workaround (if any) problematic enough to warrant > a weak map implementation? Another use case was mentioned in the "New topic regarding Proxies: intercession for ===" thread. If you want to write a value type, the constructor needs to return the same object for identical parameters for === to work, which means it needs a table of all the objects it has ever made, but it doesn't need (or want) to hang on to objects that are no longer in use. [The alternative being discussed in the proxy thread is to be able to customize === to give the same illusion.] ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Usage for weak-maps
On 10/29/10 4:50 AM, Peter van der Zee wrote: What's the use case for weak maps? What would you do with it that currently impossible and why is the workaround (if any) problematic enough to warrant a weak map implementation? One case this came up recently was for Firebug, Firebug wants to cache information about DOM Windows (the set of scripts compiled for that window, etc) and map from a Window to the information, but it obviously doesn't want to change the state of the Windows by adding properties, nor does it want to leak every single Window object. There is no in-language workaround for this use case; we solved it in Gecko by adding special out-of-band API that assigns a numeric ID to each window and exposes that ID on an object that is not the window itself and which content doesn't have access to, so that Firebug can use the IDs as the keys in its map. The use case generalizes, of course, to any situation where you want to store information for/about objects without sticking it on the object itself. -Boris ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Usage for weak-maps
What's the use case for weak maps? What would you do with it that currently impossible and why is the workaround (if any) problematic enough to warrant a weak map implementation? There's been quite a bit of discussion because of it. Especially in the area of covert channels and garbage collection. Yet I've missed the major cases for wanting it in the language in the first place. Google doesn't seem to help me much here. I don't need an in depth explanation of what weak maps are, just practical reasons for wanting it opposed to what's already possible... thanks :) - peter ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss