Skip to content

Commit d6167e1

Browse files
authored
Only check isomorphism of types in debug compiles (#7801)
* Only check isomorpism of types in debug compiles `default(T)` must create a T in the compiler VM which is slow
1 parent 8078bf0 commit d6167e1

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

beacon_chain/libnimbus_lc/libnimbus_lc.nim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import
1111
std/[json, sequtils, times],
12+
stew/objects,
1213
eth/common/eth_types_rlp,
1314
eth/common/keys,
1415
eth/p2p/discoveryv5/random2,
@@ -957,7 +958,7 @@ proc ETHLightClientHeaderCopyExecutionHash(
957958
root.toUnmanagedPtr()
958959

959960
type ExecutionPayloadHeader =
960-
typeof(default(lcDataFork.LightClientHeader).execution)
961+
typeof(declval(lcDataFork.LightClientHeader).execution)
961962

962963
func ETHLightClientHeaderGetExecution(
963964
header: ptr lcDataFork.LightClientHeader

beacon_chain/spec/datatypes/base.nim

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -884,19 +884,22 @@ func getSizeofSig(x: auto, n: int = 0): seq[(string, int, int)] =
884884
template isomorphicCast*[T](x: auto): T =
885885
# Each of these pairs of types has ABI-compatible memory representations.
886886
type U = typeof(x)
887+
887888
static: doAssert (T is ref) == (U is ref)
888889
when T is ref:
889-
type
890-
TT = typeof default(typeof T)[]
891-
UU = typeof default(typeof U)[]
892-
static:
893-
doAssert sizeof(TT) == sizeof(UU)
894-
doAssert getSizeofSig(TT()) == getSizeofSig(UU())
890+
when defined(debug):
891+
type
892+
TT = pointerBase(T)
893+
UU = pointerBase(U)
894+
static:
895+
doAssert sizeof(TT) == sizeof(UU)
896+
doAssert getSizeofSig(TT()) == getSizeofSig(UU())
895897
cast[T](x)
896898
else:
897-
static:
898-
doAssert getSizeofSig(T()) == getSizeofSig(U())
899-
doAssert sizeof(T) == sizeof(U)
899+
when defined(debug): # 10s+ compile time due to `default(T)` and `replace`!
900+
static:
901+
doAssert sizeof(T) == sizeof(U)
902+
doAssert getSizeofSig(T()) == getSizeofSig(U())
900903
cast[ptr T](unsafeAddr x)[]
901904

902905
func prune*(cache: var StateCache, epoch: Epoch) =

beacon_chain/spec/state_transition.nim

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@
4343
import
4444
chronicles,
4545
results,
46+
stew/objects,
4647
../extras,
47-
"."/[
48-
beaconstate, eth2_merkleization, forks, helpers, signatures,
49-
state_transition_block, state_transition_epoch, validator]
48+
./[
49+
beaconstate, eth2_merkleization, forks, helpers, signatures, state_transition_block,
50+
state_transition_epoch, validator,
51+
]
5052

5153
export results, extras, state_transition_block
5254

@@ -377,7 +379,7 @@ proc makeBeaconBlockWithRewards*(
377379
## the block is to be created.
378380
type
379381
MaybeBlindedBeaconBlock = consensusFork.BeaconBlock(type(execution_payload))
380-
MaybeBlindedBlockBody = typeof(default(MaybeBlindedBeaconBlock).body)
382+
MaybeBlindedBlockBody = typeof(declval(MaybeBlindedBeaconBlock).body)
381383

382384
# https://github.com/ethereum/consensus-specs/blob/v1.5.0-beta.2/specs/phase0/validator.md#preparing-for-a-beaconblock
383385
var blck = MaybeBlindedBeaconBlock(

0 commit comments

Comments
 (0)