[cfe-dev] Guidelines for use of emplace_back vs push_back in clang?
<Alexander G. Riccio> via cfe-dev
cfe-dev at lists.llvm.org
Sun Jan 17 15:50:00 PST 2016
There are many places in clang where I see code like this:
Checkers.push_back(CheckerInfo(fn, name, desc));
The equivalent use of emplace_back would be:
Checkers.emplace_back(fn, name, desc);
....which is (a) more concise, and (b) more efficient. Is there any reason
why clang uses push_back instead of emplace_back? Are there any guidelines
as to where push_back should be used instead of emplace_back?
If not, is there any reason to keep using push_back? Why not just replace
all uses of push_back with emplace_back?
In llvm/tools/clang/lib alone, I count (via grep -r -n -I push_back lib|wc
-l) 5,421 uses of push_back.
Not all of those are for STL containers (not many of the llvm containers
have emplace_back), but surely this is some low-hanging (performance)
fruit? If not in release builds (where push_back's copy constructor call is
likely elided), then in debug builds?
Background info on emplace_back
<http://en.cppreference.com/w/cpp/container/vector/emplace_back> for those
who are unfamiliar:
emplace_back in-place-constructs (placement new) an element instead of
creating a temporary
<http://stackoverflow.com/questions/4303513/push-back-vs-emplace-back/4306581#4306581>
and then copy constructing (push_back does this).
Sincerely,
Alexander Riccio
--
"Change the world or go home."
about.me/ariccio
<http://about.me/ariccio>
If left to my own devices, I will build more.
⁂
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160117/79b4dd37/attachment.html>
More information about the cfe-dev
mailing list