Skip to content

Commit 726e2ca

Browse files
committed
optimize for normal usage
1 parent 3b83f45 commit 726e2ca

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

threading/smartptrs.nim

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010
import std/isolation, atomics
1111
from typetraits import supportsCopyMem
1212

13-
template checkNotNil(p, msg: typed) =
13+
proc raiseNilAccess() {.noinline.} =
14+
raise newException(NilAccessDefect, "dereferencing nil smart pointer")
15+
16+
template checkNotNil(p: typed) =
1417
when compileOption("boundChecks"):
1518
{.line.}:
1619
if p.isNil:
17-
raise newException(NilAccessDefect, msg)
20+
raiseNilAccess()
1821

1922
type
2023
UniquePtr*[T] = object
@@ -55,11 +58,11 @@ proc isNil*[T](p: UniquePtr[T]): bool {.inline.} =
5558

5659
proc `[]`*[T](p: UniquePtr[T]): var T {.inline.} =
5760
## Returns a mutable view of the internal value of `p`.
58-
checkNotNil(p, "dereferencing nil unique pointer")
61+
checkNotNil(p)
5962
p.val[]
6063

6164
proc `[]=`*[T](p: UniquePtr[T], val: sink Isolated[T]) {.inline.} =
62-
checkNotNil(p, "dereferencing nil unique pointer")
65+
checkNotNil(p)
6366
p.val[] = extract val
6467

6568
template `[]=`*[T](p: UniquePtr[T]; val: T) =
@@ -114,11 +117,11 @@ proc isNil*[T](p: SharedPtr[T]): bool {.inline.} =
114117
p.val == nil
115118

116119
proc `[]`*[T](p: SharedPtr[T]): var T {.inline.} =
117-
checkNotNil(p, "dereferencing nil shared pointer")
120+
checkNotNil(p)
118121
p.val.value
119122

120123
proc `[]=`*[T](p: SharedPtr[T], val: sink Isolated[T]) {.inline.} =
121-
checkNotNil(p, "dereferencing nil shared pointer")
124+
checkNotNil(p)
122125
p.val.value = extract val
123126

124127
template `[]=`*[T](p: SharedPtr[T]; val: T) =
@@ -146,7 +149,7 @@ proc isNil*[T](p: ConstPtr[T]): bool {.inline.} =
146149

147150
proc `[]`*[T](p: ConstPtr[T]): lent T {.inline.} =
148151
## Returns an immutable view of the internal value of `p`.
149-
checkNotNil(p, "dereferencing nil const pointer")
152+
checkNotNil(p)
150153
SharedPtr[T](p).val.value
151154

152155
proc `[]=`*[T](p: ConstPtr[T], v: T) = {.error: "`ConstPtr` cannot be assigned.".}

0 commit comments

Comments
 (0)