18
18
#pragma once
19
19
20
20
#include < gen_cpp/olap_file.pb.h>
21
+ #include < parallel_hashmap/phmap.h>
21
22
22
23
#include < memory>
23
- #include < mutex>
24
- #include < unordered_map>
25
24
26
25
#include " olap/tablet_schema.h"
27
26
@@ -37,26 +36,23 @@ class TabletSchemaCache {
37
36
38
37
static TabletSchemaCache* instance () { return _s_instance; }
39
38
40
- std::shared_ptr<TabletSchema> insert (const std::string& key) {
41
- std::lock_guard guard (_mtx);
42
- auto iter = _cache.find (key);
43
- if (iter == _cache.end ()) {
44
- TabletSchemaPB tablet_schema_pb;
45
- tablet_schema_pb.ParseFromString (key);
46
- std::shared_ptr<TabletSchema> value = std::make_shared<TabletSchema>();
47
- value->init_from_pb (tablet_schema_pb);
48
- _cache[key] = value;
49
- return value;
50
- } else {
51
- return iter->second ;
52
- }
39
+ TabletSchemaSPtr insert (const std::string& key) {
40
+ TabletSchemaSPtr res;
41
+ _cache.lazy_emplace_l (
42
+ key, [&](const TabletSchemaSPtr& tablet_schema) { res = tablet_schema; },
43
+ [&](const auto & ctor) {
44
+ TabletSchemaPB tablet_schema_pb;
45
+ tablet_schema_pb.ParseFromString (key);
46
+ res = std::make_shared<TabletSchema>();
47
+ res->init_from_pb (tablet_schema_pb);
48
+ ctor (key, res);
49
+ });
50
+ return res;
53
51
}
54
52
55
53
private:
56
54
static inline TabletSchemaCache* _s_instance = nullptr ;
57
-
58
- std::unordered_map<std::string, TabletSchemaSPtr> _cache;
59
- std::mutex _mtx;
55
+ phmap::parallel_flat_hash_map<std::string, TabletSchemaSPtr> _cache;
60
56
};
61
57
62
58
} // namespace doris
0 commit comments