Skip to content

Commit

Permalink
Switch to flat_multimap, minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Bronek committed Oct 12, 2023
1 parent e3d382f commit dc1af06
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
42 changes: 25 additions & 17 deletions src/ripple/rpc/impl/Handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include <ripple/rpc/impl/Handler.h>
#include <ripple/rpc/impl/RPCHelpers.h>

#include <boost/container/flat_map.hpp>

namespace ripple {
namespace RPC {
namespace {
Expand Down Expand Up @@ -57,6 +59,19 @@ handle(JsonContext& context, Object& object)
return status;
};

template <typename HandlerImpl>
Handler
handlerFrom()
{
return {
HandlerImpl::name,
&handle<Json::Value, HandlerImpl>,
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
Expand Down Expand Up @@ -174,7 +189,8 @@ Handler const handlerArray[]{
class HandlerTable
{
private:
using handler_table_t = std::multimap<std::string, Handler>;
using handler_table_t =
boost::container::flat_multimap<std::string, Handler>;

// 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
Expand Down Expand Up @@ -244,15 +260,15 @@ class HandlerTable
std::vector<char const*>
getHandlerNames() const
{
static constexpr auto less = [](const char* const lh,
const char* const rh) -> bool {
return std::strcmp(lh, rh);
};
std::set<const char* const, decltype(less)> names;
std::vector<char const*> 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<char const*>{names.begin(), names.end()};
return ret;
}

private:
Expand All @@ -270,15 +286,7 @@ class HandlerTable
HandlerImpl::minApiVer,
HandlerImpl::maxApiVer));

Handler h;
h.name_ = HandlerImpl::name;
h.valueMethod_ = &handle<Json::Value, HandlerImpl>;
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<HandlerImpl>()});
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/ripple/rpc/impl/Handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ struct Handler
RPC::Condition condition_;

unsigned minApiVer_ = 1;
unsigned maxApiVer_ = RPC::apiMaximumValidVersion;
unsigned maxApiVer_ = apiMaximumValidVersion;
};

Handler const*
Expand Down

0 comments on commit dc1af06

Please sign in to comment.