Skip to content

Commit

Permalink
work around pointless copy in isZeroMemory due to nim-lang/Nim#24093 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tersec authored Sep 26, 2024
1 parent 41f48ef commit 1c9190a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
32 changes: 20 additions & 12 deletions stew/objects.nim
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
15 changes: 11 additions & 4 deletions stew/shims/sets.nim
Original file line number Diff line number Diff line change
@@ -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.

{.push raises: [].}

when not declared(initHashSet):
template initHashSet*[T](initialSize = 64): auto =
initSet[T](initialSize)
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)
Expand Down

0 comments on commit 1c9190a

Please sign in to comment.