Skip to content

Commit 5efd4be

Browse files
indutnylouwers
authored andcommitted
sqlite: cache column names in stmt.all()
While the statement is running, it is impossible to modify the column names and thus it is beneficial to create the host-language (JS) keys once per all rows and reuse them for all results. With this change the performance of `.all()` improves by around 25% depending on the number of columns and rows in the result. PR-URL: nodejs#55373 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]>
1 parent 88efd47 commit 5efd4be

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/node_sqlite.cc

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -743,23 +743,32 @@ void StatementSync::All(const FunctionCallbackInfo<Value>& args) {
743743
auto reset = OnScopeLeave([&]() { sqlite3_reset(stmt->statement_); });
744744
int num_cols = sqlite3_column_count(stmt->statement_);
745745
LocalVector<Value> rows(isolate);
746+
LocalVector<Name> row_keys(isolate);
746747
while ((r = sqlite3_step(stmt->statement_)) == SQLITE_ROW) {
747-
LocalVector<Name> row_keys(isolate);
748-
row_keys.reserve(num_cols);
748+
if (row_keys.size() == 0) {
749+
row_keys.reserve(num_cols);
750+
751+
for (int i = 0; i < num_cols; ++i) {
752+
Local<Name> key;
753+
if (!stmt->ColumnNameToName(i).ToLocal(&key)) return;
754+
row_keys.emplace_back(key);
755+
}
756+
}
757+
749758
LocalVector<Value> row_values(isolate);
750759
row_values.reserve(num_cols);
751760

752-
for (int i = 0; i < num_cols; ++i) {
753-
Local<Name> key;
754-
if (!stmt->ColumnNameToName(i).ToLocal(&key)) return;
761+
for (size_t i = 0; i < row_keys.size(); ++i) {
755762
Local<Value> val;
756763
if (!stmt->ColumnToValue(i).ToLocal(&val)) return;
757-
row_keys.emplace_back(key);
758764
row_values.emplace_back(val);
759765
}
760766

761-
Local<Object> row = Object::New(
762-
isolate, Null(isolate), row_keys.data(), row_values.data(), num_cols);
767+
Local<Object> row = Object::New(isolate,
768+
Null(isolate),
769+
row_keys.data(),
770+
row_values.data(),
771+
row_keys.size());
763772
rows.emplace_back(row);
764773
}
765774

0 commit comments

Comments
 (0)