Debian Codegraph @

tests/hashtable_debug.h

raw
1 // Copyright 2018 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // This library provides APIs to debug the probing behavior of hash tables.
16 //
17 // In general, the probing behavior is a black box for users and only the
18 // side effects can be measured in the form of performance differences.
19 // These APIs give a glimpse on the actual behavior of the probing algorithms in
20 // these hashtables given a specified hash function and a set of elements.
21 //
22 // The probe count distribution can be used to assess the quality of the hash
23 // function for that particular hash table. Note that a hash function that
24 // performs well in one hash table implementation does not necessarily performs
25 // well in a different one.
26 //
27 // This library supports std::unordered_{set,map}, dense_hash_{set,map} and
28 // phmap::{flat,node,string}_hash_{set,map}.
29
30 #ifndef PHMAP_PRIV_HASHTABLE_DEBUG_H_
31 #define PHMAP_PRIV_HASHTABLE_DEBUG_H_
32
33 #include <cstddef>
34 #include <algorithm>
35 #include <type_traits>
36 #include <vector>
37
38 namespace phmap {
39 namespace priv {
40
41 // Returns the number of probes required to lookup `key`. Returns 0 for a
42 // search with no collisions. Higher values mean more hash collisions occurred;
43 // however, the exact meaning of this number varies according to the container
44 // type.
45 template <typename C>
46 size_t GetHashtableDebugNumProbes(
47 const C& c, const typename C::key_type& key) {
48 return phmap::priv::hashtable_debug_internal::
49 HashtableDebugAccess<C>::GetNumProbes(c, key);
50 }
51
52 // Gets a histogram of the number of probes for each elements in the container.
53 // The sum of all the values in the vector is equal to container.size().
54 template <typename C>
55 std::vector<size_t> GetHashtableDebugNumProbesHistogram(const C& container) {
56 std::vector<size_t> v;
57 for (auto it = container.begin(); it != container.end(); ++it) {
58 size_t num_probes = GetHashtableDebugNumProbes(
59 container,
60 phmap::priv::hashtable_debug_internal::GetKey<C>(*it, 0));
61 v.resize((std::max)(v.size(), num_probes + 1));
62 v[num_probes]++;
63 }
64 return v;
65 }
66
67 struct HashtableDebugProbeSummary {
68 size_t total_elements;
69 size_t total_num_probes;
70 double mean;
71 };
72
73 // Gets a summary of the probe count distribution for the elements in the
74 // container.
75 template <typename C>
76 HashtableDebugProbeSummary GetHashtableDebugProbeSummary(const C& container) {
77 auto probes = GetHashtableDebugNumProbesHistogram(container);
78 HashtableDebugProbeSummary summary = {};
79 for (size_t i = 0; i < probes.size(); ++i) {
80 summary.total_elements += probes[i];
81 summary.total_num_probes += probes[i] * i;
82 }
83 summary.mean = 1.0 * summary.total_num_probes / summary.total_elements;
84 return summary;
85 }
86
87 // Returns the number of bytes requested from the allocator by the container
88 // and not freed.
89 template <typename C>
90 size_t AllocatedByteSize(const C& c) {
91 return phmap::priv::hashtable_debug_internal::
92 HashtableDebugAccess<C>::AllocatedByteSize(c);
93 }
94
95 // Returns a tight lower bound for AllocatedByteSize(c) where `c` is of type `C`
96 // and `c.size()` is equal to `num_elements`.
97 template <typename C>
98 size_t LowerBoundAllocatedByteSize(size_t num_elements) {
99 return phmap::priv::hashtable_debug_internal::
100 HashtableDebugAccess<C>::LowerBoundAllocatedByteSize(num_elements);
101 }
102
103 } // namespace priv
104 } // namespace phmap
105
106 #endif // PHMAP_PRIV_HASHTABLE_DEBUG_H_