From 7669fd53d9a86695fcda0ce714606b29b1047b4e Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Wed, 12 Feb 2020 11:23:35 -0800 Subject: [PATCH] ircd::json: Move exception construction branch to unlikely seg; minor cleanup. --- include/ircd/json/object.h | 48 ++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/include/ircd/json/object.h b/include/ircd/json/object.h index db14c651e..c233539e1 100644 --- a/include/ircd/json/object.h +++ b/include/ircd/json/object.h @@ -188,11 +188,15 @@ T ircd::json::at(const object &object) try { - const auto it(object.find(key)); - if(it == end(object)) + const auto it + { + object.find(key) + }; + + if(unlikely(it == end(object))) throw not_found { - "[key hash] '%zu'", key + "[key hash] '%lu'", ulong(key) }; return lex_cast(it->second); @@ -201,8 +205,8 @@ catch(const bad_lex_cast &e) { throw type_error { - "[key hash] '%zu' must cast to type %s", - key, + "[key hash] '%lu' must cast to type %s", + ulong(key), typeid(T).name() }; } @@ -212,8 +216,12 @@ T ircd::json::object::at(const string_view &key) const try { - const auto it(find(key)); - if(it == end()) + const auto it + { + find(key) + }; + + if(unlikely(it == end())) throw not_found { "'%s'", key @@ -238,12 +246,22 @@ ircd::json::get(const object &object, const T &def) try { - const auto it{object.find(key)}; + const auto it + { + object.find(key) + }; + if(it == end(object)) return def; - const string_view sv{it->second}; - return !sv.empty()? lex_cast(sv) : def; + const string_view sv + { + it->second + }; + + return !sv.empty()? + lex_cast(sv): + def; } catch(const bad_lex_cast &e) { @@ -256,8 +274,14 @@ ircd::json::object::get(const string_view &key, const T &def) const try { - const string_view sv(operator[](key)); - return !sv.empty()? lex_cast(sv) : def; + const string_view sv + { + operator[](key) + }; + + return !sv.empty()? + lex_cast(sv): + def; } catch(const bad_lex_cast &e) {