From c4637a4222a92b8a9b56d28444bf063acd4f9d51 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Thu, 13 Feb 2025 20:57:07 +0900 Subject: [PATCH] feat: keep only 1 scanner in a cache while parsing, thread_local cache will not use scanners constructed from multiple toml::spec. we don't need to keep multiple scanners --- include/toml11/impl/syntax_impl.hpp | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/include/toml11/impl/syntax_impl.hpp b/include/toml11/impl/syntax_impl.hpp index fdd04a3..2dc768a 100644 --- a/include/toml11/impl/syntax_impl.hpp +++ b/include/toml11/impl/syntax_impl.hpp @@ -21,27 +21,21 @@ struct syntax_cache static_assert(std::is_base_of::value, ""); explicit syntax_cache(F f) - : func_(std::move(f)), cache_{} + : func_(std::move(f)), cache_(cxx::make_nullopt()) {} value_type const& at(const spec& s) { - const auto found = std::find_if(cache_.begin(), cache_.end(), - [&s](const std::pair& kv) { return kv.first == s; }); - if(found == cache_.end()) + if( ! this->cache_.has_value() || this->cache_.value().first != s) { - this->cache_.emplace_back(s, func_(s)); - return cache_.back().second; - } - else - { - return found->second; + this->cache_ = std::make_pair(s, func_(s)); } + return this->cache_.value().second; } private: F func_; - std::vector> cache_; + cxx::optional> cache_; }; template