Skip to content

Commit 1ce8928

Browse files
indutnyaduh95
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: #55373 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
1 parent c3f2216 commit 1ce8928

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/node_sqlite.cc

+17-8
Original file line numberDiff line numberDiff line change
@@ -561,23 +561,32 @@ void StatementSync::All(const FunctionCallbackInfo<Value>& args) {
561561
auto reset = OnScopeLeave([&]() { sqlite3_reset(stmt->statement_); });
562562
int num_cols = sqlite3_column_count(stmt->statement_);
563563
LocalVector<Value> rows(isolate);
564+
LocalVector<Name> row_keys(isolate);
564565
while ((r = sqlite3_step(stmt->statement_)) == SQLITE_ROW) {
565-
LocalVector<Name> row_keys(isolate);
566-
row_keys.reserve(num_cols);
566+
if (row_keys.size() == 0) {
567+
row_keys.reserve(num_cols);
568+
569+
for (int i = 0; i < num_cols; ++i) {
570+
Local<Name> key;
571+
if (!stmt->ColumnNameToName(i).ToLocal(&key)) return;
572+
row_keys.emplace_back(key);
573+
}
574+
}
575+
567576
LocalVector<Value> row_values(isolate);
568577
row_values.reserve(num_cols);
569578

570-
for (int i = 0; i < num_cols; ++i) {
571-
Local<Name> key;
572-
if (!stmt->ColumnNameToName(i).ToLocal(&key)) return;
579+
for (size_t i = 0; i < row_keys.size(); ++i) {
573580
Local<Value> val;
574581
if (!stmt->ColumnToValue(i).ToLocal(&val)) return;
575-
row_keys.emplace_back(key);
576582
row_values.emplace_back(val);
577583
}
578584

579-
Local<Object> row = Object::New(
580-
isolate, Null(isolate), row_keys.data(), row_values.data(), num_cols);
585+
Local<Object> row = Object::New(isolate,
586+
Null(isolate),
587+
row_keys.data(),
588+
row_values.data(),
589+
row_keys.size());
581590
rows.emplace_back(row);
582591
}
583592

0 commit comments

Comments
 (0)