Skip to content

Commit

Permalink
fixes #22883; replace default(typeof( with reset; suppress `Unsaf… (
Browse files Browse the repository at this point in the history
#22895)

fixes #22883

…eDefault` warnings

avoid issues mentioned by https://forum.nim-lang.org namely, it
allocated unnecessary stack objects in the loop

```c
while (1)
{
tyObject_N__8DSNqSGSHBKOhI8CqSgAow T5_;
nimZeroMem((void *)(&T5_), sizeof(tyObject_N__8DSNqSGSHBKOhI8CqSgAow));
eqsink___test4954_u450((&(*t_p0).data.p->data[i].Field1), T5_);
}
```

It might be more efficient in some cases

follow up #21821

(cherry picked from commit 92141e8)
  • Loading branch information
ringabout authored and narimiran committed Apr 18, 2024
1 parent 5e20e93 commit bd829c0
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/pure/collections/setimpl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ proc exclImpl[A](s: var HashSet[A], key: A): bool {.inline.} =
var j = i # The correctness of this depends on (h+1) in nextTry,
var r = j # though may be adaptable to other simple sequences.
s.data[i].hcode = 0 # mark current EMPTY
s.data[i].key = default(typeof(s.data[i].key))
reset(s.data[i].key)
doWhile((i >= r and r > j) or (r > j and j > i) or (j > i and i >= r)):
i = (i + 1) and msk # increment mod table size
if isEmpty(s.data[i].hcode): # end of collision cluster; So all done
Expand Down
4 changes: 2 additions & 2 deletions lib/pure/collections/sets.nim
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ proc clear*[A](s: var HashSet[A]) =
s.counter = 0
for i in 0 ..< s.data.len:
s.data[i].hcode = 0
s.data[i].key = default(typeof(s.data[i].key))
reset(s.data[i].key)


proc union*[A](s1, s2: HashSet[A]): HashSet[A] =
Expand Down Expand Up @@ -813,7 +813,7 @@ proc clear*[A](s: var OrderedSet[A]) =
for i in 0 ..< s.data.len:
s.data[i].hcode = 0
s.data[i].next = 0
s.data[i].key = default(typeof(s.data[i].key))
reset(s.data[i].key)

proc len*[A](s: OrderedSet[A]): int {.inline.} =
## Returns the number of elements in `s`.
Expand Down
8 changes: 4 additions & 4 deletions lib/pure/collections/tableimpl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ template delImplIdx(t, i, makeEmpty, cellEmpty, cellHash) =
var j = i # The correctness of this depends on (h+1) in nextTry
var r = j # though may be adaptable to other simple sequences.
makeEmpty(i) # mark current EMPTY
t.data[i].key = default(typeof(t.data[i].key))
t.data[i].val = default(typeof(t.data[i].val))
reset(t.data[i].key)
reset(t.data[i].val)
while true:
i = (i + 1) and msk # increment mod table size
if cellEmpty(i): # end of collision cluster; So all done
Expand Down Expand Up @@ -151,8 +151,8 @@ template clearImpl() {.dirty.} =
for i in 0 ..< t.dataLen:
when compiles(t.data[i].hcode): # CountTable records don't contain a hcode
t.data[i].hcode = 0
t.data[i].key = default(typeof(t.data[i].key))
t.data[i].val = default(typeof(t.data[i].val))
reset(t.data[i].key)
reset(t.data[i].val)
t.counter = 0

template ctAnd(a, b): bool =
Expand Down
4 changes: 4 additions & 0 deletions lib/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -931,14 +931,18 @@ proc default*[T](_: typedesc[T]): T {.magic: "Default", noSideEffect.} =
proc reset*[T](obj: var T) {.noSideEffect.} =
## Resets an object `obj` to its default value.
when nimvm:
{.push warning[UnsafeDefault]:off.}
obj = default(typeof(obj))
{.pop.}
else:
when defined(gcDestructors):
{.cast(noSideEffect), cast(raises: []), cast(tags: []).}:
`=destroy`(obj)
`=wasMoved`(obj)
else:
{.push warning[UnsafeDefault]:off.}
obj = default(typeof(obj))
{.pop.}

proc setLen*[T](s: var seq[T], newlen: Natural) {.
magic: "SetLengthSeq", noSideEffect, nodestroy.}
Expand Down

0 comments on commit bd829c0

Please sign in to comment.