forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataTypesCache.h
More file actions
88 lines (66 loc) · 2.75 KB
/
Copy pathDataTypesCache.h
File metadata and controls
88 lines (66 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#pragma once
#include <DataTypes/IDataType.h>
#include <DataTypes/DataTypesBinaryEncoding.h>
#include <array>
#include <unordered_map>
namespace DB
{
/// Cache of simple (parameterless) data types and their serializations,
/// pre-filled at construction time. Avoids repeated DataTypeFactory lookups
/// and shared_ptr allocations for commonly used types.
/// Thread-local to avoid atomic refcount contention on shared_ptr
/// when multiple threads return copies of the same DataTypePtr.
class SimpleDataTypesCache
{
public:
struct Element
{
String name;
DataTypePtr type;
SerializationPtr serialization;
};
bool hasElement(BinaryTypeIndex index) const;
/// O(1) lookup by BinaryTypeIndex. Returns the cached element.
const Element & getElement(BinaryTypeIndex index) const;
/// O(1) lookup by BinaryTypeIndex. Returns pre-cached type.
DataTypePtr getType(BinaryTypeIndex index) const;
/// O(1) lookup by BinaryTypeIndex. Returns pre-cached serialization.
SerializationPtr getSerialization(BinaryTypeIndex index) const;
/// Lookup by type name. Returns pre-cached element for simple types, nullptr otherwise.
const Element * findByName(const String & type_name) const;
/// Lookup by type name. Returns pre-cached type for simple types,
/// falls back to DataTypeFactory for others.
DataTypePtr getType(const String & type_name) const;
/// Lookup serialization by type name. Returns pre-cached serialization
/// for simple types, falls back to DataTypeFactory for others.
SerializationPtr getSerialization(const String & type_name) const;
SimpleDataTypesCache();
private:
void addSimpleType(BinaryTypeIndex index, const String & type_name);
std::array<Element, BINARY_TYPE_INDEX_SIZE> by_index{};
std::unordered_map<String, Element> by_name;
};
/// Return a thread-local instance of the simple data type cache.
const SimpleDataTypesCache & getSimpleDataTypesCache();
/// Thread-local cache for data type lookups by name.
/// Checks the thread-local SimpleDataTypesCache first; only caches
/// non-simple types (e.g. DateTime64(9), Variant types) in its own map.
class DataTypesCache
{
public:
DataTypePtr getType(const String & type_name);
SerializationPtr getSerialization(const String & type_name);
private:
static constexpr size_t MAX_ELEMENTS = 16;
struct Element
{
DataTypePtr type;
SerializationPtr serialization;
};
const Element & getCacheElement(const String & type_name);
std::unordered_map<String, Element> cache;
};
/// Return instance of a thread local cache.
/// Cache is relatively small, so it's ok to have separate instance per thread to avoid using mutex inside the cache.
DataTypesCache & getDataTypesCache();
}