diff --git a/include/nlohmann/detail/view/object_index.hpp b/include/nlohmann/detail/view/object_index.hpp index 54a429f9c..9b0ba63ea 100644 --- a/include/nlohmann/detail/view/object_index.hpp +++ b/include/nlohmann/detail/view/object_index.hpp @@ -94,6 +94,49 @@ inline std::uint64_t key_hash(const char* s, std::size_t n) noexcept return h ^ (h >> 31u); } +/// whether the n <= 16 bytes at a and b are equal, as two overlapping words +/// (the first and the last 8, 4, or 2 bytes) and without a call +NLOHMANN_VIEW_ALWAYS_INLINE bool same_short_bytes(const unsigned char* a, const unsigned char* b, std::size_t n) noexcept +{ + if (n >= 8) + { + std::uint64_t x0 = 0; + std::uint64_t x1 = 0; + std::uint64_t y0 = 0; + std::uint64_t y1 = 0; + std::memcpy(&x0, a, 8); + std::memcpy(&x1, a + n - 8, 8); + std::memcpy(&y0, b, 8); + std::memcpy(&y1, b + n - 8, 8); + return x0 == y0 && x1 == y1; + } + if (n >= 4) + { + std::uint32_t x0 = 0; + std::uint32_t x1 = 0; + std::uint32_t y0 = 0; + std::uint32_t y1 = 0; + std::memcpy(&x0, a, 4); + std::memcpy(&x1, a + n - 4, 4); + std::memcpy(&y0, b, 4); + std::memcpy(&y1, b + n - 4, 4); + return x0 == y0 && x1 == y1; + } + if (n >= 2) + { + std::uint16_t x0 = 0; + std::uint16_t x1 = 0; + std::uint16_t y0 = 0; + std::uint16_t y1 = 0; + std::memcpy(&x0, a, 2); + std::memcpy(&x1, a + n - 2, 2); + std::memcpy(&y0, b, 2); + std::memcpy(&y1, b + n - 2, 2); + return x0 == y0 && x1 == y1; + } + return n == 0 || *a == *b; +} + /// build the table of a large object inline void build_object_index(document_data& d, node* obj) { @@ -158,6 +201,27 @@ inline const node* find_indexed(const document_data& d, const node* obj, const c const document_data::object_index& ix = d.indexes[obj->extra - 1u]; const std::uint32_t* const table = d.index_slots.data() + ix.start; std::size_t slot = conditional_static_cast(key_hash(key, n)) & ix.mask; + const auto* const k_bytes = reinterpret_cast(key); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) + // (the loop for longer keys comes first, as the code is laid out; it is not the more common case) + if (NLOHMANN_VIEW_UNLIKELY(n <= 16)) + { + // (up to 16 bytes, the bytes are compared as two words, without a call) + for (std::size_t distance = 0; distance <= index_max_displacement; ++distance) + { + const std::uint32_t s = table[slot]; + if (s == 0) + { + return nullptr; + } + const node* const k = obj + s; + if (k->len == n && same_short_bytes(reinterpret_cast(d.str(*k)), k_bytes, n)) // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) + { + return k; + } + slot = (slot + 1) & ix.mask; + } + return nullptr; + } for (std::size_t distance = 0; distance <= index_max_displacement; ++distance) { const std::uint32_t s = table[slot]; diff --git a/single_include/nlohmann/json_view.hpp b/single_include/nlohmann/json_view.hpp index ef5da68d7..960d204d7 100644 --- a/single_include/nlohmann/json_view.hpp +++ b/single_include/nlohmann/json_view.hpp @@ -3328,6 +3328,49 @@ inline std::uint64_t key_hash(const char* s, std::size_t n) noexcept return h ^ (h >> 31u); } +/// whether the n <= 16 bytes at a and b are equal, as two overlapping words +/// (the first and the last 8, 4, or 2 bytes) and without a call +NLOHMANN_VIEW_ALWAYS_INLINE bool same_short_bytes(const unsigned char* a, const unsigned char* b, std::size_t n) noexcept +{ + if (n >= 8) + { + std::uint64_t x0 = 0; + std::uint64_t x1 = 0; + std::uint64_t y0 = 0; + std::uint64_t y1 = 0; + std::memcpy(&x0, a, 8); + std::memcpy(&x1, a + n - 8, 8); + std::memcpy(&y0, b, 8); + std::memcpy(&y1, b + n - 8, 8); + return x0 == y0 && x1 == y1; + } + if (n >= 4) + { + std::uint32_t x0 = 0; + std::uint32_t x1 = 0; + std::uint32_t y0 = 0; + std::uint32_t y1 = 0; + std::memcpy(&x0, a, 4); + std::memcpy(&x1, a + n - 4, 4); + std::memcpy(&y0, b, 4); + std::memcpy(&y1, b + n - 4, 4); + return x0 == y0 && x1 == y1; + } + if (n >= 2) + { + std::uint16_t x0 = 0; + std::uint16_t x1 = 0; + std::uint16_t y0 = 0; + std::uint16_t y1 = 0; + std::memcpy(&x0, a, 2); + std::memcpy(&x1, a + n - 2, 2); + std::memcpy(&y0, b, 2); + std::memcpy(&y1, b + n - 2, 2); + return x0 == y0 && x1 == y1; + } + return n == 0 || *a == *b; +} + /// build the table of a large object inline void build_object_index(document_data& d, node* obj) { @@ -3392,6 +3435,27 @@ inline const node* find_indexed(const document_data& d, const node* obj, const c const document_data::object_index& ix = d.indexes[obj->extra - 1u]; const std::uint32_t* const table = d.index_slots.data() + ix.start; std::size_t slot = conditional_static_cast(key_hash(key, n)) & ix.mask; + const auto* const k_bytes = reinterpret_cast(key); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) + // (the loop for longer keys comes first, as the code is laid out; it is not the more common case) + if (NLOHMANN_VIEW_UNLIKELY(n <= 16)) + { + // (up to 16 bytes, the bytes are compared as two words, without a call) + for (std::size_t distance = 0; distance <= index_max_displacement; ++distance) + { + const std::uint32_t s = table[slot]; + if (s == 0) + { + return nullptr; + } + const node* const k = obj + s; + if (k->len == n && same_short_bytes(reinterpret_cast(d.str(*k)), k_bytes, n)) // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) + { + return k; + } + slot = (slot + 1) & ix.mask; + } + return nullptr; + } for (std::size_t distance = 0; distance <= index_max_displacement; ++distance) { const std::uint32_t s = table[slot]; diff --git a/tests/src/unit-json_view_dump.cpp b/tests/src/unit-json_view_dump.cpp index db2a5e2a1..7e3018d7c 100644 --- a/tests/src/unit-json_view_dump.cpp +++ b/tests/src/unit-json_view_dump.cpp @@ -671,6 +671,59 @@ TEST_CASE("json_view large objects") } } + SECTION("keys of every length that differ in one byte") + { + // In a large object, a key is found by comparing its bytes with those of the member the + // table points to: up to 16 bytes as two overlapping words, longer ones with memcmp. + // Keys of every length from 0 to 40, and keys that differ from them in one byte. + std::mt19937_64 rng(2026); + std::vector keys; + std::set distinct; + for (std::size_t length = 0; length <= 40; ++length) + { + for (int copy = 0; copy < 5; ++copy) + { + std::string key(length, 'a'); + for (char& c : key) + { + c = static_cast('a' + (rng() % 26)); + } + if (distinct.insert(key).second) + { + keys.push_back(key); + } + } + } + std::string text = "{"; + for (std::size_t i = 0; i < keys.size(); ++i) + { + text += (i != 0 ? ",\"" : "\"") + keys[i] + "\":" + std::to_string(i); + } + text += "}"; + REQUIRE(keys.size() >= 128); // (a table is built for 128 members) + const json_document d = json_document::parse(text); + const json_view v = d.root(); + for (std::size_t i = 0; i < keys.size(); ++i) + { + CAPTURE(keys[i]) + CHECK(v[keys[i]].get() == i); + CHECK(v.at(keys[i]).get() == i); + CHECK(v.contains(keys[i])); + for (std::size_t at = 0; at < keys[i].size(); ++at) + { + std::string other = keys[i]; + other[at] = other[at] == 'z' ? 'y' : 'z'; + CAPTURE(other) + CHECK(v.contains(other) == (distinct.count(other) != 0)); + } + CHECK(v.contains(keys[i] + "a") == (distinct.count(keys[i] + "a") != 0)); + if (!keys[i].empty()) + { + CHECK(v.contains(keys[i].substr(1)) == (distinct.count(keys[i].substr(1)) != 0)); + } + } + } + SECTION("shrink_to_fit releases the tables' spare capacity") { const auto make_text = [](int objects, int members)