Commit a2c4f8cf authored by Maged Michael's avatar Maged Michael Committed by Facebook GitHub Bot

SingleWriterFixedHashMap: Optimize copying

Summary: Use memcpy for copying maps with the same capacity instead of rehashing.

Reviewed By: yfeldblum

Differential Revision: D20863257

fbshipit-source-id: 11e52f9f2246440fb724a94c2611e44e253e462d
parent 2f75edd3
......@@ -86,6 +86,12 @@ class SingleWriterFixedHashMap {
return;
}
elem_ = std::make_unique<Elem[]>(capacity_);
if (capacity_ == o.capacity_) {
memcpy(elem_.get(), o.elem_.get(), capacity_ * sizeof(Elem));
used_ = o.used_;
setSize(o.size());
return;
}
for (size_t i = 0; i < o.capacity_; ++i) {
Elem& e = o.elem_[i];
if (e.valid()) {
......
......@@ -296,6 +296,20 @@ uint64_t bench_copy_nonempty_dtor(const int nthr, const uint64_t ops) {
return runBench(ops, repFn);
}
uint64_t bench_copy_resize_dtor(const int nthr, const uint64_t ops) {
auto repFn = [&] {
SWFHM m0(16);
m0.insert(10, 10);
auto fn = [&](int tid) {
for (uint64_t i = tid; i < ops; i += nthr) {
SWFHM m(2 * m0.capacity(), m0);
}
};
return run_once(nthr, fn);
};
return runBench(ops, repFn);
}
void dottedLine() {
std::cout
<< "........................................................................"
......@@ -336,6 +350,8 @@ TEST(SingleWriterFixedHashMapBench, Bench) {
bench_copy_empty_dtor(i, ops);
std::cout << "copy nonempty / destruct ";
bench_copy_nonempty_dtor(i, ops);
std::cout << "copy resize / destruct ";
bench_copy_resize_dtor(i, ops);
}
std::cout
<< "========================================================================"
......@@ -354,12 +370,14 @@ Test name Max time Avg time Dev time Min time
iterate 10-element 32-slot map 20 ns 19 ns 0 ns 19 ns
construct / destruct 1 ns 1 ns 0 ns 1 ns
copy empty / destruct 5 ns 4 ns 0 ns 4 ns
copy nonempty / destruct 39 ns 38 ns 0 ns 37 ns
copy nonempty / destruct 25 ns 24 ns 0 ns 23 ns
copy resize / destruct 40 ns 38 ns 0 ns 37 ns
============================== 10 threads ==============================
10x find 6 ns 4 ns 1 ns 3 ns
iterate 10-element 32-slot map 3 ns 3 ns 0 ns 1 ns
construct / destruct 0 ns 0 ns 0 ns 0 ns
copy empty / destruct 0 ns 0 ns 0 ns 0 ns
copy nonempty / destruct 7 ns 5 ns 1 ns 3 ns
copy nonempty / destruct 4 ns 3 ns 0 ns 2 ns
copy resize / destruct 8 ns 6 ns 1 ns 4 ns
========================================================================
*/
......@@ -300,8 +300,8 @@ onSet 12 ns 12 ns 0 ns 12 ns
onUnset 12 ns 12 ns 0 ns 12 ns
setContext 46 ns 44 ns 1 ns 42 ns
RequestContextScopeGuard 113 ns 103 ns 3 ns 101 ns
ShallowCopyRequestC...-replace 310 ns 291 ns 8 ns 283 ns
ShallowCopyReq...-keep&replace 1562 ns 1444 ns 45 ns 1405 ns
ShallowCopyRequestC...-replace 287 ns 279 ns 5 ns 270 ns
ShallowCopyReq...-keep&replace 1191 ns 1149 ns 21 ns 1121 ns
============================== 10 threads ==============================
hasContextData 1 ns 1 ns 0 ns 1 ns
getContextData 2 ns 1 ns 0 ns 1 ns
......@@ -309,7 +309,7 @@ onSet 2 ns 2 ns 0 ns 1 ns
onUnset 2 ns 2 ns 0 ns 1 ns
setContext 11 ns 7 ns 2 ns 5 ns
RequestContextScopeGuard 22 ns 15 ns 5 ns 11 ns
ShallowCopyRequestC...-replace 64 ns 36 ns 10 ns 31 ns
ShallowCopyReq...-keep&replace 161 ns 156 ns 3 ns 152 ns
ShallowCopyRequestC...-replace 61 ns 43 ns 14 ns 30 ns
ShallowCopyReq...-keep&replace 123 ns 121 ns 0 ns 120 ns
========================================================================
*/
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment