Sfoglia il codice sorgente

perf(wal): use yyjson for replay parses (phase A)

WAL replay calls nlohmann::json::parse twice per replayed entry — once
for the doc body and once for collection-options entries. Swap both to
smartbotic::db::parse_to_nlohmann. yyjson parses the same bytes 2-3×
faster on Zoe-shape docs, which dominates cold-boot recovery time.

Wire and on-disk bytes unchanged; exception type still
nlohmann::json::parse_error so existing catch sites in the recovery
loop are unaffected.

Test build-system: tests/CMakeLists.txt now adds json_parse.cpp to
every test target that compiles wal.cpp (test_vector_storage,
test_timestamp_precision, test_snapshot_durability, test_eviction) and
links yyjson into them. The yyjson defensive find_package block was
hoisted from the test_json_parse-local position to file scope so all
test targets resolve yyjson_LIBRARIES / yyjson_INCLUDE_DIRS.

Verified:
  test_snapshot_durability  — All snapshot durability tests PASSED
  test_vector_storage       — All vector storage tests PASSED
  test_timestamp_precision  — All timestamp precision tests PASSED
  test_eviction             — All eviction tests PASSED
  test_json_parse           — still passes
fszontagh 2 mesi fa
parent
commit
1787b3f6e0
2 ha cambiato i file con 34 aggiunte e 2 eliminazioni
  1. 4 2
      service/src/persistence/wal.cpp
  2. 30 0
      tests/CMakeLists.txt

+ 4 - 2
service/src/persistence/wal.cpp

@@ -1,5 +1,7 @@
 #include "wal.hpp"
 
+#include "../json_parse.hpp"
+
 #include <algorithm>
 #include <chrono>
 #include <cstring>
@@ -230,7 +232,7 @@ std::optional<WalEntry> WalEntry::deserialize(const std::vector<uint8_t>& data)
         std::string dataStr(reinterpret_cast<const char*>(&data[offset]), dataLen);
         offset += dataLen;
         try {
-            entry.data = nlohmann::json::parse(dataStr);
+            entry.data = smartbotic::db::parse_to_nlohmann(dataStr);
         } catch (const nlohmann::json::exception&) {
             return std::nullopt;
         }
@@ -246,7 +248,7 @@ std::optional<WalEntry> WalEntry::deserialize(const std::vector<uint8_t>& data)
         std::string optsStr(reinterpret_cast<const char*>(&data[offset]), optsLen);
         offset += optsLen;
         try {
-            entry.collectionOptions = CollectionOptions::fromJson(nlohmann::json::parse(optsStr));
+            entry.collectionOptions = CollectionOptions::fromJson(smartbotic::db::parse_to_nlohmann(optsStr));
         } catch (const nlohmann::json::exception&) {
             return std::nullopt;
         }

+ 30 - 0
tests/CMakeLists.txt

@@ -1,5 +1,15 @@
 # Tests for smartbotic-database
 
