Skip to content

Commit

Permalink
Fix Uint64 DataView benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
dapplion committed Jan 15, 2022
1 parent 683bd44 commit 64be353
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions packages/ssz/test/perf/uintFromBytes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,40 @@ describe("Uint64 deserialize", () => {

// Single leafNode

itBench(`UintBigint${bitLength} x ${numElements} deserialize`, () => {
before("Create random bytes", () => {
const firstValue = byteLength >= 8 ? 30e9 : 2 ** (bitLength - 1);
const maxValue = byteLength >= 8 ? Number.MAX_SAFE_INTEGER : 2 ** bitLength;

arrayBuffer = new ArrayBuffer(numElements * byteLength);
uint8Array = new Uint8Array(arrayBuffer);
dataView = new DataView(arrayBuffer);

for (let i = 0; i < numElements; i++) {
const value = (firstValue + i) % maxValue;
switch (byteLength) {
case 1:
dataView.setUint8(i * byteLength, value);
break;
case 2:
dataView.setUint16(i * byteLength, value, true);
break;
case 4:
dataView.setUint32(i * byteLength, value, true);
break;
case 8:
dataView.setBigUint64(i * byteLength, BigInt(value), true);
break;
}
}
});

itBench(`UintBigint${bitLength} x ${numElements} tree_deserialize`, () => {
for (let i = 0; i < numElements; i++) {
uintNumberType.tree_deserializeFromBytes({uint8Array, dataView}, i * byteLength, (i + 1) * byteLength);
}
});

itBench(`UintBigint${bitLength} x ${numElements} serialize`, () => {
itBench(`UintBigint${bitLength} x ${numElements} tree_serialize`, () => {
const arrayBuf = new ArrayBuffer(arrayBuffer.byteLength);
const dataView = new DataView(arrayBuf);
const uint8Array = new Uint8Array(arrayBuf);
Expand Down Expand Up @@ -81,13 +108,13 @@ describe("Uint64 deserialize", () => {
}
});

itBench(`UintBigint${bitLength} x ${numElements} deserialize`, () => {
itBench(`UintBigint${bitLength} x ${numElements} value_deserialize`, () => {
for (let i = 0; i < numElements; i++) {
uintNumberType.value_deserializeFromBytes({uint8Array, dataView}, i * byteLength, (i + 1) * byteLength);
}
});

itBench(`UintBigint${bitLength} x ${numElements} serialize`, () => {
itBench(`UintBigint${bitLength} x ${numElements} value_serialize`, () => {
const arrayBuf = new ArrayBuffer(arrayBuffer.byteLength);
const dataView = new DataView(arrayBuf);
for (let i = 0; i < numElements; i++) {
Expand Down

0 comments on commit 64be353

Please sign in to comment.