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

Make [SameObject] beneficial #212

Open
annevk opened this issue Oct 27, 2016 · 9 comments
Open

Make [SameObject] beneficial #212

annevk opened this issue Oct 27, 2016 · 9 comments

Comments

@annevk
Copy link
Member

annevk commented Oct 27, 2016

I believe the contract with [SameObject] is supposed to be that whatever is returned the first time cannot ever change, right?

Currently specifications have to maintain that invariant and add the [SameObject] extended attribute as documentation.

However, what IDL could do instead is to allocate an internal slot when such an attribute is used. IDL's algorithm for the getter could then be to return the value from the internal slot when it's set, and when it's unset run the specification algorithm to get a value, set the internal slot to that value, and return that value.

That way the specification does not have to define how the value is stored, just how it's computed/retrieved.

@annevk

This comment was marked as outdated.

@annevk
Copy link
Member Author

annevk commented Nov 9, 2016

Note that changing this will affect specifications. @bzbarsky collected a list that will need to be updated after the change: #211 (comment). Seems doable.

@annevk
Copy link
Member Author

annevk commented Aug 23, 2017

Unlike [SameObject] I think we should allow this new [Cached] (or whatever we call it) extended attribute to be used for getters that return promises. That a non-cached value is returned when this is wrong seems fine.

@bzbarsky
Copy link
Collaborator

Yes, I agree.

@inexorabletash
Copy link
Member

+1 to [Cached] - it's basically what we do in Blink, and end up writing [SameObject, SaveSameObject] to enable autogenerated caching.

Aside: I'd love to put [SameObject] on IDBObjectStore and IDBIndex's keyPath attributes, but they can produce string or Array-of-string results. So if we can drop the restriction requiring the type to be object or an Interface, I'd like that too.

@bzbarsky
Copy link
Collaborator

Yeah, so what Gecko has is:

  • [Cached] to indicate that autogenerated caching should happen. In addition to that, attributes marked [Cached] are allowed to have sequence, dictionary, and record types, because the reason for not allowing attributes with those types (creation of a new object on each get) does not apply in the [Cached] case. Also, we check that [NewObject] and [Cached] are not used together, because that's nonsensical.
  • [SameObject] as defined in the spec, but with the additional implication that there are no side-effects from the get.
  • [Constant] which is just like [SameObject] but without type restrictions, so you can use it on any return type. Again, implies no side-effects from the get.
  • [Pure] which promises no side-effects but not constancy of return value. That said, two consecutive calls to a [Pure] thing will return the same value each time (because the first call can't affect the return value of the second call: see no side-effects), so [Cached] is allowed on [Pure] things, not just [Constant]/[SameObject] ones. There's a way to invalidate the cache if something that does have side-effects changes the value involved.

@jakearchibald
Copy link
Contributor

jakearchibald commented Feb 15, 2023

I think it might make specs a bit hard to read, as the prose may look like it's returning a different object every time, but a sneaky bit in the IDL is changing the behaviour.

I'd rather this feature was delivered via some prose defined in infra, like: "The memoized getter steps for foo return…"

There can be an idl comment for it too, of course.

@annevk
Copy link
Member Author

annevk commented Feb 15, 2023

Makes sense. I think we need both. [Cached] results in Web IDL defining an internal slot and "getter steps" for the object and those "getter steps" would delegate to "cached getter steps" defined by the specification.

@jakearchibald
Copy link
Contributor

WFM. Fwiw, I think "memoize" is a more accurate and well-known term when it comes to storing the result of an algorithm & returning it on future calls https://en.wikipedia.org/wiki/Memoization. But, 'cached' is fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants