avoid unnecessary seq allocations (#1573)

"sigh"
This commit is contained in:
Jacek Sieka 2020-08-27 08:32:51 +02:00 committed by Mamy Ratsimbazafy
parent 0e64b4e3b3
commit a0bff42016
11 changed files with 36 additions and 30 deletions

View File

@ -24,7 +24,8 @@ func is_aggregator(state: BeaconState, slot: Slot, index: CommitteeIndex,
let
committee_len = get_beacon_committee_len(state, slot, index, cache)
modulo = max(1'u64, committee_len div TARGET_AGGREGATORS_PER_COMMITTEE)
bytes_to_uint64(eth2digest(slot_signature.toRaw()).data[0..7]) mod modulo == 0
bytes_to_uint64(eth2digest(
slot_signature.toRaw()).data.toOpenArray(0, 7)) mod modulo == 0
proc aggregate_attestations*(
pool: AttestationPool, state: BeaconState, index: CommitteeIndex,

View File

@ -300,7 +300,7 @@ proc getAttestationsForBlock*(pool: AttestationPool,
continue
for v in a.validations[1..^1]:
for i in 1..a.validations.high:
# TODO We need to select a set of attestations that maximise profit by
# adding the largest combined attestation set that we can find - this
# unfortunately looks an awful lot like
@ -308,9 +308,10 @@ proc getAttestationsForBlock*(pool: AttestationPool,
# and naively add as much as possible in one go, by we could also
# add the same attestation data twice, as long as there's at least
# one new attestation in there
if not attestation.aggregation_bits.overlaps(v.aggregation_bits):
attestation.aggregation_bits.combine(v.aggregation_bits)
agg.aggregate(v.aggregate_signature)
if not attestation.aggregation_bits.overlaps(
a.validations[i].aggregation_bits):
attestation.aggregation_bits.combine(a.validations[i].aggregation_bits)
agg.aggregate(a.validations[i].aggregate_signature)
attestation.signature = agg.finish()
result.add(attestation)

View File

@ -7,7 +7,7 @@
import
# Standard library
deques, tables, hashes,
std/[deques, strformat, tables, hashes],
# Status libraries
stew/[endians2, byteutils], chronicles,
# Internals
@ -199,15 +199,15 @@ template validator_keys*(e: EpochRef): untyped = e.validator_key_store[1][]
proc shortLog*(v: BlockSlot): string =
if v.blck.slot == v.slot:
v.blck.root.data[0..3].toHex() & ":" & $v.blck.slot
&"{v.blck.root.data.toOpenArray(0, 3).toHex()}:{v.blck.slot}"
else: # There was a gap - log it
v.blck.root.data[0..3].toHex() & ":" & $v.blck.slot & "@" & $v.slot
&"{v.blck.root.data.toOpenArray(0, 3).toHex()}:{v.blck.slot}@{v.slot}"
proc shortLog*(v: BlockRef): string =
if v == nil:
"BlockRef(nil)"
else:
v.root.data[0..3].toHex() & ":" & $v.slot
&"{v.root.data.toOpenArray(0, 3).toHex()}:{v.slot}"
chronicles.formatIt BlockSlot: shortLog(it)
chronicles.formatIt BlockRef: shortLog(it)

View File

@ -79,7 +79,8 @@ func is_aggregator*(epochRef: EpochRef, slot: Slot, index: CommitteeIndex,
let
committee_len = get_beacon_committee_len(epochRef, slot, index)
modulo = max(1'u64, committee_len div TARGET_AGGREGATORS_PER_COMMITTEE)
bytes_to_uint64(eth2digest(slot_signature.toRaw()).data[0..7]) mod modulo == 0
bytes_to_uint64(eth2digest(
slot_signature.toRaw()).data.toOpenArray(0, 7)) mod modulo == 0
# https://github.com/ethereum/eth2.0-specs/blob/v0.12.2/specs/phase0/beacon-chain.md#is_valid_indexed_attestation
proc is_valid_indexed_attestation*(

View File

@ -201,7 +201,7 @@ proc toGaugeValue*(hash: Eth2Digest): int64 =
# Only the last 8 bytes are taken into consideration in accordance
# to the ETH2 metrics spec:
# https://github.com/ethereum/eth2.0-metrics/blob/6a79914cb31f7d54858c7dd57eee75b6162ec737/metrics.md#interop-metrics
cast[int64](uint64.fromBytesLE(hash.data[24..31]))
cast[int64](uint64.fromBytesLE(hash.data.toOpenArray(24, 31)))
# Codecs
# ----------------------------------------------------------------------
@ -346,16 +346,16 @@ func shortLog*(x: BlsValue): string =
# The prefix must be short
# due to the mechanics of the `shortLog` function.
if x.kind == Real:
x.blsValue.exportRaw()[0..3].toHex()
x.blsValue.exportRaw().toOpenArray(0, 3).toHex()
else:
"r:" & x.blob[0..3].toHex()
"r:" & x.blob.toOpenArray(0, 3).toHex()
func shortLog*(x: ValidatorPrivKey): string =
## Logging for raw unwrapped BLS types
"<private key>"
func shortLog*(x: TrustedSig): string =
x.data[0..3].toHex()
x.data.toOpenArray(0, 3).toHex()
# Initialization
# ----------------------------------------------------------------------

View File

@ -36,7 +36,7 @@ type
Eth2Hash* = sha256 ## Context for hash function
func shortLog*(x: Eth2Digest): string =
x.data[0..3].toHex()
x.data.toOpenArray(0, 3).toHex()
chronicles.formatIt Eth2Digest:
shortLog(it)

View File

@ -130,7 +130,7 @@ func compute_domain*(
let fork_data_root =
compute_fork_data_root(fork_version, genesis_validators_root)
result[0..3] = uint_to_bytes4(domain_type.uint64)
result[4..31] = fork_data_root.data[0..27]
result[4..31] = fork_data_root.data.toOpenArray(0, 27)
# https://github.com/ethereum/eth2.0-specs/blob/v0.12.2/specs/phase0/beacon-chain.md#get_domain
func get_domain*(

View File

@ -82,7 +82,7 @@ func addChunk(merkleizer: var SszChunksMerkleizer, data: openarray[byte]) =
merkleizer.combinedChunks[0].data[0..<data.len] = data
merkleizer.combinedChunks[0].data[data.len..<bytesPerChunk] =
zero64[0..<paddingBytes]
zero64.toOpenArray(0, paddingBytes - 1)
trs "WROTE BASE CHUNK ",
toHex(merkleizer.combinedChunks[0].data), " ", data.len

View File

@ -151,10 +151,11 @@ p2pProtocol BeaconSync(version = 1,
chainDag.getBlockRange(startSlot, reqStep,
blocks.toOpenArray(0, endIndex))
for b in blocks[startIndex..endIndex]:
doAssert not b.isNil, "getBlockRange should return non-nil blocks only"
trace "wrote response block", slot = b.slot, roor = shortLog(b.root)
await response.write(chainDag.get(b).data)
for i in startIndex..endIndex:
doAssert not blocks[i].isNil, "getBlockRange should return non-nil blocks only"
trace "wrote response block",
slot = blocks[i].slot, roor = shortLog(blocks[i].root)
await response.write(chainDag.get(blocks[i]).data)
debug "Block range request done",
peer, startSlot, count, reqStep, found = count - startIndex
@ -172,8 +173,8 @@ p2pProtocol BeaconSync(version = 1,
var found = 0
for root in blockRoots[0..<count]:
let blockRef = chainDag.getRef(root)
for i in 0..<count:
let blockRef = chainDag.getRef(blockRoots[i])
if not isNil(blockRef):
await response.write(chainDag.get(blockRef).data)
inc found

View File

@ -210,10 +210,12 @@ proc beaconBlocksByRangeUserHandler(peer: Peer; startSlot: Slot; reqCount: uint6
endIndex = count - 1
startIndex = chainDag.getBlockRange(startSlot, reqStep,
blocks.toOpenArray(0, endIndex))
for b in blocks[startIndex .. endIndex]:
doAssert not b.isNil, "getBlockRange should return non-nil blocks only"
trace "wrote response block", slot = b.slot, roor = shortLog(b.root)
await response.write(chainDag.get(b).data)
for i in startIndex .. endIndex:
doAssert not blocks[i].isNil,
"getBlockRange should return non-nil blocks only"
trace "wrote response block", slot = blocks[i].slot,
roor = shortLog(blocks[i].root)
await response.write(chainDag.get(blocks[i]).data)
debug "Block range request done", peer, startSlot, count, reqStep,
found = count - startIndex
@ -232,8 +234,8 @@ proc beaconBlocksByRootUserHandler(peer: Peer; blockRoots: BlockRootsList; respo
chainDag = peer.networkState.chainDag
count = blockRoots.len
var found = 0
for root in blockRoots[0 ..< count]:
let blockRef = chainDag.getRef(root)
for i in 0 ..< count:
let blockRef = chainDag.getRef(blockRoots[i])
if not isNil(blockRef):
await response.write(chainDag.get(blockRef).data)
inc found

2
vendor/nim-snappy vendored

@ -1 +1 @@
Subproject commit 1e506c80a90e0776014dd23ccb28abdb24a1302a
Subproject commit 8455b825e50be706eb689f7629dac7098d2d5da5