Skip to content

Commit 2be73dc

Browse files
authored
fix: concatenate encrypted data length prefix with data before sending (#387)
fix: concatenate data length prefix Sending lots of tiny buffers kills TCP performance, even with `noDelay` disabled. Sending the encrypted data length along with the data in one buffer increases `@libp2p/perf` throughput with noise+yamux from 300-320 MB/s to 320-340 MB/s
1 parent 3dee1dc commit 2be73dc

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/crypto/streaming.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { concat as uint8ArrayConcat } from 'uint8arrays'
12
import { NOISE_MSG_MAX_LENGTH_BYTES, NOISE_MSG_MAX_LENGTH_BYTES_WITHOUT_TAG } from '../constants.js'
23
import { uint16BEEncode } from '../encoder.js'
34
import type { IHandshake } from '../@types/handshake-interface.js'
@@ -20,8 +21,10 @@ export function encryptStream (handshake: IHandshake, metrics?: MetricsRegistry)
2021
const data = handshake.encrypt(chunk.subarray(i, end), handshake.session)
2122
metrics?.encryptedPackets.increment()
2223

23-
yield uint16BEEncode(data.byteLength)
24-
yield data
24+
yield uint8ArrayConcat([
25+
uint16BEEncode(data.byteLength),
26+
data
27+
], 2 + data.byteLength)
2528
}
2629
}
2730
}

0 commit comments

Comments
 (0)