mirror of
https://github.com/nlohmann/json.git
synced 2026-11-01 02:15:42 +00:00
accept() checks strings without copying them
json::accept() parses through json_sax_acceptor, which ignores the values of strings and object keys, but the lexer still copied every string token into its token buffer. That copy was 18 % of the instructions of json::accept() on twitter.json (x86, GCC 16). The lexer's flag for accept(), renamed from discard_number_values to discard_values, now also selects a second instantiation of scan_string() that performs the same checks (escapes, surrogates, UTF-8, control characters, with the same error messages) without writing to the token buffer. scan() chooses the instantiation once per string: a run-time check inside the string loop made number-heavy files up to 3 % slower on Apple M1. json::accept() over 19 files, time after/before (geomean): 0.876 on x86 with GCC 16.2, 0.884 with Clang 23, 0.932 on Apple M1; up to 0.74 on single files; files with few strings (canada, mesh, numbers) unchanged. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
5 files changed
+218
-90
No files matched your search
@@ -89,9 +89,9 @@ class parser
|
||||
const bool allow_exceptions_ = true,
|
||||
const bool ignore_comments = false,
|
||||
const bool ignore_trailing_commas_ = false,
|
||||
const bool discard_number_values_ = false)
|
||||
const bool discard_values_ = false)
|
||||
: callback(std::move(cb))
|
||||
, m_lexer(std::move(adapter), ignore_comments, discard_number_values_)
|
||||
, m_lexer(std::move(adapter), ignore_comments, discard_values_)
|
||||
, allow_exceptions(allow_exceptions_)
|
||||
, ignore_trailing_commas(ignore_trailing_commas_)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user