Skip to content
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

src: add a fast call for URL.revokeObjectURL #47794

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
src: add a fast call for URL.revokeObjectURL
Refs: #47728
  • Loading branch information
debadree25 committed Apr 30, 2023
commit 869ff2a63c743e42604056d34d7fb9820dedba27
20 changes: 20 additions & 0 deletions benchmark/url/url-create-revoke-objecturl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';
const common = require('../common.js');
const assert = require('node:assert');
const { Blob } = require('buffer');

const bench = common.createBenchmark(main, {
n: [1e6],
});

function main({ n }) {
const blob = new Blob(['hello']);
let id = '';
bench.start();
for (let i = 0; i < n; i += 1) {
id = URL.createObjectURL(blob);
URL.revokeObjectURL(id);
}
assert.ok(id);
bench.end(n);
}
79 changes: 51 additions & 28 deletions src/node_blob.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "node_external_reference.h"
#include "node_file.h"
#include "util.h"
#include "v8-fast-api-calls.h"
#include "v8.h"

#include <algorithm>
Expand All @@ -19,7 +20,9 @@ using v8::Array;
using v8::ArrayBuffer;
using v8::ArrayBufferView;
using v8::BackingStore;
using v8::CFunction;
using v8::Context;
using v8::FastOneByteString;
using v8::Function;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
Expand Down Expand Up @@ -107,25 +110,6 @@ void BlobFromFilePath(const FunctionCallbackInfo<Value>& args) {
}
} // namespace

void Blob::Initialize(
Local<Object> target,
Local<Value> unused,
Local<Context> context,
void* priv) {
Realm* realm = Realm::GetCurrent(context);

BlobBindingData* const binding_data =
realm->AddBindingData<BlobBindingData>(context, target);
if (binding_data == nullptr) return;

SetMethod(context, target, "createBlob", New);
SetMethod(context, target, "storeDataObject", StoreDataObject);
SetMethod(context, target, "getDataObject", GetDataObject);
SetMethod(context, target, "revokeObjectURL", RevokeObjectURL);
SetMethod(context, target, "concat", Concat);
SetMethod(context, target, "createBlobFromFilePath", BlobFromFilePath);
}

Local<FunctionTemplate> Blob::GetConstructorTemplate(Environment* env) {
Local<FunctionTemplate> tmpl = env->blob_constructor_template();
if (tmpl.IsEmpty()) {
Expand Down Expand Up @@ -416,14 +400,9 @@ void Blob::StoreDataObject(const v8::FunctionCallbackInfo<v8::Value>& args) {
std::string(*type, type.length())));
}

