Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 63 additions & 9 deletions src/node_sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "node_errors.h"
#include "node_mem-inl.h"
#include "node_url.h"
#include "simdutf.h"
#include "sqlite3.h"
#include "threadpoolwork-inl.h"
#include "util-inl.h"
Expand Down Expand Up @@ -55,6 +56,20 @@ using v8::TryCatch;
using v8::Uint8Array;
using v8::Value;

inline MaybeLocal<String> Utf8StringMaybeOneByte(Isolate* isolate,
std::string_view input) {
int len = static_cast<int>(input.size());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int len = static_cast<int>(input.size());
const int len = static_cast<int>(input.size());

if (simdutf::validate_ascii(input.data(), input.size())) {
return String::NewFromOneByte(
isolate,
reinterpret_cast<const uint8_t*>(input.data()),
NewStringType::kNormal,
len);
}
return String::NewFromUtf8(
isolate, input.data(), NewStringType::kNormal, len);
}

#define CHECK_ERROR_OR_THROW(isolate, db, expr, expected, ret) \
do { \
int r_ = (expr); \
Expand Down Expand Up @@ -97,7 +112,10 @@ using v8::Value;
case SQLITE_TEXT: { \
const char* v = \
reinterpret_cast<const char*>(sqlite3_##from##_text(__VA_ARGS__)); \
(result) = String::NewFromUtf8((isolate), v).As<Value>(); \
int v_len = sqlite3_##from##_bytes(__VA_ARGS__); \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int v_len = sqlite3_##from##_bytes(__VA_ARGS__); \
const int v_len = sqlite3_##from##_bytes(__VA_ARGS__); \

(result) = \
Utf8StringMaybeOneByte((isolate), std::string_view(v, v_len)) \
.As<Value>(); \
break; \
} \
case SQLITE_NULL: { \
Expand Down Expand Up @@ -2147,6 +2165,7 @@ StatementSync::~StatementSync() {
void StatementSync::Finalize() {
sqlite3_finalize(statement_);
statement_ = nullptr;
cached_column_names_.clear();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider putting this in a method.

}

inline bool StatementSync::IsFinalized() {
Expand Down Expand Up @@ -2325,7 +2344,43 @@ MaybeLocal<Name> StatementSync::ColumnNameToName(const int column) {
return MaybeLocal<Name>();
}

return String::NewFromUtf8(env()->isolate(), col_name).As<Name>();
return String::NewFromUtf8(
env()->isolate(), col_name, NewStringType::kInternalized)
.As<Name>();
}

// Returns cached internalized column name strings for this statement,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is incorrect, it returns a boolean.

// invalidating the cache when SQLite re-prepares the statement (e.g. after
// schema changes like ALTER TABLE) detected via SQLITE_STMTSTATUS_REPREPARE.
bool StatementSync::GetCachedColumnNames(LocalVector<Name>* keys) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment to this function?

Isolate* isolate = env()->isolate();

int reprepare_count =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int reprepare_count =
const int reprepare_count =

sqlite3_stmt_status(statement_, SQLITE_STMTSTATUS_REPREPARE, 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit

Suggested change
sqlite3_stmt_status(statement_, SQLITE_STMTSTATUS_REPREPARE, 0);
sqlite3_stmt_status(statement_, SQLITE_STMTSTATUS_REPREPARE, false);

if (reprepare_count != cached_column_names_reprepare_count_) {
cached_column_names_.clear();
int num_cols = sqlite3_column_count(statement_);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int num_cols = sqlite3_column_count(statement_);
const int num_cols = sqlite3_column_count(statement_);

if (num_cols == 0) {
cached_column_names_reprepare_count_ = reprepare_count;
return true;
}
cached_column_names_.reserve(num_cols);
for (int i = 0; i < num_cols; ++i) {
Local<Name> key;
if (!ColumnNameToName(i).ToLocal(&key)) {
cached_column_names_.clear();
return false;
}
cached_column_names_.emplace_back(Global<Name>(isolate, key));
}
cached_column_names_reprepare_count_ = reprepare_count;
}

keys->reserve(cached_column_names_.size());
for (const auto& name : cached_column_names_) {
keys->emplace_back(name.Get(isolate));
}
return true;
}

MaybeLocal<Value> StatementExecutionHelper::ColumnToValue(Environment* env,
Expand All @@ -2347,7 +2402,9 @@ MaybeLocal<Name> StatementExecutionHelper::ColumnNameToName(Environment* env,
return MaybeLocal<Name>();
}

return String::NewFromUtf8(env->isolate(), col_name).As<Name>();
return String::NewFromUtf8(
env->isolate(), col_name, NewStringType::kInternalized)
.As<Name>();
}

void StatementSync::MemoryInfo(MemoryTracker* tracker) const {}
Expand Down Expand Up @@ -3251,12 +3308,9 @@ void StatementSyncIterator::Next(const FunctionCallbackInfo<Value>& args) {
if (iter->stmt_->return_arrays_) {
row_value = Array::New(isolate, row_values.data(), row_values.size());
} else {
row_keys.reserve(num_cols);
for (int i = 0; i < num_cols; ++i) {
Local<Name> key;
if (!iter->stmt_->ColumnNameToName(i).ToLocal(&key)) return;
row_keys.emplace_back(key);
}
// Use cached internalized column names to avoid repeated V8 string
// creation and enable hidden class sharing across row objects.
if (!iter->stmt_->GetCachedColumnNames(&row_keys)) return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment to here?


DCHECK_EQ(row_keys.size(), row_values.size());
row_value = Object::New(
Expand Down
4 changes: 4 additions & 0 deletions src/node_sqlite.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <list>
#include <map>
#include <unordered_set>
#include <vector>

namespace node {
namespace sqlite {
Expand Down Expand Up @@ -228,6 +229,7 @@ class StatementSync : public BaseObject {
static void SetReturnArrays(const v8::FunctionCallbackInfo<v8::Value>& args);
v8::MaybeLocal<v8::Value> ColumnToValue(const int column);
v8::MaybeLocal<v8::Name> ColumnNameToName(const int column);
bool GetCachedColumnNames(v8::LocalVector<v8::Name>* keys);
void Finalize();
bool IsFinalized();

Expand All @@ -243,6 +245,8 @@ class StatementSync : public BaseObject {
bool allow_bare_named_params_;
bool allow_unknown_named_params_;
std::optional<std::map<std::string, std::string>> bare_named_params_;
std::vector<v8::Global<v8::Name>> cached_column_names_;
int cached_column_names_reprepare_count_ = -1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using std::optional.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion @louwers but I benchmarked on the iterate path (30 runs) saw a consistent -1% to -2% regression after making these changes:

sqlite/sqlite-prepare-select-all.js statement='SELECT text_column FROM foo LIMIT 1' method='iterate' tableSeedSize=100000 n=100000 *** -2.06 %
sqlite/sqlite-prepare-select-all.js statement='SELECT text_column FROM foo LIMIT 100' method='iterate' tableSeedSize=100000 n=100000 *** -1.42 %

The reason might be std::optional comparison generates more instructions per row than just int

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. Not worth it then.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah it's confirmed, but very interesting

bool BindParams(const v8::FunctionCallbackInfo<v8::Value>& args);
bool BindValue(const v8::Local<v8::Value>& value, const int index);

Expand Down
Loading