-
-
Notifications
You must be signed in to change notification settings - Fork 35.8k
buffer: add fast api for isUtf8 and isAscii #64169
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
gurgunday
wants to merge
1
commit into
nodejs:main
Choose a base branch
from
gurgunday:feat/fast-api-isUtf8
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.
+98
−9
Open
Changes from all commits
Commits
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 |
|---|---|---|
|
|
@@ -1360,37 +1360,89 @@ void FastSwap64(Local<Value> receiver, | |
|
|
||
| static CFunction fast_swap64(CFunction::Make(FastSwap64)); | ||
|
|
||
| static bool ValidateUtf8(Local<Value> value, bool* was_detached) { | ||
| ArrayBufferViewContents<char> abv(value); | ||
| *was_detached = abv.WasDetached(); | ||
| return !*was_detached && simdutf::validate_utf8(abv.data(), abv.length()); | ||
| } | ||
|
|
||
| static void IsUtf8(const FunctionCallbackInfo<Value>& args) { | ||
| Environment* env = Environment::GetCurrent(args); | ||
| CHECK_EQ(args.Length(), 1); | ||
| CHECK(args[0]->IsTypedArray() || args[0]->IsArrayBuffer() || | ||
| args[0]->IsSharedArrayBuffer()); | ||
| ArrayBufferViewContents<char> abv(args[0]); | ||
|
|
||
| if (abv.WasDetached()) { | ||
| bool was_detached; | ||
| const bool result = ValidateUtf8(args[0], &was_detached); | ||
| if (was_detached) { | ||
| return node::THROW_ERR_INVALID_STATE( | ||
| env, "Cannot validate on a detached buffer"); | ||
| } | ||
|
|
||
| args.GetReturnValue().Set(simdutf::validate_utf8(abv.data(), abv.length())); | ||
| args.GetReturnValue().Set(result); | ||
| } | ||
|
|
||
| static bool FastIsUtf8(Local<Value> receiver, | ||
| Local<Value> value, | ||
| // NOLINTNEXTLINE(runtime/references) | ||
| FastApiCallbackOptions& options) { | ||
| TRACK_V8_FAST_API_CALL("buffer.isUtf8"); | ||
| HandleScope scope(options.isolate); | ||
|
|
||
| bool was_detached; | ||
| const bool result = ValidateUtf8(value, &was_detached); | ||
| if (was_detached) { | ||
| node::THROW_ERR_INVALID_STATE(options.isolate, | ||
| "Cannot validate on a detached buffer"); | ||
| return false; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| static CFunction fast_is_utf8(CFunction::Make(FastIsUtf8)); | ||
|
|
||
| static bool ValidateAscii(Local<Value> value, bool* was_detached) { | ||
| ArrayBufferViewContents<char> abv(value); | ||
| *was_detached = abv.WasDetached(); | ||
| return !*was_detached && | ||
| !simdutf::validate_ascii_with_errors(abv.data(), abv.length()).error; | ||
|
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. ditto |
||
| } | ||
|
|
||
| static void IsAscii(const FunctionCallbackInfo<Value>& args) { | ||
| Environment* env = Environment::GetCurrent(args); | ||
| CHECK_EQ(args.Length(), 1); | ||
| CHECK(args[0]->IsTypedArray() || args[0]->IsArrayBuffer() || | ||
| args[0]->IsSharedArrayBuffer()); | ||
| ArrayBufferViewContents<char> abv(args[0]); | ||
|
|
||
| if (abv.WasDetached()) { | ||
| bool was_detached; | ||
| const bool result = ValidateAscii(args[0], &was_detached); | ||
| if (was_detached) { | ||
| return node::THROW_ERR_INVALID_STATE( | ||
| env, "Cannot validate on a detached buffer"); | ||
| } | ||
|
|
||
| args.GetReturnValue().Set( | ||
| !simdutf::validate_ascii_with_errors(abv.data(), abv.length()).error); | ||
| args.GetReturnValue().Set(result); | ||
| } | ||
|
|
||
| static bool FastIsAscii(Local<Value> receiver, | ||
| Local<Value> value, | ||
| // NOLINTNEXTLINE(runtime/references) | ||
| FastApiCallbackOptions& options) { | ||
| TRACK_V8_FAST_API_CALL("buffer.isAscii"); | ||
| HandleScope scope(options.isolate); | ||
|
|
||
| bool was_detached; | ||
| const bool result = ValidateAscii(value, &was_detached); | ||
| if (was_detached) { | ||
| node::THROW_ERR_INVALID_STATE(options.isolate, | ||
| "Cannot validate on a detached buffer"); | ||
| return false; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| static CFunction fast_is_ascii(CFunction::Make(FastIsAscii)); | ||
|
|
||
| void SetBufferPrototype(const FunctionCallbackInfo<Value>& args) { | ||
| Realm* realm = Realm::GetCurrent(args); | ||
|
|
||
|
|
@@ -1762,8 +1814,9 @@ void Initialize(Local<Object> target, | |
| SetFastMethod(context, target, "swap32", Swap32, &fast_swap32); | ||
| SetFastMethod(context, target, "swap64", Swap64, &fast_swap64); | ||
|
|
||
| SetMethodNoSideEffect(context, target, "isUtf8", IsUtf8); | ||
| SetMethodNoSideEffect(context, target, "isAscii", IsAscii); | ||
| SetFastMethodNoSideEffect(context, target, "isUtf8", IsUtf8, &fast_is_utf8); | ||
| SetFastMethodNoSideEffect( | ||
| context, target, "isAscii", IsAscii, &fast_is_ascii); | ||
|
|
||
| target | ||
| ->Set(context, | ||
|
|
@@ -1836,7 +1889,9 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) { | |
| registry->Register(fast_swap64); | ||
|
|
||
| registry->Register(IsUtf8); | ||
| registry->Register(fast_is_utf8); | ||
| registry->Register(IsAscii); | ||
| registry->Register(fast_is_ascii); | ||
|
|
||
| registry->Register(StringSlice<ASCII>); | ||
| registry->Register(StringSlice<BASE64>); | ||
|
|
||
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 |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // Flags: --expose-internals --no-warnings --allow-natives-syntax | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| const assert = require('assert'); | ||
| const { Buffer, isAscii, isUtf8 } = require('buffer'); | ||
|
|
||
| const ascii = Buffer.from('hello'); | ||
| const utf8 = Buffer.from('hello \xc4\x9f'); | ||
|
|
||
| function testFastIsAscii() { | ||
| assert.strictEqual(isAscii(ascii), true); | ||
| } | ||
|
|
||
| function testFastIsUtf8() { | ||
| assert.strictEqual(isUtf8(utf8), true); | ||
| } | ||
|
|
||
| eval('%PrepareFunctionForOptimization(isAscii)'); | ||
| testFastIsAscii(); | ||
| eval('%OptimizeFunctionOnNextCall(isAscii)'); | ||
| testFastIsAscii(); | ||
|
|
||
| eval('%PrepareFunctionForOptimization(isUtf8)'); | ||
| testFastIsUtf8(); | ||
| eval('%OptimizeFunctionOnNextCall(isUtf8)'); | ||
| testFastIsUtf8(); | ||
|
|
||
| if (common.isDebug) { | ||
| const { internalBinding } = require('internal/test/binding'); | ||
| const { getV8FastApiCallCount } = internalBinding('debug'); | ||
| assert.strictEqual(getV8FastApiCallCount('buffer.isAscii'), 1); | ||
| assert.strictEqual(getV8FastApiCallCount('buffer.isUtf8'), 1); | ||
| } |
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.
Would definitely recommend returning an
std::pair<bool, bool>or just outright a struct here instead of a return value and a separate out-parameter boolean