From 4a328609b96609dbeb8dc98e19aa2f52d2e5eaab Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Wed, 4 Jan 2023 03:30:15 -0800 Subject: 10x faster `new Buffer` (#1717) Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> --- src/bun.js/builtins/js/JSBufferConstructor.js | 10 +++- src/bun.js/builtins/js/JSBufferPrototype.js | 74 +++++++++++++-------------- 2 files changed, 46 insertions(+), 38 deletions(-) (limited to 'src/bun.js/builtins/js') diff --git a/src/bun.js/builtins/js/JSBufferConstructor.js b/src/bun.js/builtins/js/JSBufferConstructor.js index bfb122034..48342fe26 100644 --- a/src/bun.js/builtins/js/JSBufferConstructor.js +++ b/src/bun.js/builtins/js/JSBufferConstructor.js @@ -25,6 +25,14 @@ // ^ that comment is required or the builtins generator will have a fit. +function alloc(n) { + "use strict"; + if (typeof n !== "number" || n < 0) { + @throwRangeError("n must be a positive integer less than 2^32"); + } + + return new this(n); +} function from(items) { "use strict"; @@ -64,5 +72,5 @@ function from(items) { // Don't pass the second argument because Node's Buffer.from doesn't accept // a function and Uint8Array.from requires it if it exists // That means we cannot use @tailCallFowrardArguments here, sadly - return this.toBuffer(@Uint8Array.from(arrayLike)); + return new this(@Uint8Array.from(arrayLike).buffer); } diff --git a/src/bun.js/builtins/js/JSBufferPrototype.js b/src/bun.js/builtins/js/JSBufferPrototype.js index 689dc2dde..ef3332d88 100644 --- a/src/bun.js/builtins/js/JSBufferPrototype.js +++ b/src/bun.js/builtins/js/JSBufferPrototype.js @@ -31,176 +31,176 @@ function setBigUint64(offset, value, le) { "use strict"; - return this.dataView.setBigUint64(offset, value, le); + return (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).setBigUint64(offset, value, le); } function readInt8(offset) { "use strict"; - return this.dataView.getInt8(offset); + return (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).getInt8(offset); } function readUInt8(offset) { "use strict"; - return this.dataView.getUint8(offset); + return (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).getUint8(offset); } function readInt16LE(offset) { "use strict"; - return this.dataView.getInt16(offset, true); + return (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).getInt16(offset, true); } function readInt16BE(offset) { "use strict"; - return this.dataView.getInt16(offset, false); + return (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).getInt16(offset, false); } function readUInt16LE(offset) { "use strict"; - return this.dataView.getUint16(offset, true); + return (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).getUint16(offset, true); } function readUInt16BE(offset) { "use strict"; - return this.dataView.getUint16(offset, false); + return (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).getUint16(offset, false); } function readInt32LE(offset) { "use strict"; - return this.dataView.getInt32(offset, true); + return (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).getInt32(offset, true); } function readInt32BE(offset) { "use strict"; - return this.dataView.getInt32(offset, false); + return (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).getInt32(offset, false); } function readUInt32LE(offset) { "use strict"; - return this.dataView.getUint32(offset, true); + return (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).getUint32(offset, true); } function readUInt32BE(offset) { "use strict"; - return this.dataView.getUint32(offset, false); + return (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).getUint32(offset, false); } function readFloatLE(offset) { "use strict"; - return this.dataView.getFloat32(offset, true); + return (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).getFloat32(offset, true); } function readFloatBE(offset) { "use strict"; - return this.dataView.getFloat32(offset, false); + return (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).getFloat32(offset, false); } function readDoubleLE(offset) { "use strict"; - return this.dataView.getFloat64(offset, true); + return (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).getFloat64(offset, true); } function readDoubleBE(offset) { "use strict"; - return this.dataView.getFloat64(offset, false); + return (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).getFloat64(offset, false); } function readBigInt64LE(offset) { "use strict"; - return this.dataView.getBigInt64(offset, true); + return (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).getBigInt64(offset, true); } function readBigInt64BE(offset) { "use strict"; - return this.dataView.getBigInt64(offset, false); + return (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).getBigInt64(offset, false); } function readBigUInt64LE(offset) { "use strict"; - return this.dataView.getBigUint64(offset, true); + return (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).getBigUint64(offset, true); } function readBigUInt64BE(offset) { "use strict"; - return this.dataView.getBigUint64(offset, false); + return (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).getBigUint64(offset, false); } function writeInt8(value, offset) { "use strict"; - this.dataView.setInt8(offset, value); + (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).setInt8(offset, value); return offset + 1; } function writeUInt8(value, offset) { "use strict"; - this.dataView.setUint8(offset, value); + (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).setUint8(offset, value); return offset + 1; } function writeInt16LE(value, offset) { "use strict"; - this.dataView.setInt16(offset, value, true); + (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).setInt16(offset, value, true); return offset + 2; } function writeInt16BE(value, offset) { "use strict"; - this.dataView.setInt16(offset, value, false); + (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).setInt16(offset, value, false); return offset + 2; } function writeUInt16LE(value, offset) { "use strict"; - this.dataView.setUint16(offset, value, true); + (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).setUint16(offset, value, true); return offset + 2; } function writeUInt16BE(value, offset) { "use strict"; - this.dataView.setUint16(offset, value, false); + (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).setUint16(offset, value, false); return offset + 2; } function writeInt32LE(value, offset) { "use strict"; - this.dataView.setInt32(offset, value, true); + (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).setInt32(offset, value, true); return offset + 4; } function writeInt32BE(value, offset) { "use strict"; - this.dataView.setInt32(offset, value, false); + (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).setInt32(offset, value, false); return offset + 4; } function writeUInt32LE(value, offset) { "use strict"; - this.dataView.setUint32(offset, value, true); + (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).setUint32(offset, value, true); return offset + 4; } function writeUInt32BE(value, offset) { "use strict"; - this.dataView.setUint32(offset, value, false); + (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).setUint32(offset, value, false); return offset + 4; } function writeFloatLE(value, offset) { "use strict"; - this.dataView.setFloat32(offset, value, true); + (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).setFloat32(offset, value, true); return offset + 4; } function writeFloatBE(value, offset) { "use strict"; - this.dataView.setFloat32(offset, value, false); + (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).setFloat32(offset, value, false); return offset + 4; } function writeDoubleLE(value, offset) { "use strict"; - this.dataView.setFloat64(offset, value, true); + (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).setFloat64(offset, value, true); return offset + 8; } function writeDoubleBE(value, offset) { "use strict"; - this.dataView.setFloat64(offset, value, false); + (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).setFloat64(offset, value, false); return offset + 8; } function writeBigInt64LE(value, offset) { "use strict"; - this.dataView.setBigInt64(offset, value, true); + (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).setBigInt64(offset, value, true); return offset + 8; } function writeBigInt64BE(value, offset) { "use strict"; - this.dataView.setBigInt64(offset, value, false); + (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).setBigInt64(offset, value, false); return offset + 8; } function writeBigUInt64LE(value, offset) { "use strict"; - this.dataView.setBigUint64(offset, value, true); + (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).setBigUint64(offset, value, true); return offset + 8; } function writeBigUInt64BE(value, offset) { "use strict"; - this.dataView.setBigUint64(offset, value, false); + (this.@dataView ||= new DataView(this.buffer, this.byteOffset, this.byteLength)).setBigUint64(offset, value, false); return offset + 8; } -- cgit v1.2.3 Unnamed repository; edit this file 'description' to name the repository.
aboutsummaryrefslogtreecommitdiff
path: root/test/snippets/segfault.js (unfollow)
AgeCommit message (Expand)AuthorFilesLines
2023-10-11Update nodejs-apis.mdGravatar Jarred Sumner 1-1/+1
2023-10-10Fix lifecycle scripts not running on reinstallation (#6376)Gravatar Arden Sinclair 3-9/+59
2023-10-10feat(test): implement `toEqualIgnoringWhitespace` (#6293)Gravatar Elad Bezalel 7-1/+165
2023-10-10fix: form data content type (#6380)Gravatar saurabh 1-2/+5
2023-10-10make peer dependencies install by default (#6396)Gravatar Dylan Conway 8-17/+344
2023-10-10Add File to binary data TOC (#6025)Gravatar Aaron Dewes 1-3/+3
2023-10-10docs: rearranged cli/runtime related sections (#6275)Gravatar cyfung1031 8-305/+110
2023-10-10Update inspector-protocolGravatar Ashcon Partovi 3-99/+357
2023-10-10Update debug-adapter-protocolGravatar Ashcon Partovi 3-11/+33
2023-10-10Add missing ws declarations (#6307)Gravatar Vasilis Themelis 1-0/+4
2023-10-10Update vite.md (#6399)Gravatar Clément P 1-2/+1
2023-10-10Documentation for the IPC of Bun.spawn (#6400)Gravatar Nicolae-Rares Ailincai 3-0/+122
2023-10-09fix(AbortSignal/fetch) fix AbortSignal.timeout, fetch lock behavior and fetch...Gravatar Ciro Spaciari 29-61/+303
2023-10-09Fix npm tag for canary bun-types, againGravatar Ashcon Partovi 2-56/+10
2023-10-09Add Fedora build instructions to development.md (#6359)Gravatar otterDeveloper 1-0/+10
2023-10-09added commands (#6314)Gravatar babar 1-1/+2
2023-10-09Update README.md (#6291)Gravatar TPLJ 1-1/+1
2023-10-09docs: fixing a couple typos (#6331)Gravatar Michael Di Prisco 2-2/+2
2023-10-09fix: support uint8 exit code range (#6303)Gravatar Liz 2-2/+11
2023-10-09Fix array variables preview in debugger (#6379)Gravatar 2hu 1-1/+4
2023-10-07feat(KeyObject) (#5940)Gravatar Ciro Spaciari 106-67/+9342
2023-10-07Exclude more filesGravatar Jarred Sumner 1-1/+1
2023-10-07Exclude more filesGravatar Jarred Sumner 1-1/+2
2023-10-07Update settings.jsonGravatar Jarred Sumner 1-1/+2
2023-10-07Update settings.jsonGravatar Jarred Sumner 1-2/+3
2023-10-06fix a couple install testsGravatar Dylan Conway 1-8/+8
2023-10-06formatGravatar Dylan Conway 1-1/+2
2023-10-06Fix memory leak in fetch() (#6350)Gravatar Jarred Sumner 1-2/+0
2023-10-06[types] allow onLoad plugin callbacks to return undefined (#6346)Gravatar Silver 1-1/+1
2023-10-06docs: `file.stream()` is not a promise (#6337)Gravatar Paul Nodet 1-1/+1