From 1c9190a632a419dc4588e06175cce22b50741069 Mon Sep 17 00:00:00 2001 From: tersec Date: Thu, 26 Sep 2024 15:49:08 +0000 Subject: [PATCH] work around pointless copy in isZeroMemory due to https://github.com/nim-lang/Nim/issues/24093 (#233) --- stew/objects.nim | 32 ++++++++++++++++++++------------ stew/shims/sets.nim | 15 +++++++++++---- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/stew/objects.nim b/stew/objects.nim index 0777249..58665fa 100644 --- a/stew/objects.nim +++ b/stew/objects.nim @@ -1,6 +1,16 @@ +# stew +# Copyright 2018-2024 Status Research & Development GmbH +# Licensed under either of +# +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) +# * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) +# +# at your option. This file may not be copied, modified, or distributed except according to those terms. + +{.push raises: [].} + import - macros, - sequtils + std/[macros, sequtils] template init*(lvalue: var auto) = mixin init @@ -18,10 +28,7 @@ template init*(lvalue: var auto, a1, a2, a3: auto) = mixin init lvalue = init(type(lvalue), a1, a2, a3) -when not declared(default): - proc default*(T: type): T = discard - -proc toArray*[T](N: static int, data: openArray[T]): array[N, T] = +func toArray*[T](N: static int, data: openArray[T]): array[N, T] = doAssert data.len == N copyMem(addr result[0], unsafeAddr data[0], N) @@ -86,7 +93,7 @@ macro hasHoles*(T: type[enum]): bool = quote: `T`.high.ord - `T`.low.ord != `len` -proc contains*[I: SomeInteger](e: type[enum], v: I): bool = +func contains*[I: SomeInteger](e: type[enum], v: I): bool = when I is uint64: if v > int.high.uint64: return false @@ -105,14 +112,15 @@ func checkedEnumAssign*[E: enum, I: SomeInteger](res: var E, value: I): bool = return false res = cast[E](value) - return true + true func isZeroMemory*[T](x: T): bool = # TODO: iterate over words here - for b in cast[ptr array[sizeof(T), byte]](unsafeAddr x)[]: - if b != 0: - return false - return true + # bufPtr avoids pointless https://github.com/nim-lang/Nim/issues/24093 copy + let bufPtr = cast[ptr array[sizeof(T), byte]](unsafeAddr x) + for b in bufPtr[]: + if b != 0: return false + true func isDefaultValue*[T](x: T): bool = # TODO: There are ways to optimise this for simple POD types diff --git a/stew/shims/sets.nim b/stew/shims/sets.nim index a575129..7d849c7 100644 --- a/stew/shims/sets.nim +++ b/stew/shims/sets.nim @@ -1,8 +1,15 @@ -import std/sets, ../objects, ../templateutils +# stew +# Copyright 2019-2024 Status Research & Development GmbH +# Licensed under either of +# +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) +# * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) +# +# at your option. This file may not be copied, modified, or distributed except according to those terms. -when not declared(initHashSet): - template initHashSet*[T](initialSize = 64): auto = - initSet[T](initialSize) +{.push raises: [].} + +import std/sets, ../objects, ../templateutils template init*[T](_: type HashSet[T]): auto = initHashSet[T]() template init*[T](_: type HashSet[T], defaultSize: int): auto = initHashSet[T](defaultSize)