Skip to content

Commit

Permalink
fix: concatenate encrypted data length prefix with data before sending (
Browse files Browse the repository at this point in the history
#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
  • Loading branch information
achingbrain committed Nov 13, 2023
1 parent 3dee1dc commit 2be73dc
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/crypto/streaming.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { concat as uint8ArrayConcat } from 'uint8arrays'
import { NOISE_MSG_MAX_LENGTH_BYTES, NOISE_MSG_MAX_LENGTH_BYTES_WITHOUT_TAG } from '../constants.js'
import { uint16BEEncode } from '../encoder.js'
import type { IHandshake } from '../@types/handshake-interface.js'
Expand All @@ -20,8 +21,10 @@ export function encryptStream (handshake: IHandshake, metrics?: MetricsRegistry)
const data = handshake.encrypt(chunk.subarray(i, end), handshake.session)
metrics?.encryptedPackets.increment()

yield uint16BEEncode(data.byteLength)
yield data
yield uint8ArrayConcat([
uint16BEEncode(data.byteLength),
data
], 2 + data.byteLength)
}
}
}
Expand Down

0 comments on commit 2be73dc

Please sign in to comment.