make Eth2Digest isZero 8x faster (#6682)

* make Eth2Digest isZero 8x faster

* use staticFor and copyMem

* branchless with or
This commit is contained in:
tersec 2024-10-28 05:21:18 +00:00 committed by GitHub
parent 58a34e00a1
commit 4565c029e1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 8 additions and 1 deletions

View File

@ -32,6 +32,7 @@ import
json_serialization json_serialization
from nimcrypto/utils import burnMem from nimcrypto/utils import burnMem
from stew/staticfor import staticFor
export export
# Exports from sha2 / hash are explicit to avoid exporting upper-case `$` and # Exports from sha2 / hash are explicit to avoid exporting upper-case `$` and
@ -143,7 +144,13 @@ func `==`*(a, b: Eth2Digest): bool =
equalMem(unsafeAddr a.data[0], unsafeAddr b.data[0], sizeof(a.data)) equalMem(unsafeAddr a.data[0], unsafeAddr b.data[0], sizeof(a.data))
func isZero*(x: Eth2Digest): bool = func isZero*(x: Eth2Digest): bool =
x.isZeroMemory var tmp {.noinit.}: uint64
var tmp2 = 0'u64
static: doAssert sizeof(x.data) mod sizeof(tmp) == 0
staticFor i, 0 ..< sizeof(x.data) div sizeof(tmp):
copyMem(addr tmp, addr x.data[i*sizeof(tmp)], sizeof(tmp))
tmp2 = tmp2 or tmp
tmp2 == 0
proc writeValue*(w: var JsonWriter, a: Eth2Digest) {.raises: [IOError].} = proc writeValue*(w: var JsonWriter, a: Eth2Digest) {.raises: [IOError].} =
w.writeValue $a w.writeValue $a