Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 35c283a

Browse filesBrowse files
anonrigaduh95
authored andcommitted
src: reduce string allocations on sqlite
PR-URL: #57227 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Edy Silva <edigleyssonsilva@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Zeyu "Alex" Yang <himself65@outlook.com>
1 parent a757f00 commit 35c283a
Copy full SHA for 35c283a

File tree

Expand file treeCollapse file tree

1 file changed

+16
-32
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+16
-32
lines changed
Open diff view settings
Collapse file

‎src/node_sqlite.cc‎

Copy file name to clipboardExpand all lines: src/node_sqlite.cc
+16-32Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -593,54 +593,38 @@ bool DatabaseSync::ShouldIgnoreSQLiteError() {
593593
std::optional<std::string> ValidateDatabasePath(Environment* env,
594594
Local<Value> path,
595595
const std::string& field_name) {
596-
auto has_null_bytes = [](const std::string& str) {
597-
return str.find('\0') != std::string::npos;
596+
constexpr auto has_null_bytes = [](std::string_view str) {
597+
return str.find('\0') != std::string_view::npos;
598598
};
599-
std::string location;
600599
if (path->IsString()) {
601-
location = Utf8Value(env->isolate(), path.As<String>()).ToString();
602-
if (!has_null_bytes(location)) {
603-
return location;
600+
Utf8Value location(env->isolate(), path.As<String>());
601+
if (!has_null_bytes(location.ToStringView())) {
602+
return location.ToString();
604603
}
605-
}
606-
607-
if (path->IsUint8Array()) {
604+
} else if (path->IsUint8Array()) {
608605
Local<Uint8Array> buffer = path.As<Uint8Array>();
609606
size_t byteOffset = buffer->ByteOffset();
610607
size_t byteLength = buffer->ByteLength();
611608
auto data =
612609
static_cast<const uint8_t*>(buffer->Buffer()->Data()) + byteOffset;
613-
if (!(std::find(data, data + byteLength, 0) != data + byteLength)) {
614-
Local<Value> out;
615-
if (String::NewFromUtf8(env->isolate(),
616-
reinterpret_cast<const char*>(data),
617-
NewStringType::kNormal,
618-
static_cast<int>(byteLength))
619-
.ToLocal(&out)) {
620-
return Utf8Value(env->isolate(), out.As<String>()).ToString();
621-
}
610+
if (std::find(data, data + byteLength, 0) == data + byteLength) {
611+
return std::string(reinterpret_cast<const char*>(data), byteLength);
622612
}
623-
}
624-
625-
// When is URL
626-
if (path->IsObject()) {
627-
Local<Object> url = path.As<Object>();
613+
} else if (path->IsObject()) { // When is URL
614+
auto url = path.As<Object>();
628615
Local<Value> href;
629-
Local<Value> protocol;
630616
if (url->Get(env->context(), env->href_string()).ToLocal(&href) &&
631-
href->IsString() &&
632-
url->Get(env->context(), env->protocol_string()).ToLocal(&protocol) &&
633-
protocol->IsString()) {
634-
location = Utf8Value(env->isolate(), href.As<String>()).ToString();
617+
href->IsString()) {
618+
Utf8Value location_value(env->isolate(), href.As<String>());
619+
auto location = location_value.ToStringView();
635620
if (!has_null_bytes(location)) {
636-
auto file_url = ada::parse(location);
637-
CHECK(file_url);
638-
if (file_url->type != ada::scheme::FILE) {
621+
CHECK(ada::can_parse(location));
622+
if (!location.starts_with("file:")) {
639623
THROW_ERR_INVALID_URL_SCHEME(env->isolate());
640624
return std::nullopt;
641625
}
642626

643-
return location;
627+
return location_value.ToString();
644628
}
645629
}
646630
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.