Skip to content

Commit

Permalink
Implement a fast constant hash function for object keys (#1325)
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti authored Nov 16, 2024
1 parent a64204f commit 117a478
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/json/include/sourcemeta/jsontoolkit/json_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@
#include <map> // std::map
#else
#include <unordered_map> // std::unordered_map

// This hash function is specifically designed to be constant
// with regards to string length, and to exploit the fact that
// most JSON objects don't have a lot of entries, so hash collision
// is not as common
namespace sourcemeta::jsontoolkit {
template <typename T> struct ObjectKeyHash {
inline auto operator()(const T &value) const noexcept -> std::size_t {
return value.empty()
? 0
: value.size() + static_cast<std::size_t>(value.front()) +
static_cast<std::size_t>(value.back());
}
};
} // namespace sourcemeta::jsontoolkit

#endif

namespace sourcemeta::jsontoolkit {
Expand All @@ -23,7 +39,7 @@ template <typename Key, typename Value> class JSONObject {
#if defined(__GNUC__) && !defined(__clang__) && (__GNUC__ < 12)
std::map<Key, Value, std::less<Key>,
#else
std::unordered_map<Key, Value, std::hash<Key>, std::equal_to<Key>,
std::unordered_map<Key, Value, ObjectKeyHash<Key>, std::equal_to<Key>,
#endif
typename Value::template Allocator<
std::pair<const typename Value::String, Value>>>;
Expand Down

4 comments on commit 117a478

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark (macos/llvm)

Benchmark suite Current: 117a478 Previous: a64204f Ratio
JSON_Array_Of_Objects_Unique 398.74955896365935 ns/iter 413.027202383038 ns/iter 0.97

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark (linux/llvm)

Benchmark suite Current: 117a478 Previous: a64204f Ratio
JSON_Array_Of_Objects_Unique 454.6221706303759 ns/iter 404.64829354306283 ns/iter 1.12

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark (linux/gcc)

Benchmark suite Current: 117a478 Previous: a64204f Ratio
JSON_Array_Of_Objects_Unique 574.7063997931348 ns/iter 568.831800625642 ns/iter 1.01

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark (windows/msvc)

Benchmark suite Current: 117a478 Previous: a64204f Ratio
JSON_Array_Of_Objects_Unique 543.939200000068 ns/iter 495.9318000001076 ns/iter 1.10

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.