// TODO(@anonrig): Add V8 Fast API to the following function
void Blob::RevokeObjectURL(const FunctionCallbackInfo<Value>& args) {
CHECK_GE(args.Length(), 1);
CHECK(args[0]->IsString());
BlobBindingData* binding_data = Realm::GetBindingData<BlobBindingData>(args);
Environment* env = Environment::GetCurrent(args);
Utf8Value input(env->isolate(), args[0].As<String>());
auto out = ada::parse<ada::url_aggregator>(input.ToStringView());
void RevokeObjectURLImpl(std::string_view input,
BlobBindingData* binding_data) {
auto out = ada::parse<ada::url_aggregator>(input);

if (!out) {
return;
Expand All @@ -441,6 +420,26 @@ void Blob::RevokeObjectURL(const FunctionCallbackInfo<Value>& args) {
}
}

void Blob::RevokeObjectURL(const FunctionCallbackInfo<Value>& args) {
CHECK_GE(args.Length(), 1);
CHECK(args[0]->IsString());
BlobBindingData* binding_data = Realm::GetBindingData<BlobBindingData>(args);
Environment* env = Environment::GetCurrent(args);
Utf8Value input(env->isolate(), args[0].As<String>());
RevokeObjectURLImpl(input.ToStringView(), binding_data);
}

void Blob::FastRevokeObjectURL(Local<Value> receiver,
const FastOneByteString& input) {
printf("FastRevokeObjectURL\n");
BlobBindingData* binding_data = FromJSObject<BlobBindingData>(receiver);
std::string_view input_view(input.data, input.length);
RevokeObjectURLImpl(input_view, binding_data);
}

CFunction Blob::fast_revoke_object_url(
CFunction::Make(Blob::FastRevokeObjectURL));

void Blob::GetDataObject(const v8::FunctionCallbackInfo<v8::Value>& args) {
BlobBindingData* binding_data = Realm::GetBindingData<BlobBindingData>(args);

Expand Down Expand Up @@ -548,16 +547,40 @@ InternalFieldInfoBase* BlobBindingData::Serialize(int index) {
return info;
}

void Blob::Initialize(Local<Object> target,
Local<Value> unused,
Local<Context> context,
void* priv) {
Realm* realm = Realm::GetCurrent(context);

BlobBindingData* const binding_data =
realm->AddBindingData<BlobBindingData>(context, target);
if (binding_data == nullptr) return;

SetMethod(context, target, "createBlob", New);
SetMethod(context, target, "storeDataObject", StoreDataObject);
SetMethod(context, target, "getDataObject", GetDataObject);
SetFastMethod(context,
target,
"revokeObjectURL",
RevokeObjectURL,
&fast_revoke_object_url);
SetMethod(context, target, "concat", Concat);
SetMethod(context, target, "createBlobFromFilePath", BlobFromFilePath);
}

void Blob::RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(Blob::New);
registry->Register(Blob::GetReader);
registry->Register(Blob::ToSlice);
registry->Register(Blob::StoreDataObject);
registry->Register(Blob::GetDataObject);
registry->Register(Blob::RevokeObjectURL);
registry->Register(Blob::Reader::Pull);
registry->Register(Concat);
registry->Register(BlobFromFilePath);
registry->Register(Blob::RevokeObjectURL);
registry->Register(Blob::FastRevokeObjectURL);
registry->Register(fast_revoke_object_url.GetTypeInfo());
}

} // namespace node
Expand Down
4 changes: 4 additions & 0 deletions src/node_blob.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "node_internals.h"
#include "node_snapshotable.h"
#include "node_worker.h"
#include "v8-fast-api-calls.h"
#include "v8.h"

#include <string>
Expand All @@ -38,6 +39,8 @@ class Blob : public BaseObject {
static void StoreDataObject(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetDataObject(const v8::FunctionCallbackInfo<v8::Value>& args);
static void RevokeObjectURL(const v8::FunctionCallbackInfo<v8::Value>& args);
static void FastRevokeObjectURL(v8::Local<v8::Value> receiver,
const v8::FastOneByteString& input);

static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
Environment* env);
Expand Down Expand Up @@ -107,6 +110,7 @@ class Blob : public BaseObject {

private:
std::shared_ptr<DataQueue> data_queue_;
static v8::CFunction fast_revoke_object_url;
};

class BlobBindingData : public SnapshotableObject {
Expand Down
4 changes: 4 additions & 0 deletions src/node_external_reference.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ using CFunctionCallbackWithBool = void (*)(v8::Local<v8::Object> receiver,
using CFunctionCallbackWithStrings =
bool (*)(v8::Local<v8::Value>, const v8::FastOneByteString& input);

using CFunctionCallbackWithStringsReturnVoid =
void (*)(v8::Local<v8::Value>, const v8::FastOneByteString& input);
Copy link
Member

Choose a reason for hiding this comment

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

@joyeecheung You used Local<v8::Object> for the receiver, whereas, in CanParse @KhafraDev used Local<v8::Value> for the receiver. Which one is the correct usage for the Fast API, or is both of them correct?

Copy link
Member

Choose a reason for hiding this comment

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

The strings returned by createObjectURL() are cons strings. They won't hit the fast API.

Copy link
Member

Choose a reason for hiding this comment

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

Is this documented? How can we update the benchmark to flatten the string?

(For people who didn't know what cons strings mean: cons strings are pairs of strings, result of concatenation.)

Copy link
Member

Choose a reason for hiding this comment

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

Adding hello-1-2-3 as an input triggers fast api, but even crypto.randomUUID() as a parameter does not trigger fast API...

function main({ n }) {
  bench.start();
  for (let i = 0; i < n; i += 1) {
    URL.revokeObjectURL('hello-1-2-3');
  }
  bench.end(n);
}

Copy link
Member

Choose a reason for hiding this comment

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

FastOneByteString means the string type is a sequential one byte string. The string types are internal to V8 and subject to changes. I don't think it's meaningful to flatten the string in the benchmark if users in the wild are going to pass urls returned by createObjectURL() to it...

Copy link
Member

Choose a reason for hiding this comment

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

The JS-land version buffers the UUID so to match the performance the C++ version needs to buffer the UUID as well. Also snprintf() can be slow if it's heavily used.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think doing all this would probably make the C++ side way more complicated than the original TODO intended, is there no faster way of string allocation on C++ side?

Copy link
Member

Choose a reason for hiding this comment

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

You could also just merge the concatenation in createObjectURL() from JS land into Blob::StoreDataObject and concatenate the strings from C++ instead.

Copy link
Member

Choose a reason for hiding this comment

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

The string should be created with NewFromOneByte(), not NewFromUtf8() as in 268fc13 because it's guaranteed to be one-byte and does not need transcoding.

Copy link
Member Author

Choose a reason for hiding this comment

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

okay so i tried this

diff --git a/src/node_blob.cc b/src/node_blob.cc
index 7db8684904..45242e7c44 100644
--- a/src/node_blob.cc
+++ b/src/node_blob.cc
@@ -391,13 +391,19 @@ void Blob::StoreDataObject(const v8::FunctionCallbackInfo<v8::Value>& args) {
 
   size_t length = args[2].As<Uint32>()->Value();
   Utf8Value type(env->isolate(), args[3]);
+  std::string key_str(*key, key.length());
 
   binding_data->store_data_object(
-      std::string(*key, key.length()),
-      BlobBindingData::StoredDataObject(
-        BaseObjectPtr<Blob>(blob),
-        length,
-        std::string(*type, type.length())));
+      key_str,
+      BlobBindingData::StoredDataObject(BaseObjectPtr<Blob>(blob),
+                                        length,
+                                        std::string(*type, type.length())));
+  std::string final_url = "blob:nodedata:" + key_str;
+  args.GetReturnValue().Set(String::NewFromOneByte(env->isolate(),
+                                                reinterpret_cast<const uint8_t*>(final_url.data()),
+                                                v8::NewStringType::kNormal,
+                                                final_url.length())
+                                .ToLocalChecked());
 }

fast paths are being hit and perf is maybe little bit better than 268fc13 but on the benchmark in the commit, its still slower than the normal js version unfortunately 😓


// This class manages the external references from the V8 heap
// to the C++ addresses in Node.js.
class ExternalReferenceRegistry {
Expand All @@ -35,6 +38,7 @@ class ExternalReferenceRegistry {
V(CFunctionCallbackWithInt64) \
V(CFunctionCallbackWithBool) \
V(CFunctionCallbackWithStrings) \
V(CFunctionCallbackWithStringsReturnVoid) \
V(const v8::CFunctionInfo*) \
V(v8::FunctionCallback) \
V(v8::AccessorGetterCallback) \
Expand Down