Skip to content
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

feat: use snappy-wasm #6483

Open
wants to merge 12 commits into
base: unstable
Choose a base branch
from
1 change: 1 addition & 0 deletions packages/beacon-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
"multiformats": "^11.0.1",
"prom-client": "^15.1.0",
"qs": "^6.11.1",
"snappy": "^7.2.2",
"snappyjs": "^0.7.0",
"strict-event-emitter-types": "^2.0.0",
"systeminformation": "^5.17.12",
Expand Down
21 changes: 18 additions & 3 deletions packages/beacon-node/src/network/gossip/encoding.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {compress, uncompress} from "snappyjs";
import * as snappyjs from "snappyjs";
import * as snappy from "snappy";
import xxhashFactory from "xxhash-wasm";
import {Message} from "@libp2p/interface";
import {digest} from "@chainsafe/as-sha256";
Expand Down Expand Up @@ -61,6 +62,11 @@ export function msgIdFn(gossipTopicCache: GossipTopicCache, msg: Message): Uint8
return Buffer.from(digest(Buffer.concat(vec))).subarray(0, 20);
}

/** Snappyjs is faster at compressing small buffers */
const SNAPPYJS_COMPRESS_THRESHOLD = 100;
/** Snappyjs is faster at uncompressing small buffers */
const SNAPPYJS_UNCOMPRESS_THRESHOLD = 200;

export class DataTransformSnappy {
constructor(
private readonly gossipTopicCache: GossipTopicCache,
Expand All @@ -74,7 +80,12 @@ export class DataTransformSnappy {
* - `outboundTransform()`: compress snappy payload
*/
inboundTransform(topicStr: string, data: Uint8Array): Uint8Array {
const uncompressedData = uncompress(data, this.maxSizePerMessage);
let uncompressedData: Uint8Array;
if (data.length < SNAPPYJS_UNCOMPRESS_THRESHOLD) {
uncompressedData = snappyjs.uncompress(data, this.maxSizePerMessage);
} else {
uncompressedData = snappy.uncompressSync(Buffer.from(data)) as Uint8Array;
}

// check uncompressed data length before we extract beacon block root, slot or
// attestation data at later steps
Expand All @@ -101,6 +112,10 @@ export class DataTransformSnappy {
throw Error(`ssz_snappy encoded data length ${length} > ${this.maxSizePerMessage}`);
}
// No need to parse topic, everything is snappy compressed
return compress(data);
if (data.length < SNAPPYJS_COMPRESS_THRESHOLD) {
return snappyjs.compress(data);
} else {
return snappy.compressSync(Buffer.from(data));
}
}
}
61 changes: 61 additions & 0 deletions packages/beacon-node/test/perf/network/gossip/snappy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {randomBytes} from "node:crypto";
import * as snappyjs from "snappyjs";
import * as snappy from "snappy";
import {itBench} from "@dapplion/benchmark";

describe("network / gossip / snappy", () => {
const msgLens = [100, 200, 300, 400, 500, 1000, 10000, 100000];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used to see blocks with 2MB, need to add 2MB, 5MB and 10MB (max size of gossip message) message length

describe("compress", () => {
for (const msgLen of msgLens) {
const uncompressed = randomBytes(msgLen);
const RUNS_FACTOR = 1000;

itBench({
id: `${msgLen} bytes - compress - snappyjs`,
runsFactor: RUNS_FACTOR,
fn: () => {
for (let i = 0; i < RUNS_FACTOR; i++) {
snappyjs.compress(uncompressed);
}
},
});

itBench({
id: `${msgLen} bytes - compress - snappy`,
runsFactor: RUNS_FACTOR,
fn: () => {
for (let i = 0; i < RUNS_FACTOR; i++) {
snappy.compressSync(uncompressed);
}
},
});
}
});
describe("uncompress", () => {
for (const msgLen of [100, 200, 300, 400, 500, 1000, 10000, 100000]) {
const uncompressed = randomBytes(msgLen);
const compressed = snappyjs.compress(uncompressed);
const RUNS_FACTOR = 1000;

itBench({
id: `${msgLen} bytes - uncompress - snappyjs`,
runsFactor: RUNS_FACTOR,
fn: () => {
for (let i = 0; i < RUNS_FACTOR; i++) {
snappyjs.uncompress(compressed);
}
},
});

itBench({
id: `${msgLen} bytes - uncompress - snappy`,
runsFactor: RUNS_FACTOR,
fn: () => {
for (let i = 0; i < RUNS_FACTOR; i++) {
snappy.uncompressSync(compressed);
}
},
});
}
});
});
Loading