mirror of
https://github.com/nlohmann/json.git
synced 2026-10-22 14:08:13 +00:00
* Scan strings with SSE2, SSSE3, or NEON The string fast path found the end of a run of plain characters 8 bytes at a time (SWAR) and validated non-ASCII text one sequence at a time, calling validate_one_utf8() out of line for each. The vector code of json_view's scanner, in the new shared header detail/input/simd_scan.hpp, now does both 16 bytes at a time: SSE2 finds the end of a run on x86-64, and the UTF-8 check of Keiser and Lemire runs with NEON on AArch64 and with SSSE3 on x86-64. SSSE3 is not part of x86-64, so the check is compiled for it with a function attribute and used when the CPU has it (cached in a constant-initialized atomic), else the scalar check; code compiled with -mssse3 skips the CPU check. The first word of a run is still checked with SWAR (the first four with NEON, where a vector mask costs more), as most keys and short values end there, and the scalar helpers are always inlined. On anything else, the scalar code is unchanged. The new macro JSON_NO_SIMD selects the portable code everywhere, including the SSE2/NEON digit formatting of the floating-point writer. The lexer tests now also run with JSON_NO_SIMD and, on x86-64, with -mssse3, and a new test places every byte that ends a run at each offset across several 16-byte blocks, after ASCII and after CJK text. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Keep the string scan from slowing down short strings and dump() on Arm Three benchmarks were 7-9% slower than develop on Apple M1 (Apple Clang, -O3 -flto): Accept/jeopardy, Dump/citm_catalog, and Dump/jeopardy. - dump(): the serializer passes a string without its closing quote, so plain_run_end() ran out of input and called the out-of-line NEON loop for every string, with fewer than 16 bytes left. The loop is now only called for 16 bytes or more. - accept(): no string of jeopardy.json reaches the vector UTF-8 check, yet compiling it into the lexer's scan function made it 5% slower: the function runs once per token and now saved more registers and kept a stack slot. lexer_string_bulk_run() checks the first four words inline and continues out of line, and the serializer keeps string_bulk_run() inline, which is faster there. - The byte that ends a run is read from the scanned buffer instead of through get(), which Apple Clang merged with the other get() calls that load the read position again right after it was stored (3%). Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Define string_bulk_run_from() only where lexer_string_bulk_run() calls it With NEON and simdutf in C++17, lexer_string_bulk_run() takes the string_bulk_run() path, so the out-of-line helper was unused. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Do not dispatch the SSSE3 string scan with GCC's C++20 modules GCC does not keep the target("ssse3") attribute on functions it reads back from a module interface, so the module build (ci_module_cpp20, gcc:latest) rejected the intrinsics in vector_utf8_run() with "needs isa option -mssse3". With __cpp_modules, GCC now uses the portable UTF-8 check like other compilers without the target attribute; the SSE2 scan stays, and -mssse3 or more still compiles the SSSE3 check directly. Clang is not affected. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Include <cpuid.h> only once when json.hpp and json_view.hpp are both used The SSSE3 dispatch of the string scan and the one of json_view each include <cpuid.h>. Before GCC 11 the header has no include guard, so a translation unit that includes both headers fails with GCC 10: "redefinition of 'unsigned int __get_cpuid_max(unsigned int, unsigned int*)'". Guard both includes with NLOHMANN_JSON_CPUID_INCLUDED. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Add an ABI tag for JSON_NO_SIMD JSON_NO_SIMD selects between two definitions of the internal scanning and formatting functions. Like JSON_VIEW_NO_SIMD (_vns), it now appends _ns to the inline namespace, so translation units built with and without it can be linked into one program without ODR violations. Signed-off-by: Niels Lohmann <mail@nlohmann.me> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me>
210 lines
7.0 KiB
C++
210 lines
7.0 KiB
C++
// __ _____ _____ _____
|
|
// __| | __| | | | JSON for Modern C++
|
|
// | | |__ | | | | | | version 3.12.0
|
|
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
|
|
//
|
|
// SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
#pragma once
|
|
|
|
// This file contains all macro definitions affecting or depending on the ABI
|
|
|
|
#ifndef JSON_SKIP_LIBRARY_VERSION_CHECK
|
|
#if defined(NLOHMANN_JSON_VERSION_MAJOR) && defined(NLOHMANN_JSON_VERSION_MINOR) && defined(NLOHMANN_JSON_VERSION_PATCH)
|
|
#if NLOHMANN_JSON_VERSION_MAJOR != 3 || NLOHMANN_JSON_VERSION_MINOR != 12 || NLOHMANN_JSON_VERSION_PATCH != 0
|
|
#warning "Already included a different version of the library!"
|
|
#endif
|
|
#endif
|
|
#endif
|
|
|
|
#define NLOHMANN_JSON_VERSION_MAJOR 3 // NOLINT(modernize-macro-to-enum)
|
|
#define NLOHMANN_JSON_VERSION_MINOR 12 // NOLINT(modernize-macro-to-enum)
|
|
#define NLOHMANN_JSON_VERSION_PATCH 0 // NOLINT(modernize-macro-to-enum)
|
|
|
|
#ifndef JSON_DIAGNOSTICS
|
|
#define JSON_DIAGNOSTICS 0
|
|
#endif
|
|
|
|
#ifndef JSON_DIAGNOSTIC_POSITIONS
|
|
#define JSON_DIAGNOSTIC_POSITIONS 0
|
|
#endif
|
|
|
|
#ifndef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
|
|
#define JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON 0
|
|
#endif
|
|
|
|
#ifndef JSON_BRACE_INIT_COPY_SEMANTICS
|
|
#define JSON_BRACE_INIT_COPY_SEMANTICS 0
|
|
#endif
|
|
|
|
#ifndef JSON_PRECISE_STREAM_POSITION
|
|
#define JSON_PRECISE_STREAM_POSITION 0
|
|
#endif
|
|
|
|
#ifndef JSON_STRICT_NUL_HANDLING
|
|
#define JSON_STRICT_NUL_HANDLING 0
|
|
#endif
|
|
|
|
#ifndef JSON_STRICT_BINARY_UTF8
|
|
#define JSON_STRICT_BINARY_UTF8 0
|
|
#endif
|
|
|
|
#ifndef JSON_USE_OBJECTS_FOR_ENUM_KEYED_MAPS
|
|
#define JSON_USE_OBJECTS_FOR_ENUM_KEYED_MAPS 0
|
|
#endif
|
|
|
|
#ifndef JSON_DISABLE_STREAM_BULK_SCAN
|
|
#define JSON_DISABLE_STREAM_BULK_SCAN 0
|
|
#endif
|
|
|
|
#ifndef JSON_DISABLE_UNLOCKED_FILE_IO
|
|
#define JSON_DISABLE_UNLOCKED_FILE_IO 0
|
|
#endif
|
|
|
|
#if JSON_DIAGNOSTICS
|
|
#define NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS _diag
|
|
#else
|
|
#define NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS
|
|
#endif
|
|
|
|
#if JSON_DIAGNOSTIC_POSITIONS
|
|
#define NLOHMANN_JSON_ABI_TAG_DIAGNOSTIC_POSITIONS _dp
|
|
#else
|
|
#define NLOHMANN_JSON_ABI_TAG_DIAGNOSTIC_POSITIONS
|
|
#endif
|
|
|
|
#if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
|
|
#define NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON _ldvcmp
|
|
#else
|
|
#define NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON
|
|
#endif
|
|
|
|
#if JSON_BRACE_INIT_COPY_SEMANTICS
|
|
#define NLOHMANN_JSON_ABI_TAG_BRACE_INIT_COPY_SEMANTICS _bics
|
|
#else
|
|
#define NLOHMANN_JSON_ABI_TAG_BRACE_INIT_COPY_SEMANTICS
|
|
#endif
|
|
|
|
#if JSON_PRECISE_STREAM_POSITION
|
|
#define NLOHMANN_JSON_ABI_TAG_PRECISE_STREAM_POSITION _psp
|
|
#else
|
|
#define NLOHMANN_JSON_ABI_TAG_PRECISE_STREAM_POSITION
|
|
#endif
|
|
|
|
#if JSON_STRICT_NUL_HANDLING
|
|
#define NLOHMANN_JSON_ABI_TAG_STRICT_NUL_HANDLING _snul
|
|
#else
|
|
#define NLOHMANN_JSON_ABI_TAG_STRICT_NUL_HANDLING
|
|
#endif
|
|
|
|
#if JSON_STRICT_BINARY_UTF8
|
|
#define NLOHMANN_JSON_ABI_TAG_STRICT_BINARY_UTF8 _sbu8
|
|
#else
|
|
#define NLOHMANN_JSON_ABI_TAG_STRICT_BINARY_UTF8
|
|
#endif
|
|
|
|
#if JSON_USE_OBJECTS_FOR_ENUM_KEYED_MAPS
|
|
#define NLOHMANN_JSON_ABI_TAG_OBJECTS_FOR_ENUM_KEYED_MAPS _ekmo
|
|
#else
|
|
#define NLOHMANN_JSON_ABI_TAG_OBJECTS_FOR_ENUM_KEYED_MAPS
|
|
#endif
|
|
|
|
#if JSON_DISABLE_STREAM_BULK_SCAN
|
|
#define NLOHMANN_JSON_ABI_TAG_DISABLE_STREAM_BULK_SCAN _dsbs
|
|
#else
|
|
#define NLOHMANN_JSON_ABI_TAG_DISABLE_STREAM_BULK_SCAN
|
|
#endif
|
|
|
|
#if JSON_DISABLE_UNLOCKED_FILE_IO
|
|
#define NLOHMANN_JSON_ABI_TAG_DISABLE_UNLOCKED_FILE_IO _dufio
|
|
#else
|
|
#define NLOHMANN_JSON_ABI_TAG_DISABLE_UNLOCKED_FILE_IO
|
|
#endif
|
|
|
|
#ifdef JSON_NO_SIMD
|
|
#define NLOHMANN_JSON_ABI_TAG_NO_SIMD _ns
|
|
#else
|
|
#define NLOHMANN_JSON_ABI_TAG_NO_SIMD
|
|
#endif
|
|
|
|
#ifdef JSON_VIEW_NO_SIMD
|
|
#define NLOHMANN_JSON_ABI_TAG_VIEW_NO_SIMD _vns
|
|
#else
|
|
#define NLOHMANN_JSON_ABI_TAG_VIEW_NO_SIMD
|
|
#endif
|
|
|
|
#ifdef JSON_VIEW_USE_SSSE3
|
|
#define NLOHMANN_JSON_ABI_TAG_VIEW_USE_SSSE3 _vssse3
|
|
#else
|
|
#define NLOHMANN_JSON_ABI_TAG_VIEW_USE_SSSE3
|
|
#endif
|
|
|
|
#ifndef NLOHMANN_JSON_NAMESPACE_NO_VERSION
|
|
#define NLOHMANN_JSON_NAMESPACE_NO_VERSION 0
|
|
#endif
|
|
|
|
// Construct the namespace ABI tags component
|
|
#define NLOHMANN_JSON_ABI_TAGS_CONCAT_EX(a, b, c, d, e, f, g, h, i, j, k, l, m) json_abi ## a ## b ## c ## d ## e ## f ## g ## h ## i ## j ## k ## l ## m
|
|
#define NLOHMANN_JSON_ABI_TAGS_CONCAT(a, b, c, d, e, f, g, h, i, j, k, l, m) \
|
|
NLOHMANN_JSON_ABI_TAGS_CONCAT_EX(a, b, c, d, e, f, g, h, i, j, k, l, m)
|
|
|
|
#define NLOHMANN_JSON_ABI_TAGS \
|
|
NLOHMANN_JSON_ABI_TAGS_CONCAT( \
|
|
NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS, \
|
|
NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON, \
|
|
NLOHMANN_JSON_ABI_TAG_DIAGNOSTIC_POSITIONS, \
|
|
NLOHMANN_JSON_ABI_TAG_BRACE_INIT_COPY_SEMANTICS, \
|
|
NLOHMANN_JSON_ABI_TAG_PRECISE_STREAM_POSITION, \
|
|
NLOHMANN_JSON_ABI_TAG_STRICT_NUL_HANDLING, \
|
|
NLOHMANN_JSON_ABI_TAG_STRICT_BINARY_UTF8, \
|
|
NLOHMANN_JSON_ABI_TAG_OBJECTS_FOR_ENUM_KEYED_MAPS, \
|
|
NLOHMANN_JSON_ABI_TAG_DISABLE_STREAM_BULK_SCAN, \
|
|
NLOHMANN_JSON_ABI_TAG_DISABLE_UNLOCKED_FILE_IO, \
|
|
NLOHMANN_JSON_ABI_TAG_NO_SIMD, \
|
|
NLOHMANN_JSON_ABI_TAG_VIEW_NO_SIMD, \
|
|
NLOHMANN_JSON_ABI_TAG_VIEW_USE_SSSE3)
|
|
|
|
// Construct the namespace version component
|
|
#define NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT_EX(major, minor, patch) \
|
|
_v ## major ## _ ## minor ## _ ## patch
|
|
#define NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT(major, minor, patch) \
|
|
NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT_EX(major, minor, patch)
|
|
|
|
#if NLOHMANN_JSON_NAMESPACE_NO_VERSION
|
|
#define NLOHMANN_JSON_NAMESPACE_VERSION
|
|
#else
|
|
#define NLOHMANN_JSON_NAMESPACE_VERSION \
|
|
NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT(NLOHMANN_JSON_VERSION_MAJOR, \
|
|
NLOHMANN_JSON_VERSION_MINOR, \
|
|
NLOHMANN_JSON_VERSION_PATCH)
|
|
#endif
|
|
|
|
// Combine namespace components
|
|
#define NLOHMANN_JSON_NAMESPACE_CONCAT_EX(a, b) a ## b
|
|
#define NLOHMANN_JSON_NAMESPACE_CONCAT(a, b) \
|
|
NLOHMANN_JSON_NAMESPACE_CONCAT_EX(a, b)
|
|
|
|
#ifndef NLOHMANN_JSON_NAMESPACE
|
|
#define NLOHMANN_JSON_NAMESPACE \
|
|
nlohmann::NLOHMANN_JSON_NAMESPACE_CONCAT( \
|
|
NLOHMANN_JSON_ABI_TAGS, \
|
|
NLOHMANN_JSON_NAMESPACE_VERSION)
|
|
#endif
|
|
|
|
#ifndef NLOHMANN_JSON_NAMESPACE_BEGIN
|
|
#define NLOHMANN_JSON_NAMESPACE_BEGIN \
|
|
namespace nlohmann \
|
|
{ \
|
|
inline namespace NLOHMANN_JSON_NAMESPACE_CONCAT( \
|
|
NLOHMANN_JSON_ABI_TAGS, \
|
|
NLOHMANN_JSON_NAMESPACE_VERSION) \
|
|
{
|
|
#endif
|
|
|
|
#ifndef NLOHMANN_JSON_NAMESPACE_END
|
|
#define NLOHMANN_JSON_NAMESPACE_END \
|
|
} /* namespace (inline namespace) NOLINT(readability/namespace) */ \
|
|
} // namespace nlohmann
|
|
#endif
|