Commit 6b849e9d authored by Nick Terrell's avatar Nick Terrell Committed by Facebook Github Bot

Remove unnecessary copies in dynamic::hash()

Summary:
`dynamic::hash()` would copy every key-value pair in the object accumulator hash because of two bugs in the code:
1. The lambda took `auto` instead of `auto const&`
2. The hasher was `hash<pair<dynamic, dynamic>>` not `hash<pair<dynamic const, dynamic>>` meaning a conversion was needed.

These bugs together caused 2 copies for each sub-object. Since the copies are recursive, each object got copied 2*depth times.

Reviewed By: yfeldblum

Differential Revision: D16452213

fbshipit-source-id: 64a55e1640abb022c148183646e9f9720fd8482e
parent 0752c055
......@@ -292,12 +292,12 @@ std::size_t dynamic::hash() const {
// Accumulate using addition instead of using hash_range (as in the ARRAY
// case), as we need a commutative hash operation since unordered_map's
// iteration order is unspecified.
auto h = std::hash<std::pair<dynamic, dynamic>>{};
auto h = std::hash<std::pair<dynamic const, dynamic>>{};
return std::accumulate(
items().begin(),
items().end(),
size_t{0x0B1EC7},
[&](auto acc, auto item) { return acc + h(item); });
[&](auto acc, auto const& item) { return acc + h(item); });
}
case ARRAY:
return folly::hash::hash_range(begin(), end());
......
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