From 2aedf52d54934b7d6dfe1e54ad58d2627b22a7e7 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Tue, 29 Sep 2026 08:41:16 +0200 Subject: [PATCH] Inline get() of arithmetic values of json_view get() of arithmetic types is inlined down to the conversion, so that its checks of the node kind merge with those of the caller, and reading an integer needs no call. Traversing every value: citm_catalog -6%, marine_ik -5%, numbers and twitter -3%, mesh -2.5%, canada -1% (and more above the float conversion from the digit layout: citm_catalog -14%, marine_ik -11%). Signed-off-by: Niels Lohmann --- include/nlohmann/detail/view/value.hpp | 2 +- include/nlohmann/json_view.hpp | 4 ++-- single_include/nlohmann/json_view.hpp | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/nlohmann/detail/view/value.hpp b/include/nlohmann/detail/view/value.hpp index 64b05b254..b43a9c4df 100644 --- a/include/nlohmann/detail/view/value.hpp +++ b/include/nlohmann/detail/view/value.hpp @@ -40,7 +40,7 @@ converted with static_cast, booleans give 0 or 1, and other types throw type_error.302. */ template -T arithmetic_value(const document_data& d, const node& n) +NLOHMANN_VIEW_ALWAYS_INLINE T arithmetic_value(const document_data& d, const node& n) { switch (static_cast(n.kind)) { diff --git a/include/nlohmann/json_view.hpp b/include/nlohmann/json_view.hpp index 14a360cac..972ecd63d 100644 --- a/include/nlohmann/json_view.hpp +++ b/include/nlohmann/json_view.hpp @@ -512,7 +512,7 @@ class basic_json_view /// maps with string keys, and views are converted directly; other types /// through materialize().get() template - T get() const + NLOHMANN_VIEW_ALWAYS_INLINE T get() const { return get_impl(detail::view::value_tag {}, detail::priority_tag<2> {}); } @@ -599,7 +599,7 @@ class basic_json_view } template < typename T, typename std::enable_if < std::is_arithmetic::value && !std::is_same::value, int >::type = 0 > - T get_impl(detail::view::value_tag /*unused*/, detail::priority_tag<2> /*unused*/) const + NLOHMANN_VIEW_ALWAYS_INLINE T get_impl(detail::view::value_tag /*unused*/, detail::priority_tag<2> /*unused*/) const { if (NLOHMANN_VIEW_UNLIKELY(m_node == nullptr)) { diff --git a/single_include/nlohmann/json_view.hpp b/single_include/nlohmann/json_view.hpp index 94f634102..f3d955f22 100644 --- a/single_include/nlohmann/json_view.hpp +++ b/single_include/nlohmann/json_view.hpp @@ -2718,7 +2718,7 @@ converted with static_cast, booleans give 0 or 1, and other types throw type_error.302. */ template -T arithmetic_value(const document_data& d, const node& n) +NLOHMANN_VIEW_ALWAYS_INLINE T arithmetic_value(const document_data& d, const node& n) { switch (static_cast(n.kind)) { @@ -3242,7 +3242,7 @@ class basic_json_view /// maps with string keys, and views are converted directly; other types /// through materialize().get() template - T get() const + NLOHMANN_VIEW_ALWAYS_INLINE T get() const { return get_impl(detail::view::value_tag {}, detail::priority_tag<2> {}); } @@ -3329,7 +3329,7 @@ class basic_json_view } template < typename T, typename std::enable_if < std::is_arithmetic::value && !std::is_same::value, int >::type = 0 > - T get_impl(detail::view::value_tag /*unused*/, detail::priority_tag<2> /*unused*/) const + NLOHMANN_VIEW_ALWAYS_INLINE T get_impl(detail::view::value_tag /*unused*/, detail::priority_tag<2> /*unused*/) const { if (NLOHMANN_VIEW_UNLIKELY(m_node == nullptr)) {