You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Custom elements disconnected from a document should not be upgraded #419
The current specification says, in section 6, that:
Each registry has an associated upgrade candidates map of all instances of unresolved elements, mapping a custom element type to a sorted element queue. It is is initially empty.
Whenever an unresolved element is created, it must be added to the respective sorted element queue in upgrade candidates map.
This results in the following code to keep accumulating the list of unresolved elements in the heap:
I don't think this is an acceptable behavior. I don't think any custom element created prior to its definition getting loaded and not inserted into a document should be upgraded. In particular, any element that's not even referenced by JS and not in any node tree shouldn't be kept alive just for the sole purpose of being upgraded.
This also results in rather unexpected behavior of unresolved custom elements inside template content and document without a browsing context getting upgraded. THAT should at least be avoided.
This also results in rather unexpected behavior of unresolved custom elements inside template content and document without a browsing context getting upgraded. THAT should at least be avoided.
That is a different issue and is about what documents the registry works for.
As for this issue, it seems reasonable that the upgrade candidates map holds weak references to the elements. That is, if they are only referenced from there, they should just be collected and not instantiated.
As for this issue, it seems reasonable that the upgrade candidates map holds weak references to the elements. That is, if they are only referenced from there, they should just be collected and not instantiated.
I initially considered that approach but that could expose GC behavior. For example, WebKit currently implements stack-scanning conservative generational GC. It would mean that if the stack happens to contain a value that matches the node's memory address or if the major GC (non-eden collection) doesn't happen in time before the end of nano-task occurs, we may end up instantiating custom elements that would otherwise be dead in other browsers.
Can't we figure out by the time we invoke the callback whether anyone but "us" holds a reference to the object? That seems like the kind of deterministic check we'd need here.
We can't because JSC/WebKit uses stack-scanning conservative GC. It's literally impossible for GC to deterministically tell us whether a given node is definitely alive or not.
Note that this is not a problem unique to WebKit. Blink's new memory collector, Oilpan, uses conservative stack-scanning GC as well so they won't be able to implement such a deterministic check unless they modify their GC either.
Nodes inserted into the document that matches their registry, at the time of upgrade, will be upgraded.
Will nodes inserted past the time of upgrade, but created before the time of upgrade, still be upgraded, but only upon being inserted? I think if we don't do this we create race conditions.
It seems a little easier to just upgrade all of them and assume developers won't allocate a million of custom elements they don't intend to use.
Yeah, that's the proposal. The problem is that the current spec says that we need to treat any element with - as a custom element regardless of whether the author has shown the intent to use custom elements or not. As such, any existing content that creates a lot of temporary elements with - in its name and never call defineElement would leak all those elements indefinitely.
Perhaps once the "load" event has fired we should clear the "upgrade candidates map" or some such? And require all elements to be defined from that point on? I wonder what @esprehn@domenic et al think.
I would really prefer not to introduce this kind of inconsistency by requiring document-insertion for upgrade. Developers do complicated and interesting things with elements before inserting them into documents, as part of libraries and such. They shouldn't need to do some kind of insert-then-remove dance to get their elements to have the correct prototype and behavior.
I would prefer to keep the list of unresolved elements as the spec currently has them. It could be made a weak set in the spec, and implementers can either implement that immediately or will wait until they see problems in practice caused by lots of unused un-upgraded elements. I know Blink has implemented such things in the past with just weak references without a problem (we do that for the unhandled promises weak set).
It could be made a weak set in the spec, and implementers can either implement that immediately or will wait until they see problems in practice caused by lots of unused un-upgraded elements.
As I mentioned in #419 (comment), that kind of optimization will be observably non-interoperable between browsers.
@rniwa you must be misunderstanding me. I am referring to holding a weak reference in an unobservable way, which is certainly possible in Blink with Oilpan. It might not be possible with your GC, which is fine; it just means you cannot implement this optimization.
Maybe @dglazkov or @hayatoito or someone can give us background on why Blink/the current spec decided not to clone shadow trees, and make us feel better about that decision?
Since the most common use case of shadow DOM is in conjunction with custom elements, it should work best in that use case.
Now, I don't think we can check whether the newly created element has shadow DOM or not because we're going to upgrade those elements right before returning to author scripts. If anything, it needs to be keyed off of element names (i.e. whether the cloned node's name has - in it or not).
I've taken care of the majority of this in recent commits. However I haven't added a customElements.upgrade API yet, and have noticed some other issues with upgrades which I'll open a separate issue for.
Activity
rniwa commentedon Mar 7, 2016
@annevk @travisleithead @dglazkov @domenic
rniwa commentedon Mar 7, 2016
This also results in rather unexpected behavior of unresolved custom elements inside template content and document without a browsing context getting upgraded. THAT should at least be avoided.
annevk commentedon Mar 7, 2016
That is a different issue and is about what documents the registry works for.
As for this issue, it seems reasonable that the upgrade candidates map holds weak references to the elements. That is, if they are only referenced from there, they should just be collected and not instantiated.
rniwa commentedon Mar 7, 2016
I initially considered that approach but that could expose GC behavior. For example, WebKit currently implements stack-scanning conservative generational GC. It would mean that if the stack happens to contain a value that matches the node's memory address or if the major GC (non-eden collection) doesn't happen in time before the end of nano-task occurs, we may end up instantiating custom elements that would otherwise be dead in other browsers.
annevk commentedon Mar 7, 2016
Can't we figure out by the time we invoke the callback whether anyone but "us" holds a reference to the object? That seems like the kind of deterministic check we'd need here.
rniwa commentedon Mar 7, 2016
We can't because JSC/WebKit uses stack-scanning conservative GC. It's literally impossible for GC to deterministically tell us whether a given node is definitely alive or not.
Note that this is not a problem unique to WebKit. Blink's new memory collector, Oilpan, uses conservative stack-scanning GC as well so they won't be able to implement such a deterministic check unless they modify their GC either.
annevk commentedon Mar 7, 2016
So let me try to understand the proposal:
It seems a little easier to just upgrade all of them and assume developers won't allocate a million of custom elements they don't intend to use.
rniwa commentedon Mar 7, 2016
Yeah, that's the proposal. The problem is that the current spec says that we need to treat any element with
-as a custom element regardless of whether the author has shown the intent to use custom elements or not. As such, any existing content that creates a lot of temporary elements with-in its name and never calldefineElementwould leak all those elements indefinitely.annevk commentedon Mar 7, 2016
Perhaps once the "load" event has fired we should clear the "upgrade candidates map" or some such? And require all elements to be defined from that point on? I wonder what @esprehn @domenic et al think.
domenic commentedon Mar 7, 2016
I would really prefer not to introduce this kind of inconsistency by requiring document-insertion for upgrade. Developers do complicated and interesting things with elements before inserting them into documents, as part of libraries and such. They shouldn't need to do some kind of insert-then-remove dance to get their elements to have the correct prototype and behavior.
I would prefer to keep the list of unresolved elements as the spec currently has them. It could be made a weak set in the spec, and implementers can either implement that immediately or will wait until they see problems in practice caused by lots of unused un-upgraded elements. I know Blink has implemented such things in the past with just weak references without a problem (we do that for the unhandled promises weak set).
dglazkov commentedon Mar 7, 2016
Generally agreeing with @domenic here. I understand the concern though. It's just tree-connected concept seems like the wrong tool for the job.
rniwa commentedon Mar 7, 2016
As I mentioned in #419 (comment), that kind of optimization will be observably non-interoperable between browsers.
domenic commentedon Mar 7, 2016
@rniwa you must be misunderstanding me. I am referring to holding a weak reference in an unobservable way, which is certainly possible in Blink with Oilpan. It might not be possible with your GC, which is fine; it just means you cannot implement this optimization.
33 remaining items
domenic commentedon Mar 16, 2016
That could work, but is probably a bit too magic.
Maybe @dglazkov or @hayatoito or someone can give us background on why Blink/the current spec decided not to clone shadow trees, and make us feel better about that decision?
rniwa commentedon Mar 16, 2016
Since the most common use case of shadow DOM is in conjunction with custom elements, it should work best in that use case.
Now, I don't think we can check whether the newly created element has shadow DOM or not because we're going to upgrade those elements right before returning to author scripts. If anything, it needs to be keyed off of element names (i.e. whether the cloned node's name has
-in it or not).dglazkov commentedon Mar 16, 2016
This will take a while to page in, but I distinctly remember https://bugs.webkit.org/show_bug.cgi?id=61997 as being one of the first places we've discovered that cloning shadow trees along with the tree is fraught with peril.
Remove auto-upgrading of custom elements
Perform upgrades in tree order on insertion
domenic commentedon Mar 21, 2016
I've taken care of the majority of this in recent commits. However I haven't added a
customElements.upgradeAPI yet, and have noticed some other issues with upgrades which I'll open a separate issue for.Move upgrade example to the intro, and add an edge-case example
domenic commentedon Mar 24, 2016
Since nobody besides me seems enthusiastic about
customElements.upgrade(), we can leave it as a v2 feature. I'll close this now. I've added an example illustrating the edge-cases around upgrades and in-document-ness to https://w3c.github.io/webcomponents/spec/custom/#custom-elements-upgrades-examples.Make custom element definition trigger in-document upgrades
Make custom element definition trigger in-document upgrades
Make custom element definition trigger in-document upgrades
Make custom element definition trigger in-document upgrades
Make custom element definition trigger in-document upgrades
defineElement should upgrade existing unresolved custom elements