Bug 129990 - Avoid copy-prone idiom "for (auto item : collection)"
Summary: Avoid copy-prone idiom "for (auto item : collection)"
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebKit Misc. (show other bugs)
Version: 528+ (Nightly build)
Hardware: All All
: P2 Normal
Assignee: Darin Adler
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-03-08 22:58 PST by Darin Adler
Modified: 2014-03-23 13:43 PDT (History)
17 users (show)

See Also:


Attachments
Patch (37.57 KB, patch)
2014-03-08 23:08 PST, Darin Adler
no flags Details | Formatted Diff | Diff
Patch (37.58 KB, patch)
2014-03-10 10:27 PDT, Darin Adler
no flags Details | Formatted Diff | Diff
Patch (38.22 KB, patch)
2014-03-11 20:09 PDT, Darin Adler
ggaren: review+
ggaren: commit-queue+
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Darin Adler 2014-03-08 22:58:56 PST
Avoid copy-prone idiom "for (auto item : collection)"
Comment 1 Darin Adler 2014-03-08 23:08:58 PST
Created attachment 226253 [details]
Patch
Comment 2 Darin Adler 2014-03-10 10:27:57 PDT
Created attachment 226317 [details]
Patch
Comment 3 Darin Adler 2014-03-10 10:29:21 PDT
To find these I used this command:

% git grep "for (auto .*:"
Comment 4 Alexey Proskuryakov 2014-03-10 10:35:29 PDT
Comment on attachment 226317 [details]
Patch

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

> Source/JavaScriptCore/inspector/agents/InspectorDebuggerAgent.cpp:344
> -    for (auto breakpointID : breakpointIDs) {
> +    for (auto& breakpointID : m_breakpointIdentifierToDebugServerBreakpointIDs.take(breakpointIdentifier)) {

breakpointID is a size_t, so auto& isn't making the code better.

I'd much prefer an explicitly named type to auto here.
Comment 5 Darin Adler 2014-03-10 23:46:49 PDT
(In reply to comment #4)
> I'd much prefer an explicitly named type to auto here.

I’ll change it. But don’t you always prefer an explicitly named type?
Comment 6 Darin Adler 2014-03-11 20:07:17 PDT
(In reply to comment #4)
> > Source/JavaScriptCore/inspector/agents/InspectorDebuggerAgent.cpp:344
> > -    for (auto breakpointID : breakpointIDs) {
> > +    for (auto& breakpointID : m_breakpointIdentifierToDebugServerBreakpointIDs.take(breakpointIdentifier)) {
> 
> breakpointID is a size_t, so auto& isn't making the code better.

Right, but it also doesn’t make it any worse.
Comment 7 Darin Adler 2014-03-11 20:09:13 PDT
Created attachment 226468 [details]
Patch
Comment 8 Geoffrey Garen 2014-03-12 09:44:49 PDT
Comment on attachment 226468 [details]
Patch

r=me
Comment 9 Darin Adler 2014-03-13 10:52:27 PDT
Committed r165546: <http://trac.webkit.org/changeset/165546>
Comment 10 Antti Koivisto 2014-03-13 14:22:46 PDT
Comment on attachment 226468 [details]
Patch

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

> Source/WebCore/dom/Node.cpp:730
> -    HashSet<LiveNodeList*> liveNodeLists = std::move(m_listsInvalidatedAtDocument);
> -    for (auto it : liveNodeLists)
> -        it->invalidateCache(attrName);
> -
> -    HashSet<HTMLCollection*> collectionLists = std::move(m_collectionsInvalidatedAtDocument);
> -    for (auto it : collectionLists)
> -        it->invalidateCache(attrName);
> +    for (auto* list : std::move(m_listsInvalidatedAtDocument))
> +        list->invalidateCache(attrName);
> +    for (auto* list : std::move(m_collectionsInvalidatedAtDocument))
> +        list->invalidateCache(attrName);

Looks like this part is making many tests crash. I suppose the collection dies immediately after the move.
Comment 11 Antti Koivisto 2014-03-13 14:28:50 PDT
Reverted that part in https://trac.webkit.org/r165566
Comment 12 Jer Noble 2014-03-13 15:29:19 PDT
This seems to have caused 50+ tests to assert:

<http://build.webkit.org/results/Apple%20Mavericks%20Debug%20WK2%20(Tests)/r165564%20(3310)/results.html>
Comment 13 Jer Noble 2014-03-13 15:29:48 PDT
Oh, thanks Antti. :)
Comment 14 Darin Adler 2014-03-13 15:32:38 PDT
(In reply to comment #11)
> Reverted that part in https://trac.webkit.org/r165566

Thanks Antti. I would have used auto in the revert, but otherwise it’s exactly what I would do.
Comment 15 Ryosuke Niwa 2014-03-13 22:21:15 PDT
Comment on attachment 226468 [details]
Patch

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

>> Source/WebCore/dom/Node.cpp:730
>> +        list->invalidateCache(attrName);
> 
> Looks like this part is making many tests crash. I suppose the collection dies immediately after the move.

Yeah, the whole point of this code was to swap.
Comment 16 Darin Adler 2014-03-14 15:22:57 PDT
(In reply to comment #15)
> Yeah, the whole point of this code was to swap.

No, the point of the code was to move. The swap is just a tricky way to do a move when C++ didn’t have swap.
Comment 17 Darin Adler 2014-03-14 15:23:29 PDT
when C++ didn’t have move.

Antti’s patch works around some problem related to doing a move on the collection in a for loop.
Comment 18 Ryosuke Niwa 2014-03-23 13:43:25 PDT
Comment on attachment 226468 [details]
Patch

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

>>> Source/WebCore/dom/Node.cpp:730
>>> +        list->invalidateCache(attrName);
>> 
>> Looks like this part is making many tests crash. I suppose the collection dies immediately after the move.
> 
> Yeah, the whole point of this code was to swap.

I had a chance to talk with someone work who works on clang,
and he clarified to me that std::move doesn't create a copy.
It simply casts & to &&, so the temporary is never created here.