Closed
Bug 1324547
Opened 7 years ago
Closed 7 years ago
ensure we're not needlessly copying things in ranged for loops
Categories
(Developer Infrastructure :: Source Code Analysis, defect)
Developer Infrastructure
Source Code Analysis
Tracking
(firefox53 affected)
RESOLVED
FIXED
Tracking | Status | |
---|---|---|
firefox53 | --- | affected |
People
(Reporter: froydnj, Assigned: andi)
Details
Attachments
(1 file)
e.g. along the lines of: https://cgit.freedesktop.org/libreoffice/core/tree/compilerplugins/clang/rangedforcopy.cxx Quote: // Check that we're not unnecessarily copying variables in a range based for loop // e.g. "for (OUString a: aList)" results in a copy of each string being made, // whereas "for (const OUString& a: aList)" does not. The whole directory there is full of some interesting things that could be ported over to our static analyses. Some of the plugins there are intended to be run over the whole codebase from time to time and then filtered through a post-processing step (kind of a poor man's LTO).
Reporter | ||
Comment 1•7 years ago
|
||
Is this something you'd be interested in doing?
Flags: needinfo?(bpostelnicu)
Assignee | ||
Comment 2•7 years ago
|
||
Sure, I think i have time for this at the beginning of next next year.
Flags: needinfo?(bpostelnicu)
Assignee | ||
Updated•7 years ago
|
Assignee: nobody → bpostelnicu
Comment 3•7 years ago
|
||
Looks like clang tidy has something for this: http://clang.llvm.org/extra/clang-tidy/checks/performance-for-range-copy.html
Comment hidden (mozreview-request) |
Comment 5•7 years ago
|
||
It found only three occurrences only in tests: https://reviewboard.mozilla.org/r/100228 So, either the clang-tidy is limited in term of detection or our code is good (I guess this is the first :p)
Reporter | ||
Comment 6•7 years ago
|
||
(In reply to Sylvestre Ledru [:sylvestre] from comment #5) > It found only three occurrences only in tests: > https://reviewboard.mozilla.org/r/100228 > So, either the clang-tidy is limited in term of detection or our code is > good (I guess this is the first :p) I went through and fixed a couple other instances a while back via manually grepping and code examination. :p I wish we required |const auto&| for those changes, but *shrug*.
Comment 7•7 years ago
|
||
Do you have example of those? Thanks
Reporter | ||
Comment 8•7 years ago
|
||
(In reply to Sylvestre Ledru [:sylvestre] from comment #7) > Do you have example of those? Thanks Bug 1287785. I thought there were others somewhere in dom/media/, but a quick grep through my commits for 2016 doesn't show anything.
Assignee | ||
Comment 9•7 years ago
|
||
This checker is pretty easy to adopt into our code, i think we will have a better view after we will have it in place. Basically what it needs to be done is to create a matcher of CXXForRangeStmt and examine the var declaration from the loop variable to be reference or pointer, otherwise assert an error message. I ill do this.
Assignee | ||
Comment 10•7 years ago
|
||
This is the way how it's implemented using clang-tidy infrastructure, it can be easily adopted into our clang-plugin schematics: https://github.com/llvm-mirror/clang-tools-extra/blob/master/clang-tidy/performance/ForRangeCopyCheck.cpp This implementation is better since it avoids triggering the callback on ranged loops disregarding the loop variable qualification type.
Comment 11•7 years ago
|
||
I think it should be fine if the object is a small POD type such as uint32_t, in addition to pointers and references.
Updated•7 years ago
|
Attachment #8820785 -
Flags: review?(nfroyd)
Reporter | ||
Comment 12•7 years ago
|
||
mozreview-review |
Comment on attachment 8820785 [details] Bug 1324547 - ensure we're not needlessly copying things in ranged for loop https://reviewboard.mozilla.org/r/100228/#review102322 OK with the instances modified to `const auto&`, as below. Thanks! ::: dom/plugins/base/nsPluginHost.cpp:1582 (Diff revision 1) > > RefPtr<nsFakePluginTag> newTag; > nsresult rv = nsFakePluginTag::Create(initDictionary, getter_AddRefs(newTag)); > NS_ENSURE_SUCCESS(rv, rv); > > - for (auto existingTag : mFakePlugins) { > + for (const auto existingTag : mFakePlugins) { I would feel way better about reading this if it was `const auto&`. I know I went and thought about it after writing comment 6, but I've completely forgotten what the rationale is! Writing it `const auto&` is also a little more in tune with the current/emerging style of `const auto&&`, but not nearly so radical.
Attachment #8820785 -
Flags: review?(nfroyd) → review+
Comment hidden (mozreview-request) |
Comment 14•7 years ago
|
||
Pushed by sledru@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/12b902bdace0 ensure we're not needlessly copying things in ranged for loop r=froydnj
Updated•7 years ago
|
Keywords: leave-open
Comment 15•7 years ago
|
||
bugherder |
https://hg.mozilla.org/mozilla-central/rev/12b902bdace0
Assignee | ||
Comment 16•7 years ago
|
||
This has been fixed since now our analysis has this checker as default: https://dxr.mozilla.org/mozilla-central/source/tools/clang-tidy/config.yaml#46 Also this checker tries to determine if using reference or pointer is more efficient for some types by using: isExpensiveToCopy, see: https://clang.llvm.org/extra/doxygen/namespaceclang_1_1tidy_1_1utils_1_1type__traits.html#aa26ca62a658038b6d4d15da8b4e5e965
Status: NEW → RESOLVED
Closed: 7 years ago
Resolution: --- → FIXED
Comment 17•6 years ago
|
||
Removing leave-open keyword from resolved bugs, per :sylvestre.
Keywords: leave-open
Updated•6 years ago
|
Product: Core → Firefox Build System
Updated•2 years ago
|
Product: Firefox Build System → Developer Infrastructure
You need to log in
before you can comment on or make changes to this bug.
Description
•