TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ephemerons explained

30 pointsby kencauseyalmost 3 years ago

5 comments

jlokieralmost 3 years ago
Modern JavaScript also supports ephemerons.<p>There it&#x27;s the WeakMap type: <a href="https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Reference&#x2F;Global_Objects&#x2F;WeakMap" rel="nofollow">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Refe...</a><p>WeakMap prevents observing when the weakly referenced key objects are garbage collected. It does this by being deliberately non-enumerable (see the MDN link). So it may be considered missing part of the definition of ephemerons. It has the weak-key part without the finalizer part.<p>But modern browsers support WeakRef and FinalizationRegistry, which eventually reveal when objects have been garbage collected. <a href="https:&#x2F;&#x2F;v8.dev&#x2F;features&#x2F;weak-references" rel="nofollow">https:&#x2F;&#x2F;v8.dev&#x2F;features&#x2F;weak-references</a>, <a href="https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Reference&#x2F;Global_Objects&#x2F;FinalizationRegistry" rel="nofollow">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Refe...</a> and <a href="https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Reference&#x2F;Global_Objects&#x2F;WeakRef" rel="nofollow">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Refe...</a><p>So this part of the WeakMap design is just an annoying obstacle which can be worked around.<p>(Note, as FinalizationRegistry is designed to accompany WeakRef, it&#x27;s intriguing that MDN lists Opera as supporting FinalizationRegistry but not WeakRef.)
sfinkalmost 3 years ago
That&#x27;s a complete but quite a long explanation, and talks in terms of Squeak which I&#x27;m unfamiliar with. I&#x27;m much more familiar with JavaScript WeakMaps, where I would probably describe it something like:<p>A WeakMap M is a table of entries, each of which maps a key K to a value V. If you have the M and K in hand, you can use them to look up V. (And if you don&#x27;t have either one, then you can&#x27;t!)<p>From a GC perspective, each entry is an edge that says that V is alive if both M and K are alive[1]. (And if either of them dies, then the entry no longer serves to keep anything alive. Note the parallelism with the above parenthetical.)<p>They&#x27;re mostly useful for associating a value V with some other object K without modifying K. They&#x27;re sort of an annotation mechanism. If you tried to do that with a regular Map, then you would have to manually remove any Ks that you wanted to go away.<p>In implementation terms, ...y&#x27;know, why don&#x27;t I write it up as a blog post: <a href="https:&#x2F;&#x2F;blog.mozilla.org&#x2F;sfink&#x2F;2022&#x2F;06&#x2F;09&#x2F;ephemeron-tables-aka-javascript-weakmaps&#x2F;" rel="nofollow">https:&#x2F;&#x2F;blog.mozilla.org&#x2F;sfink&#x2F;2022&#x2F;06&#x2F;09&#x2F;ephemeron-tables-a...</a><p>[1] Note that WeakMaps are not &quot;weak&quot; in the sense of weak references; the name WeakMap is a bit of a misnomer. Normal weak things can be skipped during marking. After liveness is determined, you&#x27;ll check to see if they&#x27;re still alive. You can&#x27;t skip ephemeron edges like this! They&#x27;re strong edges and it&#x27;s invalid to throw away a V if its M and K are live. But EphemeronTable <i>is</i> a bit long...
kwatsonafteralmost 3 years ago
I clicked on this thinking it would be about philosophy but I was delighted to be once again thrust into Squeakland. God bless you Dan Ingalls!
stevedekortealmost 3 years ago
Neat idea. Would moving the dependents list to an instance variable holding a weak array avoid the need for ephemerons here?
评论 #31684182 未加载
Diggseyalmost 3 years ago
AKA, a single entry in a WeakKeyDictionary
评论 #31684164 未加载