Files
json/docs/mkdocs/docs/api/macros/json_view_use_ssse3.md
Niels Lohmann 3058d527ce Scan json_view strings with SIMD and index large objects
Speed up json_view's parser with SIMD scanning and a hash table
for large objects.

Long runs of string bytes are scanned 16 bytes at a time with NEON
(AArch64, GCC and Clang) and SSE2 (x86-64), both baseline
instruction sets. Keys keep 16 table checks before the vector
loop, because their lengths repeat from record to record; string
values get 8, because their lengths vary more. Non-ASCII text is
validated 16 bytes at a time with simdjson's "lookup4" check
(Keiser and Lemire, 2021), with NEON on AArch64 and, on x86-64,
with SSSE3. SSSE3 is not part of baseline x86-64, so the check is
compiled for SSSE3 with a function attribute and used only where
CPUID reports it, which all x86-64 CPUs since about 2011 do; the
answer is cached in a statically initialized atomic, so there is
no guard of a local static and no global constructor. The same
input is accepted either way. JSON_VIEW_NO_SIMD selects the
portable code.

On x86-64, string runs are now checked vector-first: one SSE2
compare from the first byte finds the end of most keys and short
values, instead of a branch per byte for the first 8-16 bytes.
AArch64 keeps the byte-wise steps, where a NEON mask costs more and
the branches predict well. Entering an object or array no longer
stalls: open() stores the parent's frame field by field instead of
building it on the stack and reading it back with wider loads,
which waited for the narrower stores to retire.

Objects with 128 members or more get an open-addressing hash table
built when the object closes, so operator[], at(), find(),
contains(), count(), value(), and JSON pointers take constant time
on average in such objects; of duplicate keys, the first is kept,
as for the linear search. The idea comes from Boost.JSON.

simdjson is credited in simd.hpp's SPDX block, the README, and
license.md.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-11 11:30:55 +02:00

2.1 KiB

JSON_VIEW_USE_SSSE3

#define JSON_VIEW_USE_SSSE3

When defined on x86-64, the parser of basic_json_document (<nlohmann/json_view.hpp>) validates non-ASCII text in strings with SSSE3 without asking the CPU first.

By default, the parser checks once at run time whether the CPU has SSSE3 (all x86-64 CPUs since about 2011 have it) and then validates non-ASCII text 16 bytes at a time, using the "lookup4" algorithm of simdjson; on CPUs without SSSE3, it validates one UTF-8 sequence at a time. The vector check is compiled for SSSE3 with a function attribute (GCC 4.9 and later, Clang), so this needs no compiler option. With MSVC, the check uses __cpuid. On AArch64, the vector check uses NEON and is always on.

Define the macro only together with a compiler option that enables SSSE3 (e.g. -mssse3, or -march= with a CPU that has it), and only for programs that run on such CPUs. It saves the check of the CPU, which costs little. The same input is accepted or rejected either way; only the speed of non-ASCII text differs.

!!! warning "Define consistently"

The macro selects between two definitions of the same inline functions. It must therefore be defined identically,
with the same compiler options, for **every** translation unit that includes `<nlohmann/json_view.hpp>`; mixing
translation units that define it with ones that do not is an ODR violation. Prefer a compile definition on the
target.

Default definition

By default, #!cpp JSON_VIEW_USE_SSSE3 is not defined.

#undef JSON_VIEW_USE_SSSE3

Examples

??? example

With CMake, for a program that only runs on CPUs with SSSE3:

```cmake
target_compile_definitions(your_target PRIVATE JSON_VIEW_USE_SSSE3)
target_compile_options(your_target PRIVATE -mssse3)
```

See also

Version history

  • Added in version 3.13.0.