-
-
Notifications
You must be signed in to change notification settings - Fork 32k
buffer: add fast api for isAscii & isUtf8 #58058
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
anonrig
wants to merge
4
commits into
nodejs:main
Choose a base branch
from
anonrig:yagiz/add-fast-api-isascii
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.
+113
−2
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
80ec355
buffer: add fast api for isAscii & isUtf8
anonrig 62dea03
fixup! buffer: add fast api for isAscii & isUtf8
anonrig 7e84063
fixup! buffer: add fast api for isAscii & isUtf7
anonrig 6b11f13
fixup! fixup! buffer: add fast api for isAscii & isUtf7
anonrig 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
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,71 @@ | ||
// Flags: --expose-internals --no-warnings --allow-natives-syntax | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const { internalBinding } = require('internal/test/binding'); | ||
|
||
// Get direct access to the buffer validation methods | ||
const buffer = require('buffer'); | ||
|
||
// Create test buffers | ||
const utf8Buffer = Buffer.from('Hello, 世界!'); // Valid UTF-8 with actual Unicode characters | ||
const asciiBuffer = Buffer.from('Hello, World!'); // Valid ASCII | ||
const nonUtf8Buffer = Buffer.from([0xFF, 0xFF, 0xFF]); // Invalid UTF-8 | ||
const nonAsciiBuffer = Buffer.from([0x80, 0x90, 0xA0]); // Invalid ASCII | ||
|
||
// Test basic functionality for isUtf8 | ||
assert.strictEqual(buffer.isUtf8(utf8Buffer), true); | ||
assert.strictEqual(buffer.isUtf8(nonUtf8Buffer), false); | ||
|
||
// Test basic functionality for isAscii | ||
assert.strictEqual(buffer.isAscii(asciiBuffer), true); | ||
assert.strictEqual(buffer.isAscii(nonAsciiBuffer), false); | ||
|
||
// Test detached buffers | ||
const detachedBuffer = new ArrayBuffer(10); | ||
// Let's detach the buffer if it's supported | ||
detachedBuffer.detach?.(); | ||
|
||
if (detachedBuffer.detached) { | ||
const typedArray = new Uint8Array(detachedBuffer); | ||
|
||
assert.throws(() => { | ||
buffer.isUtf8(typedArray); | ||
}, { | ||
name: 'Error', | ||
code: 'ERR_INVALID_STATE' | ||
}); | ||
|
||
assert.throws(() => { | ||
buffer.isAscii(typedArray); | ||
}, { | ||
name: 'Error', | ||
code: 'ERR_INVALID_STATE' | ||
}); | ||
} | ||
|
||
// Test optimization and fast API paths | ||
function testFastPaths() { | ||
// Test both valid and invalid cases to ensure both paths are optimized | ||
buffer.isUtf8(utf8Buffer); | ||
buffer.isUtf8(nonUtf8Buffer); | ||
buffer.isAscii(asciiBuffer); | ||
buffer.isAscii(nonAsciiBuffer); | ||
} | ||
|
||
// Since we want to optimize the C++ methods, we need to prepare them | ||
// through their JS wrappers | ||
eval('%PrepareFunctionForOptimization(buffer.isUtf8)'); | ||
eval('%PrepareFunctionForOptimization(buffer.isAscii)'); | ||
testFastPaths(); | ||
eval('%OptimizeFunctionOnNextCall(buffer.isUtf8)'); | ||
eval('%OptimizeFunctionOnNextCall(buffer.isAscii)'); | ||
testFastPaths(); | ||
|
||
// Verify fast API calls were made if running in debug mode | ||
if (common.isDebug) { | ||
const { getV8FastApiCallCount } = internalBinding('debug'); | ||
assert.strictEqual(getV8FastApiCallCount('buffer.isUtf8'), 2); // Called twice in testFastPaths | ||
assert.strictEqual(getV8FastApiCallCount('buffer.isAscii'), 2); // Called twice in testFastPaths | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.