Skip to content

Commit ea3fb77

Browse files
committed
reduce new Uint8Array in Encoder
1 parent afe4f62 commit ea3fb77

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
12
test:
3+
npm run test
4+
5+
test-all:
26
npm ci
37
npm run test:browser
48
npm run test:cover
59

6-
publish: validate-git-status test
10+
publish: validate-git-status test-all
711
npm publish
812
git push origin master
913
git push origin master --tags

src/Encoder.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export const DEFAULT_INITIAL_BUFFER_SIZE = 1024;
1111
export class Encoder {
1212
private pos = 0;
1313
private view = new DataView(new ArrayBuffer(this.initialBufferSize));
14+
private bytes = new Uint8Array(this.view.buffer);
1415

1516
constructor(
1617
readonly extensionCodec = ExtensionCodec.defaultCodec,
@@ -37,7 +38,7 @@ export class Encoder {
3738
}
3839

3940
getUint8Array(): Uint8Array {
40-
return new Uint8Array(this.view.buffer, this.view.byteOffset, this.pos);
41+
return this.bytes.subarray(0, this.pos);
4142
}
4243

4344
ensureBufferSizeToWrite(sizeToWrite: number) {
@@ -50,11 +51,13 @@ export class Encoder {
5051

5152
resizeBuffer(newSize: number) {
5253
const newBuffer = new ArrayBuffer(newSize);
54+
const newBytes = new Uint8Array(newBuffer);
55+
const newView = new DataView(newBuffer);
5356

54-
new Uint8Array(newBuffer).set(new Uint8Array(this.view.buffer));
57+
newBytes.set(this.bytes);
5558

56-
const newView = new DataView(newBuffer);
5759
this.view = newView;
60+
this.bytes = newBytes;
5861
}
5962

6063
encodeNil() {
@@ -148,8 +151,7 @@ export class Encoder {
148151
const maxSize = maxHeaderSize + strLength * 4;
149152
this.ensureBufferSizeToWrite(maxSize);
150153

151-
const output = new Uint8Array(this.view.buffer, this.view.byteOffset + this.pos);
152-
const consumedLength = utf8EncodeWasm(object, output);
154+
const consumedLength = utf8EncodeWasm(object, this.bytes, this.pos);
153155
this.pos += consumedLength;
154156
return;
155157
} else {

src/wasmFunctions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function setMemoryStr(destPtr: pointer, destByteLength: number, str: string, str
4545
* It encodes string to MessagePack str family (headByte/size + utf8 bytes).
4646
* @returns The whole byte length including headByte/size.
4747
*/
48-
export function utf8EncodeWasm(str: string, output: Uint8Array): number {
48+
export function utf8EncodeWasm(str: string, output: Uint8Array, outputOffset: number): number {
4949
const strLength = str.length;
5050
const inputByteLength = strLength * 2;
5151
const inputU16BePtr: pointer = wm.malloc(inputByteLength);
@@ -55,7 +55,7 @@ export function utf8EncodeWasm(str: string, output: Uint8Array): number {
5555
const outputPtr: pointer = wm.malloc(maxOutputHeaderSize + strLength * 4);
5656
try {
5757
const outputLength = wm.utf8EncodeUint16Array(outputPtr, inputU16BePtr, strLength);
58-
output.set(new Uint8Array(wm.memory.buffer, outputPtr, outputLength));
58+
output.set(new Uint8Array(wm.memory.buffer, outputPtr, outputLength), outputOffset);
5959
return outputLength;
6060
} finally {
6161
wm.free(inputU16BePtr);

0 commit comments

Comments
 (0)