avoid unnecessary async copies in broadcast (#3830)

This commit is contained in:
Jacek Sieka 2022-07-01 16:48:45 +02:00 committed by GitHub
parent 4fbbbfd462
commit 6a3bd89d09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 22 deletions

View File

@ -2419,21 +2419,17 @@ proc addAsyncValidator*[MsgType](node: Eth2Node,
proc unsubscribe*(node: Eth2Node, topic: string) = proc unsubscribe*(node: Eth2Node, topic: string) =
node.pubsub.unsubscribeAll(topic) node.pubsub.unsubscribeAll(topic)
proc broadcast(node: Eth2Node, topic: string, msg: auto): proc gossipEncode(msg: auto): seq[byte] =
Future[Result[void, cstring]] {.async.} =
try:
let uncompressed = SSZ.encode(msg) let uncompressed = SSZ.encode(msg)
# This function only for messages we create. A message this large amounts to
# This is only for messages we create. A message this large amounts to an # an internal logic error.
# internal logic error.
doAssert uncompressed.len <= maxGossipMaxSize() doAssert uncompressed.len <= maxGossipMaxSize()
let compressed = snappy.encode(uncompressed)
try: snappy.encode(uncompressed)
except InputTooLarge:
raiseAssert "More than 4gb? not likely.."
let peers = await node.pubsub.publish(topic, compressed) proc broadcast(node: Eth2Node, topic: string, msg: seq[byte]):
Future[Result[void, cstring]] {.async.} =
let peers = await node.pubsub.publish(topic, msg)
# TODO remove workaround for sync committee BN/VC log spam # TODO remove workaround for sync committee BN/VC log spam
if peers > 0 or find(topic, "sync_committee_") != -1: if peers > 0 or find(topic, "sync_committee_") != -1:
@ -2442,8 +2438,11 @@ proc broadcast(node: Eth2Node, topic: string, msg: auto):
else: else:
# Increments libp2p_gossipsub_failed_publish metric # Increments libp2p_gossipsub_failed_publish metric
return err("No peers on libp2p topic") return err("No peers on libp2p topic")
except IOError as exc:
raiseAssert exc.msg # TODO in-memory compression shouldn't fail proc broadcast(node: Eth2Node, topic: string, msg: auto):
Future[Result[void, cstring]] =
# Avoid {.async.} copies of message while broadcasting
broadcast(node, topic, gossipEncode(msg))
proc subscribeAttestationSubnets*( proc subscribeAttestationSubnets*(
node: Eth2Node, subnets: AttnetBits, forkDigest: ForkDigest) = node: Eth2Node, subnets: AttnetBits, forkDigest: ForkDigest) =