accept(): its own lexer instead of a run-time choice in scan()

The lexer chose between the copying and the checking string scan at run time
in scan(). Both scans were then inlined into the scan() of parse() as well,
which made json::parse() of jeopardy.json indented by 4 2 % slower on Apple
M1 (same instruction count). Whether token values are discarded is now a
template parameter (DiscardValues) of lexer and parser, false by default;
only accept() instantiates the parser with true. parse() gets a lexer with
the copying scan only, and without the run-time checks of the number paths.

Apple M1, A/B against develop, 6 code layouts: parse() of indented jeopardy
1.020 -> 1.003, twitter 1.006 -> 1.005; accept() twitter 1.012 -> 1.002,
tree-pretty 1.016 -> 1.004, mesh 1.000 -> 0.940, twitter_min 0.990 -> 1.009,
citm_catalog 0.937 -> 0.950, gsoc-2018 0.838 -> 0.836.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann committed 2026-10-31 20:22:03 +01:00
1 parent 9f654201bc
commit 14913168dd
4 files changed
+60 -44

No files matched your search

+6 -5
View File
@@ -150,7 +150,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
// can be restored when json_pointer backwards compatibility is removed
// friend ::nlohmann::json_pointer<StringType>;
template<typename BasicJsonType, typename InputType>
template<typename BasicJsonType, typename InputType, bool DiscardValues>
friend class ::nlohmann::detail::parser;
friend ::nlohmann::detail::serializer<basic_json>;
template<typename BasicJsonType>
@@ -183,12 +183,11 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
detail::parser_callback_t<basic_json>cb = nullptr,
const bool allow_exceptions = true,
const bool ignore_comments = false,
const bool ignore_trailing_commas = false,
const bool discard_values = false
const bool ignore_trailing_commas = false
)
{
return ::nlohmann::detail::parser<basic_json, InputAdapterType>(std::move(adapter),
std::move(cb), allow_exceptions, ignore_comments, ignore_trailing_commas, discard_values);
std::move(cb), allow_exceptions, ignore_comments, ignore_trailing_commas);
}
private:
@@ -3117,7 +3116,9 @@ public:
const bool ignore_comments,
const bool ignore_trailing_commas)
{
return parser(std::forward<InputAdapterType>(adapter), nullptr, false, ignore_comments, ignore_trailing_commas, true).accept(true);
// the parser and lexer that discard the values of tokens (see lexer)
return ::nlohmann::detail::parser<basic_json, typename std::decay<InputAdapterType>::type, true>(std::forward<InputAdapterType>(adapter),
nullptr, false, ignore_comments, ignore_trailing_commas).accept(true);
}
/// generate SAX events from the input behind an already created adapter