Skip to content

Commit

Permalink
opt cache impl
Browse files Browse the repository at this point in the history
  • Loading branch information
Lchangliang committed Jul 25, 2022
1 parent ee3dcb1 commit 9295c22
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions be/src/olap/tablet_schema_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@
#pragma once

#include <gen_cpp/olap_file.pb.h>
#include <parallel_hashmap/phmap.h>

#include <memory>
#include <mutex>
#include <unordered_map>

#include "olap/tablet_schema.h"

Expand All @@ -37,26 +36,22 @@ class TabletSchemaCache {

static TabletSchemaCache* instance() { return _s_instance; }

std::shared_ptr<TabletSchema> insert(const std::string& key) {
std::lock_guard guard(_mtx);
auto iter = _cache.find(key);
if (iter == _cache.end()) {
TabletSchemaPB tablet_schema_pb;
tablet_schema_pb.ParseFromString(key);
std::shared_ptr<TabletSchema> value = std::make_shared<TabletSchema>();
value->init_from_pb(tablet_schema_pb);
_cache[key] = value;
return value;
} else {
return iter->second;
}
TabletSchemaSPtr insert(const std::string& key) {
TabletSchemaSPtr res;
_cache.lazy_emplace_l(
key, [&](const TabletSchemaSPtr& tablet_schema) { res = tablet_schema; },
[&](const auto& ctor) {
TabletSchemaPB tablet_schema_pb;
tablet_schema_pb.ParseFromString(key);
res->init_from_pb(tablet_schema_pb);
ctor(key, res);
});
return res;
}

private:
static inline TabletSchemaCache* _s_instance = nullptr;

std::unordered_map<std::string, TabletSchemaSPtr> _cache;
std::mutex _mtx;
phmap::parallel_flat_hash_map<std::string, TabletSchemaSPtr> _cache;
};

} // namespace doris

0 comments on commit 9295c22

Please sign in to comment.