From 12e4eef1dd9ca2137cad06ed13b4c69927ab4024 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Thu, 27 Feb 2025 15:55:51 -0500 Subject: [PATCH 1/2] src: reduce string allocations on sqlite --- src/node_sqlite.cc | 48 ++++++++++++++++------------------------------ 1 file changed, 16 insertions(+), 32 deletions(-) diff --git a/src/node_sqlite.cc b/src/node_sqlite.cc index 54c7d5d6b2f48d..76c272dea38621 100644 --- a/src/node_sqlite.cc +++ b/src/node_sqlite.cc @@ -593,54 +593,38 @@ bool DatabaseSync::ShouldIgnoreSQLiteError() { std::optional ValidateDatabasePath(Environment* env, Local path, const std::string& field_name) { - auto has_null_bytes = [](const std::string& str) { - return str.find('\0') != std::string::npos; + constexpr auto has_null_bytes = [](std::string_view str) { + return str.find('\0') != std::string_view::npos; }; - std::string location; if (path->IsString()) { - location = Utf8Value(env->isolate(), path.As()).ToString(); - if (!has_null_bytes(location)) { - return location; + auto location = Utf8Value(env->isolate(), path.As()); + if (!has_null_bytes(location.ToStringView())) { + return location.ToString(); } - } - - if (path->IsUint8Array()) { + } else if (path->IsUint8Array()) { Local buffer = path.As(); size_t byteOffset = buffer->ByteOffset(); size_t byteLength = buffer->ByteLength(); auto data = static_cast(buffer->Buffer()->Data()) + byteOffset; - if (!(std::find(data, data + byteLength, 0) != data + byteLength)) { - Local out; - if (String::NewFromUtf8(env->isolate(), - reinterpret_cast(data), - NewStringType::kNormal, - static_cast(byteLength)) - .ToLocal(&out)) { - return Utf8Value(env->isolate(), out.As()).ToString(); - } + if (std::find(data, data + byteLength, 0) == data + byteLength) { + return std::string(reinterpret_cast(data), byteLength); } - } - - // When is URL - if (path->IsObject()) { - Local url = path.As(); + } else if (path->IsObject()) { // When is URL + auto url = path.As(); Local href; - Local protocol; if (url->Get(env->context(), env->href_string()).ToLocal(&href) && - href->IsString() && - url->Get(env->context(), env->protocol_string()).ToLocal(&protocol) && - protocol->IsString()) { - location = Utf8Value(env->isolate(), href.As()).ToString(); + href->IsString()) { + const auto location_value = Utf8Value(env->isolate(), href.As()); + auto location = location_value.ToStringView(); if (!has_null_bytes(location)) { - auto file_url = ada::parse(location); - CHECK(file_url); - if (file_url->type != ada::scheme::FILE) { + CHECK(ada::can_parse(location)); + if (!location.starts_with("file:")) { THROW_ERR_INVALID_URL_SCHEME(env->isolate()); return std::nullopt; } - return location; + return location_value.ToString(); } } } From 8a206df14823931cd6502a96858770e4b11a8f5e Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Thu, 27 Feb 2025 16:50:10 -0500 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: James M Snell --- src/node_sqlite.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/node_sqlite.cc b/src/node_sqlite.cc index 76c272dea38621..900188fa5fddf5 100644 --- a/src/node_sqlite.cc +++ b/src/node_sqlite.cc @@ -597,7 +597,7 @@ std::optional ValidateDatabasePath(Environment* env, return str.find('\0') != std::string_view::npos; }; if (path->IsString()) { - auto location = Utf8Value(env->isolate(), path.As()); + Utf8Value location(env->isolate(), path.As()); if (!has_null_bytes(location.ToStringView())) { return location.ToString(); } @@ -615,7 +615,7 @@ std::optional ValidateDatabasePath(Environment* env, Local href; if (url->Get(env->context(), env->href_string()).ToLocal(&href) && href->IsString()) { - const auto location_value = Utf8Value(env->isolate(), href.As()); + Utf8Value location_value(env->isolate(), href.As()); auto location = location_value.ToStringView(); if (!has_null_bytes(location)) { CHECK(ada::can_parse(location));