Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: WeakMap #5144

Open
DartBot opened this issue Sep 13, 2012 · 16 comments
Open

Feature Request: WeakMap #5144

DartBot opened this issue Sep 13, 2012 · 16 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug

Comments

@DartBot
Copy link

DartBot commented Sep 13, 2012

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
Copy link
Author

DartBot commented Sep 13, 2012

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
Copy link
Contributor

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

@DartBot
Copy link
Author

DartBot commented May 30, 2013

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
Copy link

kasperl commented May 30, 2013

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
Copy link
Author

DartBot commented May 30, 2013

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
Copy link
Member

lrhn commented May 31, 2013

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
Copy link
Contributor

Is expando in dart2js weak these days?


cc @floitschG.

@floitschG
Copy link
Contributor

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

@sethladd
Copy link
Contributor

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

@floitschG
Copy link
Contributor

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.

@DartBot DartBot added Type-Enhancement area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. labels Feb 25, 2015
@zoechi
Copy link
Contributor

zoechi commented Jan 18, 2016

Almost a year, time for a ping.

lrhn added a commit that referenced this issue Jan 25, 2016
Retains the existing code as fallback for settings without WeakMap for now.

Fixes issue #5144

BUG= http://dartbug.com/5144
R=sigmund@google.com

Review URL: https://codereview.chromium.org/1599393003 .
@kevmoo kevmoo added P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug and removed triaged labels Feb 29, 2016
@ScottPierce
Copy link

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

@lrhn
Copy link
Member

lrhn commented Jan 17, 2017

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
Copy link

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

@jacob314
Copy link
Member

@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.
#47772

@jacob314
Copy link
Member

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

@jacob314 jacob314 reopened this Feb 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

10 participants