blob: 71930004607e29b04fafcafc8d249015b6c6b212 [file] [log] [blame]
Austin Schuh36244a12019-09-21 17:52:38 -07001// 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// absl::{flat,node,string}_hash_{set,map}.
29
30#ifndef ABSL_CONTAINER_INTERNAL_HASHTABLE_DEBUG_H_
31#define ABSL_CONTAINER_INTERNAL_HASHTABLE_DEBUG_H_
32
33#include <cstddef>
34#include <algorithm>
35#include <type_traits>
36#include <vector>
37
38#include "absl/container/internal/hashtable_debug_hooks.h"
39
40namespace absl {
41namespace container_internal {
42
43// Returns the number of probes required to lookup `key`. Returns 0 for a
44// search with no collisions. Higher values mean more hash collisions occurred;
45// however, the exact meaning of this number varies according to the container
46// type.
47template <typename C>
48size_t GetHashtableDebugNumProbes(
49 const C& c, const typename C::key_type& key) {
50 return absl::container_internal::hashtable_debug_internal::
51 HashtableDebugAccess<C>::GetNumProbes(c, key);
52}
53
54// Gets a histogram of the number of probes for each elements in the container.
55// The sum of all the values in the vector is equal to container.size().
56template <typename C>
57std::vector<size_t> GetHashtableDebugNumProbesHistogram(const C& container) {
58 std::vector<size_t> v;
59 for (auto it = container.begin(); it != container.end(); ++it) {
60 size_t num_probes = GetHashtableDebugNumProbes(
61 container,
62 absl::container_internal::hashtable_debug_internal::GetKey<C>(*it, 0));
63 v.resize((std::max)(v.size(), num_probes + 1));
64 v[num_probes]++;
65 }
66 return v;
67}
68
69struct HashtableDebugProbeSummary {
70 size_t total_elements;
71 size_t total_num_probes;
72 double mean;
73};
74
75// Gets a summary of the probe count distribution for the elements in the
76// container.
77template <typename C>
78HashtableDebugProbeSummary GetHashtableDebugProbeSummary(const C& container) {
79 auto probes = GetHashtableDebugNumProbesHistogram(container);
80 HashtableDebugProbeSummary summary = {};
81 for (size_t i = 0; i < probes.size(); ++i) {
82 summary.total_elements += probes[i];
83 summary.total_num_probes += probes[i] * i;
84 }
85 summary.mean = 1.0 * summary.total_num_probes / summary.total_elements;
86 return summary;
87}
88
89// Returns the number of bytes requested from the allocator by the container
90// and not freed.
91template <typename C>
92size_t AllocatedByteSize(const C& c) {
93 return absl::container_internal::hashtable_debug_internal::
94 HashtableDebugAccess<C>::AllocatedByteSize(c);
95}
96
97// Returns a tight lower bound for AllocatedByteSize(c) where `c` is of type `C`
98// and `c.size()` is equal to `num_elements`.
99template <typename C>
100size_t LowerBoundAllocatedByteSize(size_t num_elements) {
101 return absl::container_internal::hashtable_debug_internal::
102 HashtableDebugAccess<C>::LowerBoundAllocatedByteSize(num_elements);
103}
104
105} // namespace container_internal
106} // namespace absl
107
108#endif // ABSL_CONTAINER_INTERNAL_HASHTABLE_DEBUG_H_