mirror of
https://github.com/vacp2p/nim-quic.git
synced 2026-01-08 21:38:05 -05:00
chore: reduce memory allocation by reducing getMessage calls (#121)
This commit is contained in:
@@ -112,10 +112,11 @@ proc disconnect(connection: Connection) {.async.} =
|
||||
proc newIncomingConnection*(
|
||||
tlsBackend: TLSBackend,
|
||||
udp: DatagramTransport,
|
||||
msg: seq[byte],
|
||||
remote: TransportAddress,
|
||||
rng: ref HmacDrbgContext,
|
||||
): Connection =
|
||||
let datagram = Datagram(data: udp.getMessage())
|
||||
let datagram = Datagram(data: msg)
|
||||
let quic =
|
||||
newQuicServerConnection(tlsBackend, udp.localAddress, remote, datagram, rng)
|
||||
let closed = newAsyncEvent()
|
||||
|
||||
@@ -42,16 +42,16 @@ proc addConnection(listener: Listener, connection: Connection, firstId: Connecti
|
||||
proc getOrCreateConnection*(
|
||||
listener: Listener,
|
||||
udp: DatagramTransport,
|
||||
msg: seq[byte],
|
||||
remote: TransportAddress,
|
||||
rng: ref HmacDrbgContext,
|
||||
): Opt[Connection] =
|
||||
var connection: Connection
|
||||
let msg = udp.getMessage()
|
||||
let destination = parseDatagram(msg).destination
|
||||
if not listener.hasConnection(destination):
|
||||
if not shouldAccept(msg):
|
||||
return Opt.none(Connection)
|
||||
connection = newIncomingConnection(listener.tlsBackend, udp, remote, rng)
|
||||
connection = newIncomingConnection(listener.tlsBackend, udp, msg, remote, rng)
|
||||
listener.addConnection(connection, destination)
|
||||
else:
|
||||
connection = listener.getConnection(destination)
|
||||
@@ -62,9 +62,11 @@ proc newListener*(
|
||||
): Listener =
|
||||
let listener = Listener(incoming: newAsyncQueue[Connection]())
|
||||
proc onReceive(udp: DatagramTransport, remote: TransportAddress) {.async.} =
|
||||
let connection = listener.getOrCreateConnection(udp, remote, rng)
|
||||
let msg = udp.getMessage()
|
||||
# call getMessage() only once to avoid unnecessary allocation
|
||||
let connection = listener.getOrCreateConnection(udp, msg, remote, rng)
|
||||
if connection.isSome():
|
||||
connection.get().receive(Datagram(data: udp.getMessage()))
|
||||
connection.get().receive(Datagram(data: msg))
|
||||
|
||||
listener.tlsBackend = tlsBackend
|
||||
listener.udp = newDatagramTransport(onReceive, local = address)
|
||||
|
||||
Reference in New Issue
Block a user