From bd950c9f9d6c83bb846cdced4a9a62de83c65bb2 Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Wed, 2 Dec 2026 02:00:36 +0100 Subject: [PATCH] perf: flag the last column of each name once per result When an asynchronous result switched to JSON, every BLOB, non-finite number or malformed UTF-8 cell scanned the remaining column names to check whether a later column with the same name would overwrite it. A result with 256 rows and 2,000 BLOB columns made about 512 million name comparisons. The builder now records, once per result and next to the JSON keys, whether each column is the last one with its name. The last column still wins, including when an earlier column holds a BLOB. Co-Authored-By: Claude Opus 5.5 Co-authored-by: Codex --- packages/nitro-sqlite-core/cpp/SQLiteQueryResults.cpp | 7 ++++++- packages/nitro-sqlite-core/cpp/SQLiteQueryResults.hpp | 2 ++ .../tests/cpp/queryResults.test.cpp | 4 ++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/nitro-sqlite-core/cpp/SQLiteQueryResults.cpp b/packages/nitro-sqlite-core/cpp/SQLiteQueryResults.cpp index 8fd383f9..376ba66c 100644 --- a/packages/nitro-sqlite-core/cpp/SQLiteQueryResults.cpp +++ b/packages/nitro-sqlite-core/cpp/SQLiteQueryResults.cpp @@ -4,6 +4,7 @@ #include "sqlite/sqlite3.h" #include #include +#include namespace margelo::nitro::nitrosqlite { @@ -75,6 +76,10 @@ bool SQLiteQueryResultBuilder::startJSON() { key.push_back(':'); _keys.push_back(std::move(key)); } + _isLastColumnWithName.resize(_columnNames.size()); + std::unordered_set laterNames; + for (size_t columnIndex = _columnNames.size(); columnIndex-- > 0;) + _isLastColumnWithName[columnIndex] = laterNames.insert(_columnNames[columnIndex]).second; } auto& json = _json.emplace().json.stringRepresentation; @@ -216,7 +221,7 @@ void SQLiteQueryResultBuilder::appendBufferedValue(size_t rowIndex, size_t colum void SQLiteQueryResultBuilder::appendPatch(size_t rowIndex, size_t columnIndex, SQLiteValue value) { _json->json.stringRepresentation.append("null"); // A later column with the same alias wins, including over a BLOB or infinity. - if (std::find(_columnNames.begin() + columnIndex + 1, _columnNames.end(), _columnNames[columnIndex]) == _columnNames.end()) { + if (_isLastColumnWithName[columnIndex]) { _json->patches.push_back({rowIndex, _columnNames[columnIndex], std::move(value)}); } } diff --git a/packages/nitro-sqlite-core/cpp/SQLiteQueryResults.hpp b/packages/nitro-sqlite-core/cpp/SQLiteQueryResults.hpp index 54e91445..6aa7f2e2 100644 --- a/packages/nitro-sqlite-core/cpp/SQLiteQueryResults.hpp +++ b/packages/nitro-sqlite-core/cpp/SQLiteQueryResults.hpp @@ -96,6 +96,8 @@ class SQLiteQueryResultBuilder final { SQLiteExecutionMode _mode; // Quoted column names followed by ':', built when the first batch switches to JSON. std::vector _keys; + // Whether no later column shares this column's name, built with _keys. JSON keeps the last value of a repeated key. + std::vector _isLastColumnWithName; bool _hasMalformedColumnName = false; std::vector _nativeRows; std::vector _compactBlocks; diff --git a/packages/react-native-nitro-sqlite/tests/cpp/queryResults.test.cpp b/packages/react-native-nitro-sqlite/tests/cpp/queryResults.test.cpp index 7dd65cff..afbbdd9a 100644 --- a/packages/react-native-nitro-sqlite/tests/cpp/queryResults.test.cpp +++ b/packages/react-native-nitro-sqlite/tests/cpp/queryResults.test.cpp @@ -113,6 +113,10 @@ int main() { R"(["toString",true,true,true,"number",5]]])"; expectEqual("direct duplicate aliases", describeRows("SELECT " + duplicates), "[" + duplicateRow + "]"); expectEqual("JSON duplicate aliases", describeRows(repeatRow(duplicates, 7)), repeatJSON(duplicateRow, 7)); + const std::string blobFirst = "X'01' AS b, 2 AS b, 3 AS c, 4 AS d, 5 AS e"; + const std::string blobFirstRow = R"([true,[["b",true,true,true,"number",2],["c",true,true,true,"number",3],)" + R"(["d",true,true,true,"number",4],["e",true,true,true,"number",5]]])"; + expectEqual("JSON duplicate aliases after a BLOB", describeRows(repeatRow(blobFirst, 7)), repeatJSON(blobFirstRow, 7)); expectEqual("ordinary names", describeRows("SELECT 1 AS id, NULL AS name"), R"([[true,[["id",true,true,true,"number",1],["name",true,true,true,"object",null]]]])");