json_view: keep GCC's inlining of view walks with the diagnostics macros

Measured on GCC 13 (x86-64) with JSON_DIAGNOSTICS or JSON_DIAGNOSTIC_POSITIONS,
a recursive walk over a view took 2.7 to 2.9 times the instructions: GCC no
longer inlined the walk into itself. The cause was not the walk's code but
that the path and range code of errors.hpp was instantiated in every
translation unit (the non-template throw helpers referenced it), which
changed GCC's early inlining of unrelated view functions. diagnostics() and
the throw helpers that take an error_site are now templates, so that code is
instantiated only where an exception can be thrown; the walks have the
instruction counts of develop again.

Iterators no longer keep their container: with it, one walk was still 1.1 %
slower. key() of an iterator over an array or a primitive value
(invalid_iterator.207) is therefore thrown without a context, as documented.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann committed 2026-10-30 22:46:49 +01:00
1 parent 8ac510b8d3
commit 429afd4fc0
7 files changed
+73 -81

No files matched your search

+5 -5
View File
@@ -481,7 +481,7 @@ class basic_json_view
return end();
}
const node* const k = find_key(key);
return k != nullptr ? iterator(m_doc, k, true, m_node) : end();
return k != nullptr ? iterator(m_doc, k, true) : end();
}
iterator find(const char* key) const
@@ -544,18 +544,18 @@ class basic_json_view
{
if (NLOHMANN_VIEW_LIKELY(is_structured()))
{
return iterator(m_doc, navigation::first(*m_doc, m_node), is_object(), m_node);
return iterator(m_doc, navigation::first(*m_doc, m_node), is_object());
}
return iterator(m_doc, m_node, false, m_node);
return iterator(m_doc, m_node, false);
}
NLOHMANN_VIEW_ALWAYS_INLINE iterator end() const noexcept
{
if (NLOHMANN_VIEW_LIKELY(is_structured()))
{
return iterator(m_doc, navigation::end(*m_doc, m_node), is_object(), m_node);
return iterator(m_doc, navigation::end(*m_doc, m_node), is_object());
}
return iterator(m_doc, (is_null() || is_discarded()) ? m_node : m_node + 1, false, m_node);
return iterator(m_doc, (is_null() || is_discarded()) ? m_node : m_node + 1, false);
}
iterator cbegin() const noexcept