zenparsing / proposal-virtual-properties

0 stars 0 forks source link

How would an implementation optimize object allocation here? #1

Open dead-claudia opened 6 years ago

dead-claudia commented 6 years ago

It'd be very inefficient for an object to simply delegate to the weak map on each reference access, so I'm curious how an implementation might choose to elide that with the help of inline caches.

My initial idea is this:

zenparsing commented 6 years ago

That sounds reasonable.

An implementation can notice that the WeakMap representing the private data will outlive all of the instances of the class with which it is associated, and simply store the private data with the object itself.

Of course, the @@referenceGet and @@referenceSet methods could be overridden so an engine will have to protect against those scenarios, but that's just how JavaScript works.

dead-claudia commented 6 years ago

I just wanted to establish whether it'd create optimization issues first before lending my support to it. I'd rather not have slow private slots, and I know implementations would be hesitant to implement something like this that they can't optimize away in a common case, especially when it's not really solving very many problems. My curiosity is whether it's possible for a map to do something similar, just using a set internally for objects with slots:

But as it stands, I kind of like your proposal here. It's not far off of mine, just it's a bit more dynamic. It even permits easy privacy control, without having to concern yourself about super/etc.