RESOLVED FIXED129990
Avoid copy-prone idiom "for (auto item : collection)"
https://bugs.webkit.org/show_bug.cgi?id=129990
Summary Avoid copy-prone idiom "for (auto item : collection)"
Darin Adler
Reported 2014-03-08 22:58:56 PST
Avoid copy-prone idiom "for (auto item : collection)"
Attachments
Patch (37.57 KB, patch)
2014-03-08 23:08 PST, Darin Adler
no flags
Patch (37.58 KB, patch)
2014-03-10 10:27 PDT, Darin Adler
no flags
Patch (38.22 KB, patch)
2014-03-11 20:09 PDT, Darin Adler
ggaren: review+
ggaren: commit-queue+
Darin Adler
Comment 1 2014-03-08 23:08:58 PST
Darin Adler
Comment 2 2014-03-10 10:27:57 PDT
Darin Adler
Comment 3 2014-03-10 10:29:21 PDT
To find these I used this command: % git grep "for (auto .*:"
Alexey Proskuryakov
Comment 4 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.
Darin Adler
Comment 5 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?
Darin Adler
Comment 6 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.
Darin Adler
Comment 7 2014-03-11 20:09:13 PDT
Geoffrey Garen
Comment 8 2014-03-12 09:44:49 PDT
Comment on attachment 226468 [details] Patch r=me
Darin Adler
Comment 9 2014-03-13 10:52:27 PDT
Antti Koivisto
Comment 10 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.
Antti Koivisto
Comment 11 2014-03-13 14:28:50 PDT
Reverted that part in https://trac.webkit.org/r165566
Jer Noble
Comment 12 2014-03-13 15:29:19 PDT
Jer Noble
Comment 13 2014-03-13 15:29:48 PDT
Oh, thanks Antti. :)
Darin Adler
Comment 14 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.
Ryosuke Niwa
Comment 15 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.
Darin Adler
Comment 16 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.
Darin Adler
Comment 17 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.
Ryosuke Niwa
Comment 18 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.
Note You need to log in before you can comment on or make changes to this bug.