Skip to content

Commit 13f8178

Browse files
committed
opt cache impl
1 parent 42d2578 commit 13f8178

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

be/src/olap/tablet_schema_cache.h

+14-18
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@
1818
#pragma once
1919

2020
#include <gen_cpp/olap_file.pb.h>
21+
#include <parallel_hashmap/phmap.h>
2122

2223
#include <memory>
23-
#include <mutex>
24-
#include <unordered_map>
2524

2625
#include "olap/tablet_schema.h"
2726

@@ -37,26 +36,23 @@ class TabletSchemaCache {
3736

3837
static TabletSchemaCache* instance() { return _s_instance; }
3938

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;
5351
}
5452

5553
private:
5654
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;
6056
};
6157

6258
} // namespace doris

0 commit comments

Comments
 (0)