From 9295c22ed773782b27cc77fa95bc73934d45f8d7 Mon Sep 17 00:00:00 2001 From: Lchangliang <915311741@qq.com> Date: Mon, 25 Jul 2022 11:42:03 +0800 Subject: [PATCH] opt cache impl --- be/src/olap/tablet_schema_cache.h | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/be/src/olap/tablet_schema_cache.h b/be/src/olap/tablet_schema_cache.h index 4a12e1c38e1ae3d..49d39672428718c 100644 --- a/be/src/olap/tablet_schema_cache.h +++ b/be/src/olap/tablet_schema_cache.h @@ -18,10 +18,9 @@ #pragma once #include +#include #include -#include -#include #include "olap/tablet_schema.h" @@ -37,26 +36,22 @@ class TabletSchemaCache { static TabletSchemaCache* instance() { return _s_instance; } - std::shared_ptr 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 value = std::make_shared(); - 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 _cache; - std::mutex _mtx; + phmap::parallel_flat_hash_map _cache; }; } // namespace doris \ No newline at end of file