mirror of
https://github.com/status-im/nimbus-eth2.git
synced 2025-02-02 17:53:52 +00:00
1ef7d237cc
This PR allows sharing the pubkey data between validators by using a thread-local cache for pubkey data, netting about a 400mb mem usage reduction on holesky due to us keeping 3 permanent + several ephemeral state copies in memory at all times and each state copy holding a full validator. The PR also introduces a hash cache for the key which gives ~14% speedup for a full state `hash_tree_root` - the key makes up for a large part of the `Validator` htr time. Finally, the time it takes to copy a state goes down as well from ~80m ms to ~60, for reasons similar to htr. We use a `ptr` even if a `ref` could in theory have been used - there is not much practical benefit to a `ref` (given it's mutable) while a `ptr` is cheaper and easier to copy (when copying temporary states). We could go further and cache a cooked pubkey but it turns out this is quite intrusive - in all the relevant places, we're already using a cooked key from the immutable validator data so there are no immediate performance gains of doing so while managing the compressed -> cooked key mapping would become more difficult - something for a future PR perhaps. Co-authored-by: Etan Kissling <etan@status.im>
71 lines
2.6 KiB
Nim
71 lines
2.6 KiB
Nim
# beacon_chain
|
|
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
|
# Licensed and distributed under either of
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
{.push raises: [].}
|
|
|
|
import
|
|
std/[typetraits],
|
|
ssz_serialization/codec,
|
|
./datatypes/base
|
|
|
|
from ./datatypes/altair import
|
|
ParticipationFlags, EpochParticipationFlags
|
|
|
|
export codec, base, typetraits, EpochParticipationFlags
|
|
|
|
# Coding and decoding of SSZ to spec-specific types
|
|
|
|
template toSszType*(v: Slot|Epoch|SyncCommitteePeriod): auto = uint64(v)
|
|
template toSszType*(v: BlsCurveType): auto = toRaw(v)
|
|
template toSszType*(v: ForkDigest|GraffitiBytes): auto = distinctBase(v)
|
|
template toSszType*(v: Version): auto = distinctBase(v)
|
|
template toSszType*(v: JustificationBits): auto = distinctBase(v)
|
|
template toSszType*(v: EpochParticipationFlags): auto = asList v
|
|
|
|
func fromSszBytes*(
|
|
T: type GraffitiBytes, data: openArray[byte]): T {.raises: [SszError].} =
|
|
if data.len != sizeof(result):
|
|
raiseIncorrectSize T
|
|
copyMem(result.addr, unsafeAddr data[0], sizeof(result))
|
|
|
|
template fromSszBytes*(T: type Slot, bytes: openArray[byte]): T =
|
|
T fromSszBytes(uint64, bytes)
|
|
|
|
template fromSszBytes*(T: type Epoch, bytes: openArray[byte]): T =
|
|
T fromSszBytes(uint64, bytes)
|
|
|
|
template fromSszBytes*(T: type SyncCommitteePeriod, bytes: openArray[byte]): T =
|
|
T fromSszBytes(uint64, bytes)
|
|
|
|
func fromSszBytes*(
|
|
T: type ForkDigest, bytes: openArray[byte]): T {.raises: [SszError].} =
|
|
if bytes.len != sizeof(result):
|
|
raiseIncorrectSize T
|
|
copyMem(result.addr, unsafeAddr bytes[0], sizeof(result))
|
|
|
|
func fromSszBytes*(
|
|
T: type Version, bytes: openArray[byte]): T {.raises: [SszError].} =
|
|
if bytes.len != sizeof(result):
|
|
raiseIncorrectSize T
|
|
copyMem(result.addr, unsafeAddr bytes[0], sizeof(result))
|
|
|
|
func fromSszBytes*(
|
|
T: type JustificationBits, bytes: openArray[byte]
|
|
): T {.raises: [SszError].} =
|
|
if bytes.len != sizeof(result):
|
|
raiseIncorrectSize T
|
|
copyMem(result.addr, unsafeAddr bytes[0], sizeof(result))
|
|
|
|
func fromSszBytes*(
|
|
T: type EpochParticipationFlags, bytes: openArray[byte]
|
|
): T {.raises: [SszError].} =
|
|
# TODO https://github.com/nim-lang/Nim/issues/21123
|
|
let tmp = cast[ptr List[ParticipationFlags, Limit VALIDATOR_REGISTRY_LIMIT]](addr result)
|
|
readSszValue(bytes, tmp[])
|
|
|
|
template toSszType*(v: HashedValidatorPubKey): auto = toRaw(v.pubkey)
|