Bug 97644 - Attr.ownerDocument should change if its parent's owner did
Summary: Attr.ownerDocument should change if its parent's owner did
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: DOM (show other bugs)
Version: 528+ (Nightly build)
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Hajime Morrita
URL: http://jsfiddle.net/fcy5u/1/
Keywords:
Depends on:
Blocks:
 
Reported: 2012-09-26 01:28 PDT by Hajime Morrita
Modified: 2013-01-16 20:24 PST (History)
9 users (show)

See Also:


Attachments
Patch (6.71 KB, patch)
2012-09-26 01:31 PDT, Hajime Morrita
no flags Details | Formatted Diff | Diff
Patch for landing (5.42 KB, patch)
2013-01-16 18:12 PST, Hajime Morrita
no flags Details | Formatted Diff | Diff
Patch for landing (5.41 KB, patch)
2013-01-16 19:39 PST, Hajime Morrita
webkit.review.bot: commit-queue-
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Hajime Morrita 2012-09-26 01:28:17 PDT
Currently Attr.ownerDocument doesn't change even if its holding element's owner changed.
Comment 1 Hajime Morrita 2012-09-26 01:31:44 PDT
Created attachment 165749 [details]
Patch
Comment 2 Alexey Proskuryakov 2012-09-27 11:05:19 PDT
This appears to slow down moving elements just to make a very rare operation correct. Would it be possible to change Attr.ownerDocument implementation instead, so that it would get the owner document from element?
Comment 3 Ryosuke Niwa 2012-09-27 16:27:15 PDT
(In reply to comment #2)
> This appears to slow down moving elements just to make a very rare operation correct. Would it be possible to change Attr.ownerDocument implementation instead, so that it would get the owner document from element?

Unfortunately, ownerDocument() is implemented in Node so that doesn't work. On the other hand, I don't expect Attr to exist on most elements so the perf. impact should be minimal.
Comment 4 Ryosuke Niwa 2012-09-27 16:31:04 PDT
Comment on attachment 165749 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=165749&action=review

> Source/WebCore/dom/TreeScopeAdopter.cpp:65
> +            Vector<Attr*> attrs;

Do we really need to have a local Vector for this? Attr can only have character data & entities, no?
Comment 5 Hajime Morrita 2012-09-27 18:13:42 PDT
Comment on attachment 165749 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=165749&action=review

>> Source/WebCore/dom/TreeScopeAdopter.cpp:65
>> +            Vector<Attr*> attrs;
> 
> Do we really need to have a local Vector for this? Attr can only have character data & entities, no?

It doesn't matter which item Attr can have. This Attr list is owned by Element and getExistingAttrs() grubs it.
We could just return AttrList, which is defined locally in ElementAttributeData.cpp. But it looks we want to hide the implementation detail
so I'm doing like this.
Comment 6 Hajime Morrita 2012-09-27 18:20:28 PDT
(In reply to comment #3)
> (In reply to comment #2)
> > This appears to slow down moving elements just to make a very rare operation correct. Would it be possible to change Attr.ownerDocument implementation instead, so that it would get the owner document from element?
> 
> Unfortunately, ownerDocument() is implemented in Node so that doesn't work. On the other hand, I don't expect Attr to exist on most elements so the perf. impact should be minimal.

Yeah, good news is that this doesn't happen in many cases.
This path is hit only when the node is moved between document 
(TreeScope in precise.) Moving XHR.responseXML to the host document 
would be almost only relevant usecase in the real world, whose performance
regression, if any, will be hidden behind network latency.
Comment 7 Ryosuke Niwa 2012-09-27 18:51:48 PDT
Comment on attachment 165749 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=165749&action=review

>>> Source/WebCore/dom/TreeScopeAdopter.cpp:65
>>> +            Vector<Attr*> attrs;
>> 
>> Do we really need to have a local Vector for this? Attr can only have character data & entities, no?
> 
> It doesn't matter which item Attr can have. This Attr list is owned by Element and getExistingAttrs() grubs it.
> We could just return AttrList, which is defined locally in ElementAttributeData.cpp. But it looks we want to hide the implementation detail
> so I'm doing like this.

IMO, encapsulation is not a good enough reason to allocate another Vector.
We should just retrieve const Vector<RefPtr<Attr> >& instead.
anttik or kling might have a strong opinion here.
Comment 8 Ryosuke Niwa 2012-09-27 18:54:05 PDT
Let me phrase it differently. The fact we have a Vector<RefPtr<Attr> > in ElementAttributeData doesn't seem like something we need to hide at the cost of cloning a heap-allocated object.
Comment 9 Andreas Kling 2012-09-27 19:03:26 PDT
(In reply to comment #7)
> (From update of attachment 165749 [details])
> View in context: https://bugs.webkit.org/attachment.cgi?id=165749&action=review
> 
> >>> Source/WebCore/dom/TreeScopeAdopter.cpp:65
> >>> +            Vector<Attr*> attrs;
> >> 
> >> Do we really need to have a local Vector for this? Attr can only have character data & entities, no?
> > 
> > It doesn't matter which item Attr can have. This Attr list is owned by Element and getExistingAttrs() grubs it.
> > We could just return AttrList, which is defined locally in ElementAttributeData.cpp. But it looks we want to hide the implementation detail
> > so I'm doing like this.
> 
> IMO, encapsulation is not a good enough reason to allocate another Vector.
> We should just retrieve const Vector<RefPtr<Attr> >& instead.
> anttik or kling might have a strong opinion here.

No strong opinion, though I guess I'd prefer if we can avoid spreading the implementation details of Attr too much. The API is not part of DOM4, and sees very little use on the current web.
Comment 10 Alexey Proskuryakov 2012-09-28 09:36:22 PDT
It may be worth sending a note to webkit-dev to solicit thoughts about performance.
Comment 11 Darin Adler 2013-01-16 13:12:12 PST
Comment on attachment 165749 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=165749&action=review

> Source/WebCore/dom/TreeScopeAdopter.cpp:51
>      if (oldDocument && willMoveToNewDocument)
>          oldDocument->incDOMTreeVersion();

One thing I don’t like about this TreeScopeAdopter factoring (not new to this patch) is that this one call to incDOMTreeVersion is now here in this file. All the others are in core files like Element.cpp yet this one stray one is out here in the “hinterlands”. Yuck.

>>>>> Source/WebCore/dom/TreeScopeAdopter.cpp:65
>>>>> +            Vector<Attr*> attrs;
>>>> 
>>>> Do we really need to have a local Vector for this? Attr can only have character data & entities, no?
>>> 
>>> It doesn't matter which item Attr can have. This Attr list is owned by Element and getExistingAttrs() grubs it.
>>> We could just return AttrList, which is defined locally in ElementAttributeData.cpp. But it looks we want to hide the implementation detail
>>> so I'm doing like this.
>> 
>> IMO, encapsulation is not a good enough reason to allocate another Vector.
>> We should just retrieve const Vector<RefPtr<Attr> >& instead.
>> anttik or kling might have a strong opinion here.
> 
> No strong opinion, though I guess I'd prefer if we can avoid spreading the implementation details of Attr too much. The API is not part of DOM4, and sees very little use on the current web.

It does seem gratuitous to copy the vector just for a slightly larger theoretical level of encapsulation. I agree with Ryosuke that returning a "const Vector<RefPtr<Attr> >&" from an existingAttrs function would be fine. It exposes implementation detail, but in a harmless way that is easy to change later.

If the encapsulation really is an issue, then one way to keep tighter encapsulation without copying would be to expose a function, perhaps a template, that applies a passed-in function object to all attrs, then pass it an an object that does this->moveTreeToNewScope(x).

But moving a Node between documents that has some associated Attr should be rare enough that I suppose any of these designs would be OK; the performance should not be critical.

I wonder how far we are from being able to remove the Attr feature from our engine? That would be the best solution, by far!
Comment 12 Hajime Morrita 2013-01-16 18:11:09 PST
Comment on attachment 165749 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=165749&action=review

>>>>>> Source/WebCore/dom/TreeScopeAdopter.cpp:65
>>>>>> +            Vector<Attr*> attrs;
>>>>> 
>>>>> Do we really need to have a local Vector for this? Attr can only have character data & entities, no?
>>>> 
>>>> It doesn't matter which item Attr can have. This Attr list is owned by Element and getExistingAttrs() grubs it.
>>>> We could just return AttrList, which is defined locally in ElementAttributeData.cpp. But it looks we want to hide the implementation detail
>>>> so I'm doing like this.
>>> 
>>> IMO, encapsulation is not a good enough reason to allocate another Vector.
>>> We should just retrieve const Vector<RefPtr<Attr> >& instead.
>>> anttik or kling might have a strong opinion here.
>> 
>> No strong opinion, though I guess I'd prefer if we can avoid spreading the implementation details of Attr too much. The API is not part of DOM4, and sees very little use on the current web.
> 
> It does seem gratuitous to copy the vector just for a slightly larger theoretical level of encapsulation. I agree with Ryosuke that returning a "const Vector<RefPtr<Attr> >&" from an existingAttrs function would be fine. It exposes implementation detail, but in a harmless way that is easy to change later.
> 
> If the encapsulation really is an issue, then one way to keep tighter encapsulation without copying would be to expose a function, perhaps a template, that applies a passed-in function object to all attrs, then pass it an an object that does this->moveTreeToNewScope(x).
> 
> But moving a Node between documents that has some associated Attr should be rare enough that I suppose any of these designs would be OK; the performance should not be critical.
> 
> I wonder how far we are from being able to remove the Attr feature from our engine? That would be the best solution, by far!

OK, keeping encapsulation here looks overkill. I'll take what Ryosuke is suggesting.
Comment 13 Hajime Morrita 2013-01-16 18:12:21 PST
Created attachment 183080 [details]
Patch for landing
Comment 14 WebKit Review Bot 2013-01-16 18:42:16 PST
Comment on attachment 183080 [details]
Patch for landing

Rejecting attachment 183080 [details] from commit-queue.

Failed to run "['/mnt/git/webkit-commit-queue/Tools/Scripts/webkit-patch', '--status-host=queues.webkit.org', '-..." exit_code: 2 cwd: /mnt/git/webkit-commit-queue

Last 500 characters of output:
rce/WebKit/chromium/webkit/media/crypto/ppapi/cdm --revision 173055 --non-interactive --force --accept theirs-conflict --ignore-externals' in '/mnt/git/webkit-commit-queue/Source/WebKit/chromium'
56>At revision 173055.

________ running '/usr/bin/python tools/clang/scripts/update.py --mac-only' in '/mnt/git/webkit-commit-queue/Source/WebKit/chromium'

________ running '/usr/bin/python gyp_webkit' in '/mnt/git/webkit-commit-queue/Source/WebKit/chromium'
Updating webkit projects from gyp files...

Full output: http://queues.webkit.org/results/15905711
Comment 15 Hajime Morrita 2013-01-16 19:39:14 PST
Created attachment 183102 [details]
Patch for landing
Comment 16 WebKit Review Bot 2013-01-16 20:15:01 PST
Comment on attachment 183102 [details]
Patch for landing

Rejecting attachment 183102 [details] from commit-queue.

Failed to run "['/mnt/git/webkit-commit-queue/Tools/Scripts/webkit-patch', '--status-host=queues.webkit.org', '-..." exit_code: 2 cwd: /mnt/git/webkit-commit-queue

Last 500 characters of output:
e/WebKit/chromium/third_party/skia/include/core/SkShader.h
50>U    /mnt/git/webkit-commit-queue/Source/WebKit/chromium/third_party/skia/include/core/SkTDArray.h
51>Updated to revision 7212.
50>Updated to revision 7212.

________ running '/usr/bin/python tools/clang/scripts/update.py --mac-only' in '/mnt/git/webkit-commit-queue/Source/WebKit/chromium'

________ running '/usr/bin/python gyp_webkit' in '/mnt/git/webkit-commit-queue/Source/WebKit/chromium'
Updating webkit projects from gyp files...

Full output: http://queues.webkit.org/results/15910732
Comment 17 Hajime Morrita 2013-01-16 20:16:41 PST
Hmm. cq looks being in trouble.
Comment 18 Hajime Morrita 2013-01-16 20:24:12 PST
Committed r139958: <http://trac.webkit.org/changeset/139958>