浏览代码

perf(grpc): use yyjson for handler body parses (phase A)

Swap all 10 nlohmann::json::parse callsites in the gRPC handler file to
smartbotic::db::parse_to_nlohmann:

  Insert (line 109 + 187)
  Update (line 222)
  Patch  (line 307)
  Find   (line 362)
  Subscribe initial-state (line 592)
  Similarity-search proto (line 708)
  Doc-from-proto helper   (line 1288)
  Filter value parses     (line 1344, 1397)

Five of the call sites passed iterators (request->data().begin/end)
because nlohmann's parse() is iterator-templated. parse_to_nlohmann
only accepts string_view/(char*,size_t) — yyjson needs a contiguous
buffer — so those sites now pass the protobuf string directly via
string_view conversion. Functionally identical, simpler call shape.

Wire bytes unchanged. parse_error catch sites in each handler
unaffected.
fszontagh 2 月之前
父节点
当前提交
b35694b681
共有 1 个文件被更改,包括 11 次插入20 次删除
  1. 11 20
      service/src/database_grpc_impl.cpp

+ 11 - 20
service/src/database_grpc_impl.cpp

@@ -1,5 +1,6 @@
 #include "database_grpc_impl.hpp"
 #include "database_service.hpp"
+#include "json_parse.hpp"
 #include "persistence/wal.hpp"
 #include "views/projection.hpp"
 
@@ -106,9 +107,7 @@ grpc::Status DatabaseGrpcImpl::Insert(
             "cannot write to view '" + request->collection() + "': views are read-only");
     }
     try {
-        nlohmann::json data = nlohmann::json::parse(
-            request->data().begin(), request->data().end()
-        );
+        nlohmann::json data = smartbotic::db::parse_to_nlohmann(request->data());
 
         Document doc;
         doc.id = request->id();
@@ -184,7 +183,7 @@ grpc::Status DatabaseGrpcImpl::Get(
 
     // Apply view projection on the response data
     if (view) {
-        nlohmann::json docJson = nlohmann::json::parse(protoDoc.data());
+        nlohmann::json docJson = smartbotic::db::parse_to_nlohmann(protoDoc.data());
         docJson = applyProjection(docJson, view->include, view->exclude);
         protoDoc.set_data(docJson.dump());
         // Preserve view name as the observed collection
@@ -219,9 +218,7 @@ grpc::Status DatabaseGrpcImpl::Update(
         return grpc::Status::OK;
     }
     try {
-        nlohmann::json data = nlohmann::json::parse(
-            request->data().begin(), request->data().end()
-        );
+        nlohmann::json data = smartbotic::db::parse_to_nlohmann(request->data());
 
         Document doc;
         doc.data = data;
@@ -304,9 +301,7 @@ grpc::Status DatabaseGrpcImpl::PatchDocument(
         return grpc::Status::OK;
     }
     try {
-        nlohmann::json patch = nlohmann::json::parse(
-            request->patch_json().begin(), request->patch_json().end()
-        );
+        nlohmann::json patch = smartbotic::db::parse_to_nlohmann(request->patch_json());
 
         uint64_t newVersion = store_.patchDocument(
             request->collection(),
@@ -359,9 +354,7 @@ grpc::Status DatabaseGrpcImpl::Upsert(
             "cannot write to view '" + request->collection() + "': views are read-only");
     }
     try {
-        nlohmann::json data = nlohmann::json::parse(
-            request->data().begin(), request->data().end()
-        );
+        nlohmann::json data = smartbotic::db::parse_to_nlohmann(request->data());
 
         bool existed = store_.exists(request->collection(), request->id());
 
@@ -589,9 +582,7 @@ grpc::Status DatabaseGrpcImpl::BatchInsert(
 
     for (const auto& item : request->items()) {
         try {
-            nlohmann::json data = nlohmann::json::parse(
-                item.data().begin(), item.data().end()
-            );
+            nlohmann::json data = smartbotic::db::parse_to_nlohmann(item.data());
 
             Document doc;
             doc.id = item.id();
@@ -705,7 +696,7 @@ grpc::Status DatabaseGrpcImpl::Find(
         encryption_.decryptSensitiveFields(doc);
         pb::Document protoDoc = toProto(doc);
         if (view) {
-            nlohmann::json docJson = nlohmann::json::parse(protoDoc.data());
+            nlohmann::json docJson = smartbotic::db::parse_to_nlohmann(protoDoc.data());
             docJson = applyProjection(docJson, view->include, view->exclude);
             protoDoc.set_data(docJson.dump());
             // Preserve view name as the observed collection
@@ -1285,7 +1276,7 @@ Document DatabaseGrpcImpl::fromProto(const pb::Document& proto) {
     Document doc;
     doc.id = proto.id();
     doc.collection = proto.collection();
-    doc.data = nlohmann::json::parse(proto.data());
+    doc.data = smartbotic::db::parse_to_nlohmann(proto.data());
     doc.version = proto.version();
     doc.createdAt = proto.created_at();
     doc.updatedAt = proto.updated_at();
@@ -1341,7 +1332,7 @@ std::vector<Filter> DatabaseGrpcImpl::fromProtoFilters(
         filter.field = f.field();
         filter.op = static_cast<FilterOp>(f.op() - 1);  // Adjust for UNSPECIFIED
         if (!f.value().empty()) {
-            filter.value = nlohmann::json::parse(f.value());
+            filter.value = smartbotic::db::parse_to_nlohmann(f.value());
         }
         result.push_back(filter);
     }
@@ -1394,7 +1385,7 @@ grpc::Status DatabaseGrpcImpl::CreateView(
             f.op = static_cast<FilterOp>(pf.op() - 1);  // Adjust for UNSPECIFIED
             if (!pf.value().empty()) {
                 try {
-                    f.value = nlohmann::json::parse(pf.value());
+                    f.value = smartbotic::db::parse_to_nlohmann(pf.value());
                 } catch (...) {
                     f.value = pf.value();  // fall back to string if not JSON
                 }