+# yyjson (v1.10+) — Phase A swaps nlohmann::json::parse for parse_to_nlohmann
+# at hot paths in wal.cpp, snapshot.cpp, history_store.cpp, etc. Any test
+# target that compiles those files now also needs json_parse.cpp linked and
+# yyjson linked. Resolve once at file scope so every test target below can
+# use ${yyjson_LIBRARIES} / ${yyjson_INCLUDE_DIRS}.
+if(NOT yyjson_FOUND)
+    find_package(PkgConfig REQUIRED)
+    pkg_check_modules(yyjson REQUIRED yyjson)
+endif()
+
 # Vector storage integration test
 # Compiles memory_store.cpp directly — no dependency on the full service executable.
 set(TEST_VECTOR_SOURCES
@@ -8,6 +18,7 @@ set(TEST_VECTOR_SOURCES
     ${CMAKE_CURRENT_SOURCE_DIR}/../service/src/config/collection_config_manager.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/../service/src/persistence/history_store.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/../service/src/persistence/wal.cpp
+    ${CMAKE_CURRENT_SOURCE_DIR}/../service/src/json_parse.cpp
 )
 
 add_executable(test_vector_storage ${TEST_VECTOR_SOURCES})
@@ -35,6 +46,10 @@ endif()
 find_package(Threads REQUIRED)
 target_link_libraries(test_vector_storage PRIVATE Threads::Threads)
 
+# yyjson (wal.cpp uses parse_to_nlohmann from v1.10+)
+target_link_libraries(test_vector_storage PRIVATE ${yyjson_LIBRARIES})
+target_include_directories(test_vector_storage PRIVATE ${yyjson_INCLUDE_DIRS})
+
 # View projection unit test
 set(TEST_VIEWS_SOURCES
     test_views.cpp
@@ -61,6 +76,7 @@ set(TEST_TIMESTAMP_PRECISION_SOURCES
     ${CMAKE_CURRENT_SOURCE_DIR}/../service/src/config/collection_config_manager.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/../service/src/persistence/history_store.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/../service/src/persistence/wal.cpp
+    ${CMAKE_CURRENT_SOURCE_DIR}/../service/src/json_parse.cpp
 )
 
 add_executable(test_timestamp_precision ${TEST_TIMESTAMP_PRECISION_SOURCES})
@@ -85,6 +101,10 @@ endif()
 find_package(Threads REQUIRED)
 target_link_libraries(test_timestamp_precision PRIVATE Threads::Threads)
 
+# yyjson (wal.cpp uses parse_to_nlohmann from v1.10+)
+target_link_libraries(test_timestamp_precision PRIVATE ${yyjson_LIBRARIES})
+target_include_directories(test_timestamp_precision PRIVATE ${yyjson_INCLUDE_DIRS})
+
 # Snapshot durability + fallback integration test
 # Exercises SnapshotManager directly; pulls in memory_store.cpp, snapshot.cpp,
 # and wal.cpp (for the crc32 helpers used by snapshot.cpp).
@@ -95,6 +115,7 @@ set(TEST_SNAPSHOT_DURABILITY_SOURCES
     ${CMAKE_CURRENT_SOURCE_DIR}/../service/src/persistence/snapshot.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/../service/src/persistence/wal.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/../service/src/persistence/history_store.cpp
+    ${CMAKE_CURRENT_SOURCE_DIR}/../service/src/json_parse.cpp
 )
 
 add_executable(test_snapshot_durability ${TEST_SNAPSHOT_DURABILITY_SOURCES})
@@ -119,6 +140,10 @@ endif()
 find_package(Threads REQUIRED)
 target_link_libraries(test_snapshot_durability PRIVATE Threads::Threads)
 
+# yyjson (wal.cpp/snapshot.cpp use parse_to_nlohmann from v1.10+)
+target_link_libraries(test_snapshot_durability PRIVATE ${yyjson_LIBRARIES})
+target_include_directories(test_snapshot_durability PRIVATE ${yyjson_INCLUDE_DIRS})
+
 # LZ4 — snapshot.cpp #includes <lz4.h> unless DISABLE_LZ4 is defined, so link it.
 find_package(PkgConfig QUIET)
 if(PKG_CONFIG_FOUND)
@@ -167,6 +192,7 @@ set(TEST_EVICTION_SOURCES
     ${CMAKE_CURRENT_SOURCE_DIR}/../service/src/config/collection_config_manager.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/../service/src/persistence/history_store.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/../service/src/persistence/wal.cpp
+    ${CMAKE_CURRENT_SOURCE_DIR}/../service/src/json_parse.cpp
 )
 
 add_executable(test_eviction ${TEST_EVICTION_SOURCES})
@@ -191,6 +217,10 @@ endif()
 find_package(Threads REQUIRED)
 target_link_libraries(test_eviction PRIVATE Threads::Threads)
 
+# yyjson (wal.cpp uses parse_to_nlohmann from v1.10+)
+target_link_libraries(test_eviction PRIVATE ${yyjson_LIBRARIES})
+target_include_directories(test_eviction PRIVATE ${yyjson_INCLUDE_DIRS})
+
 # yyjson → nlohmann bridge helper test (v1.10 Phase A T2)
 # Exercises parse_to_nlohmann() in isolation against nlohmann::json::parse
 # for a corpus of production-shaped docs. yyjson is configured at the