-
-
Notifications
You must be signed in to change notification settings - Fork 34.9k
sqlite: optimize column name creation and text value encoding #61954
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
thisalihassan
wants to merge
2
commits into
nodejs:main
Choose a base branch
from
thisalihassan:sqlite-one-byte-encoding
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+67
−9
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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" | ||
|
|
@@ -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()); | ||
| 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); \ | ||
|
|
@@ -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__); \ | ||
| (result) = \ | ||
| Utf8StringMaybeOneByte((isolate), std::string_view(v, v_len)) \ | ||
| .As<Value>(); \ | ||
| break; \ | ||
| } \ | ||
| case SQLITE_NULL: { \ | ||
|
|
@@ -2147,6 +2165,7 @@ StatementSync::~StatementSync() { | |
| void StatementSync::Finalize() { | ||
| sqlite3_finalize(statement_); | ||
| statement_ = nullptr; | ||
| cached_column_names_.clear(); | ||
| } | ||
|
|
||
| inline bool StatementSync::IsFinalized() { | ||
|
|
@@ -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, | ||
| // 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) { | ||
| Isolate* isolate = env()->isolate(); | ||
|
|
||
| int reprepare_count = | ||
| sqlite3_stmt_status(statement_, SQLITE_STMTSTATUS_REPREPARE, 0); | ||
| if (reprepare_count != cached_column_names_reprepare_count_) { | ||
| cached_column_names_.clear(); | ||
| 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, | ||
|
|
@@ -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 {} | ||
|
|
@@ -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; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?