From c77df219b5f10a58e15cfc3954b4cf8d16f2f732 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Mon, 17 Apr 2023 19:29:24 -0700 Subject: [PATCH] ircd::json: Optimize json::type() ABI. ircd::json: Fix indentation; minor cleanup. --- include/ircd/json/stack/stack.h | 8 ++++---- include/ircd/json/type.h | 28 ++++++++++------------------ include/ircd/json/util.h | 10 +++++++++- ircd/json.cc | 17 ++++++++++------- 4 files changed, 33 insertions(+), 30 deletions(-) diff --git a/include/ircd/json/stack/stack.h b/include/ircd/json/stack/stack.h index 9991d434c..9117e07a6 100644 --- a/include/ircd/json/stack/stack.h +++ b/include/ircd/json/stack/stack.h @@ -105,28 +105,28 @@ template<> inline ircd::json::stack::array & ircd::json::stack::stack::top(stack &s) { - return array::top(s); + return array::top(s); } template<> inline const ircd::json::stack::array & ircd::json::stack::stack::top(const stack &s) { - return array::top(s); + return array::top(s); } template<> inline ircd::json::stack::object & ircd::json::stack::stack::top(stack &s) { - return object::top(s); + return object::top(s); } template<> inline const ircd::json::stack::object & ircd::json::stack::stack::top(const stack &s) { - return object::top(s); + return object::top(s); } template<> diff --git a/include/ircd/json/type.h b/include/ircd/json/type.h index 119584fde..0b0a00c8d 100644 --- a/include/ircd/json/type.h +++ b/include/ircd/json/type.h @@ -29,26 +29,18 @@ namespace ircd::json /// not use the strict overload. IRCD_OVERLOAD(strict) - // Determine the type - enum type type(const string_view &); - enum type type(const string_view &, std::nothrow_t); - enum type type(const string_view &, strict_t); - enum type type(const string_view &, strict_t, std::nothrow_t); - - // Query if type - bool type(const string_view &, const enum type &, strict_t); - bool type(const string_view &, const enum type &); - // Utils - string_view reflect(const enum type &); + [[gnu::pure]] string_view reflect(const enum type) noexcept; - extern const string_view literal_null; - extern const string_view literal_true; - extern const string_view literal_false; - extern const string_view empty_string; - extern const string_view empty_object; - extern const string_view empty_array; - extern const int64_t undefined_number; + // Determine the type w/ strict correctness (full scan) + [[gnu::pure]] bool type(const string_view &, const enum type, strict_t) noexcept; + [[gnu::pure]] enum type type(const string_view &, strict_t, std::nothrow_t) noexcept; + enum type type(const string_view &, strict_t); + + // Determine the type quickly + [[gnu::pure]] bool type(const string_view &, const enum type) noexcept; + [[gnu::pure]] enum type type(const string_view &, std::nothrow_t) noexcept; + enum type type(const string_view &); } enum ircd::json::type diff --git a/include/ircd/json/util.h b/include/ircd/json/util.h index 7c6c30b73..07c64b017 100644 --- a/include/ircd/json/util.h +++ b/include/ircd/json/util.h @@ -36,7 +36,15 @@ namespace ircd::json void valid(const string_view &); std::string why(const string_view &); - struct stats extern stats; + extern const string_view literal_null; + extern const string_view literal_true; + extern const string_view literal_false; + extern const string_view empty_string; + extern const string_view empty_object; + extern const string_view empty_array; + extern const int64_t undefined_number; + + extern struct stats stats; } /// Statistics counter access; unfortunately these cannot participate as diff --git a/ircd/json.cc b/ircd/json.cc index df604bea9..a75cb937c 100644 --- a/ircd/json.cc +++ b/ircd/json.cc @@ -4754,7 +4754,8 @@ namespace ircd::json::parser bool ircd::json::type(const string_view &buf, - const enum type &type) + const enum type type) +noexcept { const bool ret { @@ -4780,6 +4781,7 @@ ircd::json::type(const string_view &buf) enum ircd::json::type ircd::json::type(const string_view &buf, std::nothrow_t) +noexcept { enum type ret; if(!parser::parse(begin(buf), end(buf), parser::type_parse, ret)) @@ -4842,8 +4844,9 @@ namespace ircd::json::parser bool ircd::json::type(const string_view &buf, - const enum type &type, + const enum type type, strict_t) +noexcept { const bool ret { @@ -4871,6 +4874,7 @@ enum ircd::json::type ircd::json::type(const string_view &buf, strict_t, std::nothrow_t) +noexcept { enum type ret; if(!parser::parse(begin(buf), end(buf), parser::type_parse_strict, ret)) @@ -4880,7 +4884,8 @@ ircd::json::type(const string_view &buf, } ircd::string_view -ircd::json::reflect(const enum type &type) +ircd::json::reflect(const enum type type) +noexcept { switch(type) { @@ -4891,8 +4896,6 @@ ircd::json::reflect(const enum type &type) case STRING: return "STRING"; } - throw type_error - { - "Unknown type %x", uint(type) - }; + assert(false); + return "STRING"; }