diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2016-08-08 20:10:14 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-08-08 20:10:14 +0200 |
commit | e3b4a0c3a28f73fd8ad21456c083396974be3833 (patch) | |
tree | 94d63cd25c318dce230a00493d99aa0cc726f49a | |
parent | 8d78c2391586405bb23c4dd4366bfbec9b4fb3f7 (diff) | |
parent | 8d86c6ed8476756d07651707ef030a58b48214e6 (diff) | |
download | Nim-e3b4a0c3a28f73fd8ad21456c083396974be3833.tar.gz |
Merge pull request #4585 from yglukhov/array-ref-swap-optimization
Optimized swapping of ref array elements.
-rw-r--r-- | lib/system.nim | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/lib/system.nim b/lib/system.nim index 66daf3be1..23f9227c3 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -1744,6 +1744,13 @@ proc swap*[T](a, b: var T) {.magic: "Swap", noSideEffect.} ## swaps the values `a` and `b`. This is often more efficient than ## ``tmp = a; a = b; b = tmp``. Particularly useful for sorting algorithms. +when not defined(js) and not defined(booting): + template swapRefsInArray*{swap(arr[a], arr[b])}(arr: openarray[ref], a, b: int) = + # Optimize swapping of array elements if they are refs. Default swap + # implementation will cause unsureAsgnRef to be emitted which causes + # unnecessary slow down in this case. + swap(cast[ptr pointer](addr arr[a])[], cast[ptr pointer](addr arr[b])[]) + template `>=%` *(x, y: untyped): untyped = y <=% x ## treats `x` and `y` as unsigned and compares them. ## Returns true iff ``unsigned(x) >= unsigned(y)``. |