-
-
Notifications
You must be signed in to change notification settings - Fork 236
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
Speed up buffer creation. #92
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -186,10 +186,11 @@ function fromArrayBuffer (that, array) { | |
if (Buffer.TYPED_ARRAY_SUPPORT) { | ||
// Return an augmented `Uint8Array` instance, for best performance | ||
array.byteLength | ||
that = Buffer._augment(new Uint8Array(array)) | ||
that = new Uint8Array(array) | ||
that.__proto__ = Buffer.prototype | ||
} else { | ||
// Fallback: Return an object instance of the Buffer class | ||
that = fromTypedArray(that, new Uint8Array(array)) | ||
that = fromTypedArray(that, array) | ||
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. This will break if the user passes an ArrayBuffer into the Buffer object implementation. |
||
} | ||
return that | ||
} | ||
|
@@ -223,6 +224,7 @@ function fromJsonObject (that, object) { | |
|
||
if (Buffer.TYPED_ARRAY_SUPPORT) { | ||
Buffer.prototype.__proto__ = Uint8Array.prototype | ||
Buffer.prototype._set = Uint8Array.prototype.set // For `copy` below. | ||
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. I know you just copied this but I wonder if we can't just use 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. Seems to work. I'll make the change. |
||
Buffer.__proto__ = Uint8Array | ||
} else { | ||
// pre-set for values that may exist in the future | ||
|
@@ -233,12 +235,11 @@ if (Buffer.TYPED_ARRAY_SUPPORT) { | |
function allocate (that, length) { | ||
if (Buffer.TYPED_ARRAY_SUPPORT) { | ||
// Return an augmented `Uint8Array` instance, for best performance | ||
that = Buffer._augment(new Uint8Array(length)) | ||
that = new Uint8Array(length) | ||
that.__proto__ = Buffer.prototype | ||
} else { | ||
// Fallback: Return an object instance of the Buffer class | ||
that.length = length | ||
that._isBuffer = true | ||
} | ||
|
||
var fromPool = length !== 0 && length <= Buffer.poolSize >>> 1 | ||
|
@@ -421,6 +422,10 @@ function slowToString (encoding, start, end) { | |
} | ||
} | ||
|
||
// Even though this property is private, it shouldn't be removed because it is | ||
// used by `is-buffer` to detect buffer instances in Safari 5-7. | ||
Buffer.prototype._isBuffer = true | ||
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. This was only being set in the fallback case originally, why is it now always set? 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. It was already always set (since it's added inside 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. Why is it still added inside edit: Nevermind, great work @mtth, I feel like I answered my own question (emphasis: augment). Looks good! |
||
|
||
Buffer.prototype.toString = function toString () { | ||
var length = this.length | 0 | ||
if (length === 0) return '' | ||
|
@@ -798,7 +803,8 @@ Buffer.prototype.slice = function slice (start, end) { | |
|
||
var newBuf | ||
if (Buffer.TYPED_ARRAY_SUPPORT) { | ||
newBuf = Buffer._augment(this.subarray(start, end)) | ||
newBuf = this.subarray(start, end) | ||
newBuf.__proto__ = Buffer.prototype | ||
} else { | ||
var sliceLen = end - start | ||
newBuf = new Buffer(sliceLen, undefined) | ||
|
@@ -1322,6 +1328,9 @@ var BP = Buffer.prototype | |
|
||
/** | ||
* Augment a Uint8Array *instance* (not the Uint8Array class!) with Buffer methods | ||
* | ||
* This function is also used externally by `typedarray-to-buffer`. | ||
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. 👍 this is good. |
||
* | ||
*/ | ||
Buffer._augment = function _augment (arr) { | ||
arr.constructor = Buffer | ||
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. I think we still need this line, or 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. Nevermind. This is inherited from 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. Thinking about it a bit more, I think we actually need this line. Not for this package, but for 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.
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. Great! |
||
|
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 think this is no-op that we can remove. Just noticed it, lol
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.
Actually, never mind. Heh. I remember why I added this. I'll add a 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.
Now you have me curious :)