From dc1af0605f7685aff753ad7b6cd72a69309fa274 Mon Sep 17 00:00:00 2001 From: Bronek Kozicki <823856+Bronek@users.noreply.github.com> Date: Thu, 12 Oct 2023 14:43:21 +0100 Subject: [PATCH] Switch to flat_multimap, minor improvements --- src/ripple/rpc/impl/Handler.cpp | 42 ++++++++++++++++++++------------- src/ripple/rpc/impl/Handler.h | 2 +- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/ripple/rpc/impl/Handler.cpp b/src/ripple/rpc/impl/Handler.cpp index b7f0dace5e3..e460c7d7a9e 100644 --- a/src/ripple/rpc/impl/Handler.cpp +++ b/src/ripple/rpc/impl/Handler.cpp @@ -22,6 +22,8 @@ #include #include +#include + namespace ripple { namespace RPC { namespace { @@ -57,6 +59,19 @@ handle(JsonContext& context, Object& object) return status; }; +template +Handler +handlerFrom() +{ + return { + HandlerImpl::name, + &handle, + HandlerImpl::role, + HandlerImpl::condition, + HandlerImpl::minApiVer, + HandlerImpl::maxApiVer}; +} + Handler const handlerArray[]{ // Some handlers not specified here are added to the table via addHandler() // Request-response methods @@ -174,7 +189,8 @@ Handler const handlerArray[]{ class HandlerTable { private: - using handler_table_t = std::multimap; + using handler_table_t = + boost::container::flat_multimap; // Use with equal_range to enforce that API range of a newly added handler // does not overlap with API range of an existing handler with same name @@ -244,15 +260,15 @@ class HandlerTable std::vector getHandlerNames() const { - static constexpr auto less = [](const char* const lh, - const char* const rh) -> bool { - return std::strcmp(lh, rh); - }; - std::set names; + std::vector ret; for (auto const& i : table_) - names.insert(i.second.name_); + { + // Note, table_ is always ordered, allowing such a simple check + if (ret.empty() || std::strcmp(ret.back(), i.second.name_) != 0) + ret.push_back(i.second.name_); + } - return std::vector{names.begin(), names.end()}; + return ret; } private: @@ -270,15 +286,7 @@ class HandlerTable HandlerImpl::minApiVer, HandlerImpl::maxApiVer)); - Handler h; - h.name_ = HandlerImpl::name; - h.valueMethod_ = &handle; - h.role_ = HandlerImpl::role; - h.condition_ = HandlerImpl::condition; - h.minApiVer_ = HandlerImpl::minApiVer; - h.maxApiVer_ = HandlerImpl::maxApiVer; - - table_.insert({HandlerImpl::name, h}); + table_.insert({HandlerImpl::name, handlerFrom()}); } }; diff --git a/src/ripple/rpc/impl/Handler.h b/src/ripple/rpc/impl/Handler.h index eafc165a60f..9c4008bb6e7 100644 --- a/src/ripple/rpc/impl/Handler.h +++ b/src/ripple/rpc/impl/Handler.h @@ -55,7 +55,7 @@ struct Handler RPC::Condition condition_; unsigned minApiVer_ = 1; - unsigned maxApiVer_ = RPC::apiMaximumValidVersion; + unsigned maxApiVer_ = apiMaximumValidVersion; }; Handler const*