avoid func call when merkleizing UintN arrays

This gets rid of an unnecessary function call when merkleizing `UintN`
arrays on `littleEndian` architectures.
This commit is contained in:
Etan Kissling 2021-10-20 16:26:28 +02:00 committed by zah
parent 052bec1c1f
commit 15ce2de3f0
1 changed files with 4 additions and 12 deletions

View File

@ -421,13 +421,15 @@ func chunkedHashTreeRootForBasicTypes[T](merkleizer: var SszMerkleizerImpl,
arr: openArray[T]): Eth2Digest =
static:
doAssert T is BasicType
doAssert bytesPerChunk mod sizeof(T) == 0
if arr.len == 0:
return getFinalHash(merkleizer)
when T is byte:
when sizeof(T) == 1 or cpuEndian == littleEndian:
var
remainingBytes = arr.len
remainingBytes = when sizeof(T) == 1: arr.len
else: arr.len * sizeof(T)
pos = cast[ptr byte](unsafeAddr arr[0])
while remainingBytes >= bytesPerChunk:
@ -438,17 +440,7 @@ func chunkedHashTreeRootForBasicTypes[T](merkleizer: var SszMerkleizerImpl,
if remainingBytes > 0:
merkleizer.addChunk(makeOpenArray(pos, remainingBytes))
elif T is bool or cpuEndian == littleEndian:
let
baseAddr = cast[ptr byte](unsafeAddr arr[0])
len = arr.len * sizeof(T)
return chunkedHashTreeRootForBasicTypes(merkleizer, makeOpenArray(baseAddr, len))
else:
static:
doAssert T is UintN
doAssert bytesPerChunk mod sizeof(T) == 0
const valuesPerChunk = bytesPerChunk div sizeof(T)
var writtenValues = 0