avoid func call when merkleizing UintN arrays (#413)

This gets rid of an unnecessary function call when merkleizing `UintN`
arrays on `littleEndian` architectures.
This commit is contained in:
Etan Kissling 2021-10-21 10:40:40 +02:00 committed by GitHub
parent 33f548186d
commit d34d3409da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 12 deletions

View File

@ -443,13 +443,15 @@ func chunkedHashTreeRootForBasicTypes[T](merkleizer: var SszMerkleizerImpl,
arr: openArray[T]): Digest = arr: openArray[T]): Digest =
static: static:
doAssert T is BasicType doAssert T is BasicType
doAssert bytesPerChunk mod sizeof(T) == 0
if arr.len == 0: if arr.len == 0:
return getFinalHash(merkleizer) return getFinalHash(merkleizer)
when T is byte: when sizeof(T) == 1 or cpuEndian == littleEndian:
var var
remainingBytes = arr.len remainingBytes = when sizeof(T) == 1: arr.len
else: arr.len * sizeof(T)
pos = cast[ptr byte](unsafeAddr arr[0]) pos = cast[ptr byte](unsafeAddr arr[0])
while remainingBytes >= bytesPerChunk: while remainingBytes >= bytesPerChunk:
@ -460,17 +462,7 @@ func chunkedHashTreeRootForBasicTypes[T](merkleizer: var SszMerkleizerImpl,
if remainingBytes > 0: if remainingBytes > 0:
merkleizer.addChunk(makeOpenArray(pos, remainingBytes)) 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: else:
static:
doAssert T is UintN
doAssert bytesPerChunk mod sizeof(T) == 0
const valuesPerChunk = bytesPerChunk div sizeof(T) const valuesPerChunk = bytesPerChunk div sizeof(T)
var writtenValues = 0 var writtenValues = 0