dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.04k stars 1.55k forks source link

Feature Request: WeakMap #5144

Open DartBot opened 11 years ago

DartBot commented 11 years ago

This issue was originally filed by @pjako


I would like to have weak references in dart, I know its hard to do in javascript, currently at least.

Currently I want to give an object some uniqueID and track it in a Map without preventing the object to get garbage collected.

DartBot commented 11 years ago

This comment was originally written by @seaneagan


I think that's what Expando is for:

http://api.dartlang.org/docs/continuous/dart_core/Expando.html

but I could be wrong. I assume it will be implemented with JS WeakMap or a polyfill thereof where necessary:

http://wiki.ecmascript.org/doku.php?id=harmony:weak_maps

sethladd commented 11 years ago

Removed Type-Defect label. Added Type-Enhancement, Area-Library, Triaged labels.

DartBot commented 11 years ago

This comment was originally written by @seaneagan


AFAIK, Expando in dart2js is not yet weak. Here is a polyfill for JS WeakMaps which could be used to fix this:

https://code.google.com/p/es-lab/source/browse/trunk/src/ses/WeakMap.js

kasperl commented 11 years ago

Expando in dart2js is "weak" in the sense that adding an expando property to an object (the key) will not keep the key object alive on its own. The key will keep the value (whatever the expando property refers to) alive.

Without having spent too much time looking at it, I think we're basically doing the same thing as the polyfill you refer to (we store an extra hidden property on the key).

DartBot commented 11 years ago

This comment was originally written by @seaneagan


Cool. According to the polyfill docs, this approach still can have leaks (but maybe dart2js avoids this?), so using JS WeakMap when available could still be advantageous.

lrhn commented 11 years ago

I guess the leak is that if the Expando object dies, the properties on the objects will not be removed, which will keep the value alive. This is where it fails to implement an ephemeron table or a proper weak map.

sethladd commented 9 years ago

Is expando in dart2js weak these days?


cc @floitschG.

floitschG commented 9 years ago

As weak as ever. Nothing changed since Kasper's response in May 2013.

sethladd commented 9 years ago

Current browser support for WeakMap: http://kangax.github.io/compat-table/es6/#WeakMap

floitschG commented 9 years ago

Although IE11 has only 2/5, it looks like the 3 points are not that important (at least after a fast glance). Once we drop support for IE10 we should look at weakmaps.

zoechi commented 8 years ago

Almost a year, time for a ping.

ScottPierce commented 7 years ago

Weak references in the dart vm for Flutter would be useful.

lrhn commented 7 years ago

A "weak reference" is very different from a weak map. The weak map doesn't expose garbage collection at all, it just optimizes around it (not keeping entries that you can't access any more). A weak reference actually changes behavior depending on garbage collection. You could say that the weak reference breaks the abstraction that garbage collection may or may not happen behind the scenes.

That said, this issue is a request for a weak map, and Expando is the answer to that request. When compiled to JS, it is backed by a WeakMap if that is available (and in most modern browsers, it is available, at least with the basic features needed here).

marcglasberg commented 4 years ago

Just for reference, I've publish this package: https://pub.dev/packages/weak_map

jacob314 commented 2 years ago

@marcglasberg, now that Dart is supporting WeakRef and Finalizer, it would be great to release a new version of the weak_map package that uses WeakRef and Finalizer to support iterable keys and values. https://github.com/dart-lang/sdk/issues/47772

jacob314 commented 2 years ago

Fyi @polina-c who may be interested in helping implement this.

henrichen commented 3 months ago

@jacob314 here is my implementation of WeakHashMap that use WeakReference and Finalizer. https://pub.dev/packages/weak