Mark S. Miller (2014-10-24T20:13:06.000Z)
d at domenic.me (2014-11-18T22:59:48.001Z)
On Fri, Oct 24, 2014 at 4:19 AM, Andreas Rossberg <rossberg at google.com> wrote: > I appreciate your analysis, but I respectfully disagree with your > conclusion. > > - The extra slot and indirection for the [[Shadow]] property is an > extra cost (V8 already has a very similar mechanism to implement > "hidden properties" as provided by its API (and in fact, hash codes), > but it is known to be (too) slow). > > - Optimising away this slot is difficult -- for starters, because it > will have various secondary effects (e.g., every add/remove of an > object to/from a weak map will potentially result in a layout change > for that object, increasing spurious polymorphism, and potentially > invalidating/deoptimising existing code). > > - Worse, when you flatten weak properties into objects, then even GC > could cause object layouts to change, which is a whole new dimension > of complexity. > Already answered: On Wed, Oct 22, 2014 at 2:26 PM, Mark Miller <erights at gmail.com> wrote: > [...] When the WeakMap is known to necessarily live longer than its keys, > as for class-private state, then those shadow properties can even be > exempted from the [ephemeron] key-mark-check. > More to the point, when the instance (transientKey) retains the WeakMap (via instance retains class retains methods retains field designators), then GC will never cause this layout change. Caveat: If the field (WeakMap) objects in question are available to user code, then they might be .clear()ed, in which case GC would indeed cause this layout change. This is one of many reasons why I still think .clear() should not be provided by WeakMap and WeakSet. The inclusion of Weak* .clear() never achieved consensus, so I believe it violates our process for these to be included in the spec. > - On the other hand, if you do _not_ do this flattening, performance > is unlikely to ever be competitive with true private properties (we > internally introduced private symbols in V8, exactly because the extra > indirection for hidden properties was too costly). > Exactly. For WeakMaps representing private state, they should engage exactly the kind of runtime machinery once associated with private symbols, but without breaking membrane transparency.