v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
fast-hash.h
Go to the documentation of this file.
1// Copyright 2022 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_COMPILER_TURBOSHAFT_FAST_HASH_H_
6#define V8_COMPILER_TURBOSHAFT_FAST_HASH_H_
7
8#include <tuple>
9
10#include "src/base/hashing.h"
11#include "src/base/vector.h"
12
14
15// fast_hash_combine() / fast_hash_value() produce a bad but very fast to
16// compute hash, intended for hash-tables and only usable for data that is
17// sufficiently random already and has high variance in their low bits.
18
19V8_INLINE size_t fast_hash_combine() { return 0u; }
20V8_INLINE size_t fast_hash_combine(size_t acc) { return acc; }
21V8_INLINE size_t fast_hash_combine(size_t acc, size_t value) {
22 return 17 * acc + value;
23}
24template <typename T, typename... Ts>
25V8_INLINE size_t fast_hash_combine(T const& v, Ts const&... vs);
26
27template <class T>
28struct fast_hash {
29 size_t operator()(const T& v) const {
30 if constexpr (std::is_enum<T>::value) {
31 return static_cast<size_t>(v);
32 } else {
33 return base::hash<T>()(v);
34 }
35 }
36};
37
38template <typename T1, typename T2>
39struct fast_hash<std::pair<T1, T2>> {
40 size_t operator()(const std::pair<T1, T2>& v) const {
41 return fast_hash_combine(v.first, v.second);
42 }
43};
44
45template <class... Ts>
46struct fast_hash<std::tuple<Ts...>> {
47 size_t operator()(const std::tuple<Ts...>& v) const {
48 return impl(v, std::make_index_sequence<sizeof...(Ts)>());
49 }
50
51 template <size_t... I>
52 V8_INLINE size_t impl(std::tuple<Ts...> const& v,
53 std::index_sequence<I...>) const {
54 return fast_hash_combine(std::get<I>(v)...);
55 }
56};
57
58template <typename T, typename... Ts>
59V8_INLINE size_t fast_hash_combine(T const& v, Ts const&... vs) {
61}
62
63template <typename Iterator>
64V8_INLINE size_t fast_hash_range(Iterator first, Iterator last) {
65 size_t acc = 0;
66 for (; first != last; ++first) {
67 acc = fast_hash_combine(acc, *first);
68 }
69 return acc;
70}
71
72template <typename T>
73struct fast_hash<base::Vector<T>> {
75 return fast_hash_range(v.begin(), v.end());
76 }
77};
78
79} // namespace v8::internal::compiler::turboshaft
80
81#endif // V8_COMPILER_TURBOSHAFT_FAST_HASH_H_
#define T
constexpr T * begin() const
Definition vector.h:96
constexpr T * end() const
Definition vector.h:103
STL namespace.
V8_INLINE size_t fast_hash_range(Iterator first, Iterator last)
Definition fast-hash.h:64
V8_INLINE size_t fast_hash_combine()
Definition fast-hash.h:19
constexpr int I
return value
Definition map-inl.h:893
V8_INLINE size_t operator()(base::Vector< T > v) const
Definition fast-hash.h:74
size_t operator()(const std::pair< T1, T2 > &v) const
Definition fast-hash.h:40
size_t operator()(const std::tuple< Ts... > &v) const
Definition fast-hash.h:47
V8_INLINE size_t impl(std::tuple< Ts... > const &v, std::index_sequence< I... >) const
Definition fast-hash.h:52
#define V8_INLINE
Definition v8config.h:500