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 14 commits into
base: unstable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = 200;
/** Snappyjs is faster at uncompressing small buffers */
const SNAPPYJS_UNCOMPRESS_THRESHOLD = 400;

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));
}
}
}
31 changes: 31 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,31 @@
import {randomBytes} from "node:crypto";
import * as snappyjs from "snappyjs";
import * as snappy from "snappy";
import {itBench} from "@dapplion/benchmark";

describe("network / gossip / snappy", () => {
for (const msgLen of [100, 200, 300, 400, 500, 1000, 10000, 100000]) {
const uncompressed = randomBytes(msgLen);
const compressed = snappyjs.compress(uncompressed);

itBench({
id: `snappyjs compress ${msgLen} bytes`,
fn: () => void snappyjs.compress(uncompressed),
wemeetagain marked this conversation as resolved.
Show resolved Hide resolved
});

itBench({
id: `snappyjs uncompress ${msgLen} bytes`,
fn: () => void snappyjs.uncompress(compressed),
});

itBench({
id: `snappy compress ${msgLen} bytes`,
fn: () => void snappy.compressSync(uncompressed),
});

itBench({
id: `snappy uncompress ${msgLen} bytes`,
fn: () => void snappy.uncompressSync(compressed),
});
}
});
Loading