-
Notifications
You must be signed in to change notification settings - Fork 30.2k
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
lib: faster type checks for some types #15663
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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,50 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
|
||
const arrayBuffer = new ArrayBuffer(); | ||
const dataView = new DataView(arrayBuffer); | ||
const uint8Array = new Uint8Array(arrayBuffer); | ||
const int32Array = new Int32Array(arrayBuffer); | ||
|
||
const args = { | ||
ArrayBufferView: { | ||
'true': dataView, | ||
'false-primitive': true, | ||
'false-object': arrayBuffer | ||
}, | ||
TypedArray: { | ||
'true': int32Array, | ||
'false-primitive': true, | ||
'false-object': arrayBuffer | ||
}, | ||
Uint8Array: { | ||
'true': uint8Array, | ||
'false-primitive': true, | ||
'false-object': int32Array | ||
} | ||
}; | ||
|
||
const bench = common.createBenchmark(main, { | ||
type: Object.keys(args), | ||
version: ['native', 'js'], | ||
argument: ['true', 'false-primitive', 'false-object'], | ||
millions: ['5'] | ||
}, { | ||
flags: ['--expose-internals'] | ||
}); | ||
|
||
function main(conf) { | ||
const util = process.binding('util'); | ||
const types = require('internal/util/types'); | ||
|
||
const n = (+conf.millions * 1e6) | 0; | ||
const func = { native: util, js: types }[conf.version][`is${conf.type}`]; | ||
const arg = args[conf.type][conf.argument]; | ||
|
||
bench.start(); | ||
for (var i = 0; i < n; i++) { | ||
func(arg); | ||
} | ||
bench.end(n); | ||
} |
This file contains 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 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 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 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 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 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 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 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 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 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 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 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 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,36 @@ | ||
'use strict'; | ||
|
||
const ReflectApply = Reflect.apply; | ||
|
||
// This function is borrowed from the function with the same name on V8 Extras' | ||
// `utils` object. V8 implements Reflect.apply very efficiently in conjunction | ||
// with the spread syntax, such that no additional special case is needed for | ||
// function calls w/o arguments. | ||
// Refs: https://github.com/v8/v8/blob/d6ead37d265d7215cf9c5f768f279e21bd170212/src/js/prologue.js#L152-L156 | ||
function uncurryThis(func) { | ||
return (thisArg, ...args) => ReflectApply(func, thisArg, args); | ||
} | ||
|
||
const TypedArrayPrototype = Object.getPrototypeOf(Uint8Array.prototype); | ||
|
||
const TypedArrayProto_toStringTag = | ||
uncurryThis( | ||
Object.getOwnPropertyDescriptor(TypedArrayPrototype, | ||
Symbol.toStringTag).get); | ||
|
||
// Cached to make sure no userland code can tamper with it. | ||
const isArrayBufferView = ArrayBuffer.isView; | ||
|
||
function isTypedArray(value) { | ||
return TypedArrayProto_toStringTag(value) !== undefined; | ||
} | ||
|
||
function isUint8Array(value) { | ||
return TypedArrayProto_toStringTag(value) === 'Uint8Array'; | ||
} | ||
|
||
module.exports = { | ||
isArrayBufferView, | ||
isTypedArray, | ||
isUint8Array | ||
}; |
This file contains 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 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 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 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 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
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.
Have you checked the generated code for
isUint8Array
? I don't think we get theReflect.apply
optimization with V8 6.1 already.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.
Yes I have:
Note: function
b()
is in fact%TypedArray%.prototype.toStringTag
, andargs
is the name given to the wrapper function created inuncurryThis
, which as you can tell is fully inlined.Note 2: I don't really care if
Reflect.apply
is fully optimized for now, because regardless of that this function is much faster than the check we are currently using (#15663 (comment)).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.
Ok, with more recent V8 this will get even better (I also have a patch to inline
TypedArray.prototype[@@toStringTag]
). Nevertheless, why don't we just useF.p.call
here and avoiduncurryThis
completely?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.
@bmeurer
Function.prototype.call
can be overwritten by userland code. I don't want that possibility for a type checking function.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.
I see. Makes sense.