[libcxx-commits] [PATCH] D121485: [libc++] Fix performance inconsistency between map copy-assignment and copy-constructor

Louis Dionne via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Fri Mar 11 12:06:52 PST 2022


ldionne created this revision.
Herald added a project: All.
ldionne requested review of this revision.
Herald added a project: libc++.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

Before this patch, std::map's copy constructor and copy assignment operators
would have significantly different performance characteristics. This was
caused by __tree::operator= using __assign_multi even in the case of a
container with unique keys like std::map. Furthermore, upon investigation,
it appears that even using __assign_unique is slower than simply inserting
elements individually, so this patch uses the most naive approach, which
still provides an improvement over the status quo.

In the future, we could instead reintroduce a copy assignment operator in
__tree and copy the whole tree without performing any comparison. This
isn't done in this patch because such a patch should ensure the copy
assignment and copy constructors for __tree are consistent, which goes
beyond the targeted fix I'm trying to do here.

This patch also adds a benchmark for std::map copy assignment, which
highlights the performance inconsistency we had:

---------------------------------------------------------------------------------

Benchmark                                     Time             CPU     Iterations
---------------------------------------------------------------------------------

(before) BM_ConstructorCopy_MapSize=1000000   198 ns          198 ns   1000000
(before) BM_CopyAssignment_MapSize=1000000    208 ns          208 ns   1000000
(after)  BM_ConstructorCopy_MapSize=1000000   193 ns          193 ns   1000000
(after)  BM_CopyAssignment_MapSize=1000000    193 ns          193 ns   1000000

rdar://89334879


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D121485

Files:
  libcxx/benchmarks/map.bench.cpp
  libcxx/include/__tree
  libcxx/include/map
  libcxx/include/set

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D121485.414723.patch
Type: text/x-patch
Size: 5127 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20220311/d5fca4bc/attachment.bin>


More information about the libcxx-commits mailing list