Inline get() of arithmetic values of json_view

get<T>() 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 <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann
2026-09-29 08:41:16 +02:00
parent 2161794b5a
commit 7767ce246d
3 changed files with 6 additions and 6 deletions

View File

@@ -40,7 +40,7 @@ converted with static_cast, booleans give 0 or 1, and other types throw
type_error.302.
*/
template<typename T, typename BasicJsonType>
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<value_t>(n.kind))
{

View File

@@ -512,7 +512,7 @@ class basic_json_view
/// maps with string keys, and views are converted directly; other types
/// through materialize().get<T>()
template<typename T>
T get() const
NLOHMANN_VIEW_ALWAYS_INLINE T get() const
{
return get_impl(detail::view::value_tag<T> {}, detail::priority_tag<2> {});
}
@@ -599,7 +599,7 @@ class basic_json_view
}
template < typename T, typename std::enable_if < std::is_arithmetic<T>::value && !std::is_same<T, bool>::value, int >::type = 0 >
T get_impl(detail::view::value_tag<T> /*unused*/, detail::priority_tag<2> /*unused*/) const
NLOHMANN_VIEW_ALWAYS_INLINE T get_impl(detail::view::value_tag<T> /*unused*/, detail::priority_tag<2> /*unused*/) const
{
if (NLOHMANN_VIEW_UNLIKELY(m_node == nullptr))
{

View File

@@ -2725,7 +2725,7 @@ converted with static_cast, booleans give 0 or 1, and other types throw
type_error.302.
*/
template<typename T, typename BasicJsonType>
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<value_t>(n.kind))
{
@@ -3249,7 +3249,7 @@ class basic_json_view
/// maps with string keys, and views are converted directly; other types
/// through materialize().get<T>()
template<typename T>
T get() const
NLOHMANN_VIEW_ALWAYS_INLINE T get() const
{
return get_impl(detail::view::value_tag<T> {}, detail::priority_tag<2> {});
}
@@ -3336,7 +3336,7 @@ class basic_json_view
}
template < typename T, typename std::enable_if < std::is_arithmetic<T>::value && !std::is_same<T, bool>::value, int >::type = 0 >
T get_impl(detail::view::value_tag<T> /*unused*/, detail::priority_tag<2> /*unused*/) const
NLOHMANN_VIEW_ALWAYS_INLINE T get_impl(detail::view::value_tag<T> /*unused*/, detail::priority_tag<2> /*unused*/) const
{
if (NLOHMANN_VIEW_UNLIKELY(m_node == nullptr))
{