Feature-Request: allow to iterate over WeakSet / WeakMap
Michael Kriegel
michael.kriegel at actifsource.com
Wed Jul 13 14:18:57 UTC 2016
I see the points in that thread, but I am not sure, whether I completely
understand them. I can imagine, that there is an issue, because gc is
somewhat unpredictable. But shouldn't it be predictable, whether there
are still "non-weak" references to an object? So my intention slightly
differs from what WeakSet/WeakMap (and WeakReference would) do: whether
an object is in the SuperWeakSet (or a key in the SuperWeakMap, or a
SuperWeakReference still points to the object) does NOT depend on,
whether the object has actually been gc'ed, but whether the object does
have "non-weak" references to it.
I am not sure, which kinds of garbage collectors the implementations of
ECMAScript typically use. If they used a reference counter, which counts
"non-weak" references only, a simple check, whether the reference count
is 0 or not on access to a weak reference would be sufficient.
Of course, when I iterate through a WeakSet (respectively the keys of a
WeakMap), in the beginning of the iteration, a keys-array needs to be
retrieved, which is an array of all objects in the weak set, which have
reference count > 0 at that point in time. As this array then holds hard
references to all key objects, the key array cannot be changed by gc
during iteration.
On 13.07.2016 15:14, Bradley Meck wrote:
> This thread from 2011 might help understand why they should not be
> enumerable (it links to another thread as well inside).
> https://mail.mozilla.org/pipermail/es-discuss/2011-April/013604.html
>
> On Wed, Jul 13, 2016 at 8:08 AM, Isiah Meadows <isiahmeadows at gmail.com
> <mailto:isiahmeadows at gmail.com>> wrote:
>
> Could you explain a real world use case for this? I recall some
> that it would've simplified some, but the best implementation
> often didn't even require iteration, but just changing the data
> layout altogether.
>
>
> On Wed, Jul 13, 2016, 08:13 Michael Kriegel
> <michael.kriegel at actifsource.com
> <mailto:michael.kriegel at actifsource.com>> wrote:
>
> Hi everyone,
>
> not sure, whether this is the right place for discussing a feature
> request - and how to find out, whether this was already
> proposed/discussed before and with which result... My google
> search did
> not bring up anything about it.
>
> On my opinion it should be made possible to iterate over all
> elements in
> a WeakSet & WeakMap. I already found many cases, in which I'd
> like to
> have used the neat feature of "garbage collect the object, if
> it is not
> referred to by others anymore", but with the necessity to
> know, which
> objects are in the set (not only, if a given object is). An
> example:
>
> Registering callbacks to objects:
>
> class Foo {
> onBla(Handler) {
> this.BlaSuperWeakMap.push(Handler);
> }
>
> someOtherMethod() {
> for (let X in this.BlaSuperWeakMap) {
> X();
> }
> }
>
> constructor() {
> this.BlaSuperWeakMap = new SuperWeakMap();
> }
> }
>
> const MyFoo = new Foo();
> {
> let MyHandler1 = function(){
> console.log('MyHandler1 called');
> };
> MyFoo.onBla(MyHandler1);
> console.log('Invokation1:');
> MyFoo.someOtherMethod();
> }
> let MyHandler2 = function(){
> console.log('MyHandler2 called');
> };
> MyFoo.onBla(MyHandler2);
> MyFoo.onBla(function(){
> console.log('MyHandler3 called');
> });
> console.log('Invokation2:');
> MyFoo.someOtherMethod();
> MyHandler2 = undefined;
> console.log('Invokation3:');
> MyFoo.someOtherMethod();
>
> Expected result:
> Invokation1:
> MyHandler1 called
> Invokation2:
> MyHandler2 called
> Invokation3:
>
> Basically the example is: Invoke the callback of all objects
> which still
> "exist" (in the meaning of having "non-weak" references
> towards them).
>
> I know this may have some caveats. It would be necessary to
> check the
> reference count of the object in the SuperWeakSet, because the
> object
> may have no "non-weak" references left but still was not yet
> garbage
> collected. I do not know, whether current
> ECMAScript-Implementations and
> their garbage collecting solutions could handle this easily -
> basically
> it is the same decision, which the garbage collector would
> make: If an
> object can be disposed, because it has no non-Weak references
> pointing
> to it, the object is by definition not in the WeakSet anymore. And
> implementations could in that case even give a hint to the garbage
> collector, which may improve performance of applications,
> which use the
> SuperWeakSet alot.
>
> Greetings, Michael
>
>
>
>
>
>
>
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org <mailto:es-discuss at mozilla.org>
> https://mail.mozilla.org/listinfo/es-discuss
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org <mailto:es-discuss at mozilla.org>
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20160713/9d18220f/attachment-0001.html>
More information about the es-discuss
mailing list