diff --git a/packages/beacon-node/src/network/gossip/encoding.ts b/packages/beacon-node/src/network/gossip/encoding.ts index 32cc36d9a2f..59b5ff665fe 100644 --- a/packages/beacon-node/src/network/gossip/encoding.ts +++ b/packages/beacon-node/src/network/gossip/encoding.ts @@ -104,7 +104,8 @@ export class DataTransformSnappy implements DataTransform { } // Only after sanity length checks, we can decompress the data - const uncompressedData = Buffer.allocUnsafe(uncompressedDataLength); + // Using Buffer.alloc() instead of Buffer.allocUnsafe() to mitigate high GC pressure observed in some environments + const uncompressedData = Buffer.alloc(uncompressedDataLength); decoder.decompress_into(data, uncompressedData); return uncompressedData; } @@ -120,7 +121,8 @@ export class DataTransformSnappy implements DataTransform { throw Error(`ssz_snappy encoded data length ${data.length} > ${this.maxSizePerMessage}`); } - const compressedData = Buffer.allocUnsafe(snappyWasm.max_compress_len(data.length)); + // Using Buffer.alloc() instead of Buffer.allocUnsafe() to mitigate high GC pressure observed in some environments + const compressedData = Buffer.alloc(snappyWasm.max_compress_len(data.length)); const compressedLen = encoder.compress_into(data, compressedData); return compressedData.subarray(0, compressedLen); } diff --git a/packages/beacon-node/test/perf/network/gossip/snappy.test.ts b/packages/beacon-node/test/perf/network/gossip/snappy.test.ts index 514ab7fe99f..954cfb550c7 100644 --- a/packages/beacon-node/test/perf/network/gossip/snappy.test.ts +++ b/packages/beacon-node/test/perf/network/gossip/snappy.test.ts @@ -57,7 +57,7 @@ describe("network / gossip / snappy", () => { runsFactor: RUNS_FACTOR, fn: () => { for (let i = 0; i < RUNS_FACTOR; i++) { - let out = Buffer.allocUnsafe(snappyWasm.max_compress_len(uncompressed.length)); + let out = Buffer.alloc(snappyWasm.max_compress_len(uncompressed.length)); const len = encoder.compress_into(uncompressed, out); out = out.subarray(0, len); } @@ -108,7 +108,7 @@ describe("network / gossip / snappy", () => { runsFactor: RUNS_FACTOR, fn: () => { for (let i = 0; i < RUNS_FACTOR; i++) { - decoder.decompress_into(compressed, Buffer.allocUnsafe(snappyWasm.decompress_len(compressed))); + decoder.decompress_into(compressed, Buffer.alloc(snappyWasm.decompress_len(compressed))); } }, });