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