Weak References

Mark S. Miller erights at google.com
Sat Sep 5 17:41:26 UTC 2015


On Sat, Sep 5, 2015 at 4:07 AM, Isiah Meadows <isiahmeadows at gmail.com>
wrote:

> I'm resurrecting this [1] because I have found a use case where I needed a
> weak reference. Is there any chance this could get into the language?
>

I wrote the [1] strawman and continue to be positive on the idea. The
strawman text is old and needs revision, as well as reconciliation with the
separately written strawman at <
http://wiki.ecmascript.org/doku.php?id=strawman:weak_refs>. Together these
capture the needed abstract idea, but are not necessarily the best API.
FWIW, the ParcPlaceSmalltalk API helped amortize some costs by providing a
weak-array as the primitive, where notification includes the array index.
(If someone has a link for that API, please post.)


I'll note that the pattern in your code:

        if (ref.exists()) setStream(ref.get(), format)

would not work on any previous weak-reference system, since the designated
object might be collected, and `ref` nulled, between your `ref.exists()`
and your `ref.get()`. It would happen to work in the strawman because
observable gc events only happen between turns/jobs. Nevertheless, frankly,
I don't like this pattern and would recommend instead:

      const strong = ref.get();
      if (strong !== undefined) { setStream(strong, format); }

On the flip side, despite my recommendation, we should certainly expect
that many people will independently use your pattern. The fact that the
strawman's turn atomicity will happen to make this code more reliable does
corroborate the attractiveness of turn atomicity. I mention this because
turn atomicity was a controversial aspect of this strawman.


To answer your question, I think there is a *good* chance that we could get
this into the language if someone else would champion it. Although I am
positive, given the scarcity of my time and where this idea falls on my
priority queue, I would not find the time to be its champion. If someone
else does step up as champion, I would certainly help.

Volunteers?



>
> Here's my particular use case: making a stream with a rotating destination
> that itself acts as a stream (snippet of that code, or what I'd like it to
> be).
>
> ```js
> function rotateStream(interval, format) {
>     // Without the weak reference here, there's a memory leak
>     const ref = new WeakReference({})
>     setStream(ref.get(), format)
>     setInterval(function () {
>         if (ref.exists()) setStream(ref.get(), format)
>         else clearInterval(this)
>     }, interval)
>     return ref.get()
> }
> ```
>
> This basically rotates an active stream, with the weak reference pointing
> to the said stream. The alternative requires explicit marking (in this
> case, with a `close` method):
>
> ```js
> function leakyRotateStream(interval, format) {
>     let stream = {close() {
>         clearInterval(timer)
>         timer = stream = null
>     }}
>     const timer = setInterval(function () {
>         setStream(stream, format)
>     }, interval)
>     setStream(stream, format)
>     return stream
> }
> ```
>
> In this use case, weak references would simplify the implementation and
> reduce memory costs. And I would rather take advantage of the GC machinery
> than explicit memory management (as explicit as C), as it would be easier
> to collect when that's the only thing referenced.
>
> (The setInterval callback is gc'd with all its references when it's
> cleared from within.)
>
> Thankfully, I'm using Node.js, so `weak` [2] is an option (the only
> option, really). But I would definitely appreciate if it was available in
> JS proper, across engines. V8, SpiderMonkey, and JSC all have weak
> references to JS objects as part of their public API, and I would be
> surprised if the implementation work would be anything significant.
>
> [1]: https://esdiscuss.org/topic/what-is-the-status-of-weak-references
> [2]: https://github.com/TooTallNate/node-weak
>
> --
> Isiah Meadows
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>


-- 
    Cheers,
    --MarkM
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150905/a46215e0/attachment.html>


More information about the es-discuss mailing list