Skip to content

Commit

Permalink
Added more performant resize
Browse files Browse the repository at this point in the history
  • Loading branch information
BlobMaster41 committed May 16, 2024
1 parent a0b6665 commit 2045c04
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type": "module",
"name": "@btc-vision/bsi-binary",
"version": "1.0.13",
"version": "1.0.14",
"description": "",
"main": "build/index.js",
"types": "build/index.d.ts",
Expand Down
11 changes: 9 additions & 2 deletions src/buffer/BinaryWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export class BinaryWriter {

this.allocSafe(8);
this.buffer.setBigUint64(this.currentOffset, value, true);
this.currentOffset += 8;
}

public writeSelector(value: Selector): void {
Expand All @@ -82,6 +83,8 @@ export class BinaryWriter {
public writeU256(bigIntValue: bigint): void {
if(this.trackDataTypes) this.selectorDatatype.push(BufferDataType.U256);

this.allocSafe(32);

const bytesToHex = BufferHelper.valueToUint8Array(bigIntValue);
if (bytesToHex.byteLength !== 32) {
console.log('Invalid u256 value:', bytesToHex);
Expand All @@ -95,13 +98,16 @@ export class BinaryWriter {
}

public writeBytes(value: Uint8Array | Buffer): void {
this.allocSafe(value.byteLength);

for (let i = 0; i < value.byteLength; i++) {
this.writeU8(value[i]);
}
}

public writeString(value: string): void {
if(this.trackDataTypes) this.selectorDatatype.push(BufferDataType.STRING);
this.allocSafe(value.length);

for (let i: i32 = 0; i < value.length; i++) {
this.writeU8(value.charCodeAt(i));
Expand All @@ -112,13 +118,13 @@ export class BinaryWriter {
if(this.trackDataTypes) this.selectorDatatype.push(BufferDataType.ADDRESS);

const bytes = this.fromAddress(value);

this.writeBytes(bytes);
}

public writeStringWithLength(value: string): void {
this.writeU16(value.length);
this.allocSafe(value.length + 2);

this.writeU16(value.length);
this.writeString(value);
}

Expand Down Expand Up @@ -190,6 +196,7 @@ export class BinaryWriter {
}

public writeTuple(values: bigint[]): void {
this.allocSafe(4 + values.length * 32);
this.writeU32(values.length);

for (let i = 0; i < values.length; i++) {
Expand Down

0 comments on commit 2045c04

Please sign in to comment.