Skip to content

Commit 577ffbc

Browse files
ringaboutnarimiran
authored andcommitted
items, pairs and friends now use unCheckedInc (#22729)
`{.push overflowChecks: off.}` works in backends. Though it could be implemented as a magic function. By inspecting the generated C code, the overflow check is eliminated in the debug or release mode. ![image](https://github.com/nim-lang/Nim/assets/43030857/49c3dbf4-675e-414a-b972-b91cf218c9f8) Likewise, the index checking is probably not needed. (cherry picked from commit d82bc0a)
1 parent e918a76 commit 577ffbc

File tree

2 files changed

+35
-26
lines changed

2 files changed

+35
-26
lines changed

lib/system/iterators.nim

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@ when not defined(nimNoLentIterators):
88
else:
99
template lent2(T): untyped = T
1010

11+
template unCheckedInc(x) =
12+
{.push overflowChecks: off.}
13+
inc(x)
14+
{.pop.}
15+
1116
iterator items*[T: not char](a: openArray[T]): lent2 T {.inline.} =
1217
## Iterates over each item of `a`.
1318
var i = 0
1419
while i < len(a):
1520
yield a[i]
16-
inc(i)
21+
unCheckedInc(i)
1722

1823
iterator items*[T: char](a: openArray[T]): T {.inline.} =
1924
## Iterates over each item of `a`.
@@ -23,14 +28,14 @@ iterator items*[T: char](a: openArray[T]): T {.inline.} =
2328
var i = 0
2429
while i < len(a):
2530
yield a[i]
26-
inc(i)
31+
unCheckedInc(i)
2732

2833
iterator mitems*[T](a: var openArray[T]): var T {.inline.} =
2934
## Iterates over each item of `a` so that you can modify the yielded value.
3035
var i = 0
3136
while i < len(a):
3237
yield a[i]
33-
inc(i)
38+
unCheckedInc(i)
3439

3540
iterator items*[IX, T](a: array[IX, T]): T {.inline.} =
3641
## Iterates over each item of `a`.
@@ -39,7 +44,7 @@ iterator items*[IX, T](a: array[IX, T]): T {.inline.} =
3944
while true:
4045
yield a[i]
4146
if i >= high(IX): break
42-
inc(i)
47+
unCheckedInc(i)
4348

4449
iterator mitems*[IX, T](a: var array[IX, T]): var T {.inline.} =
4550
## Iterates over each item of `a` so that you can modify the yielded value.
@@ -48,7 +53,7 @@ iterator mitems*[IX, T](a: var array[IX, T]): var T {.inline.} =
4853
while true:
4954
yield a[i]
5055
if i >= high(IX): break
51-
inc(i)
56+
unCheckedInc(i)
5257

5358
iterator items*[T](a: set[T]): T {.inline.} =
5459
## Iterates over each element of `a`. `items` iterates only over the
@@ -57,7 +62,7 @@ iterator items*[T](a: set[T]): T {.inline.} =
5762
var i = low(T).int
5863
while i <= high(T).int:
5964
if T(i) in a: yield T(i)
60-
inc(i)
65+
unCheckedInc(i)
6166

6267
iterator items*(a: cstring): char {.inline.} =
6368
## Iterates over each item of `a`.
@@ -76,7 +81,7 @@ iterator items*(a: cstring): char {.inline.} =
7681
let n = len(a)
7782
while i < n:
7883
yield a[i]
79-
inc(i)
84+
unCheckedInc(i)
8085
when defined(js): impl()
8186
else:
8287
when nimvm:
@@ -86,7 +91,7 @@ iterator items*(a: cstring): char {.inline.} =
8691
var i = 0
8792
while a[i] != '\0':
8893
yield a[i]
89-
inc(i)
94+
unCheckedInc(i)
9095

9196
iterator mitems*(a: var cstring): var char {.inline.} =
9297
## Iterates over each item of `a` so that you can modify the yielded value.
@@ -109,15 +114,15 @@ iterator mitems*(a: var cstring): var char {.inline.} =
109114
let n = len(a)
110115
while i < n:
111116
yield a[i]
112-
inc(i)
117+
unCheckedInc(i)
113118
when defined(js): impl()
114119
else:
115120
when nimvm: impl()
116121
else:
117122
var i = 0
118123
while a[i] != '\0':
119124
yield a[i]
120-
inc(i)
125+
unCheckedInc(i)
121126

122127
iterator items*[T: enum and Ordinal](E: typedesc[T]): T =
123128
## Iterates over the values of `E`.
@@ -140,15 +145,15 @@ iterator pairs*[T](a: openArray[T]): tuple[key: int, val: T] {.inline.} =
140145
var i = 0
141146
while i < len(a):
142147
yield (i, a[i])
143-
inc(i)
148+
unCheckedInc(i)
144149

145150
iterator mpairs*[T](a: var openArray[T]): tuple[key: int, val: var T]{.inline.} =
146151
## Iterates over each item of `a`. Yields `(index, a[index])` pairs.
147152
## `a[index]` can be modified.
148153
var i = 0
149154
while i < len(a):
150155
yield (i, a[i])
151-
inc(i)
156+
unCheckedInc(i)
152157

153158
iterator pairs*[IX, T](a: array[IX, T]): tuple[key: IX, val: T] {.inline.} =
154159
## Iterates over each item of `a`. Yields `(index, a[index])` pairs.
@@ -157,7 +162,7 @@ iterator pairs*[IX, T](a: array[IX, T]): tuple[key: IX, val: T] {.inline.} =
157162
while true:
158163
yield (i, a[i])
159164
if i >= high(IX): break
160-
inc(i)
165+
unCheckedInc(i)
161166

162167
iterator mpairs*[IX, T](a: var array[IX, T]): tuple[key: IX, val: var T] {.inline.} =
163168
## Iterates over each item of `a`. Yields `(index, a[index])` pairs.
@@ -167,15 +172,15 @@ iterator mpairs*[IX, T](a: var array[IX, T]): tuple[key: IX, val: var T] {.inlin
167172
while true:
168173
yield (i, a[i])
169174
if i >= high(IX): break
170-
inc(i)
175+
unCheckedInc(i)
171176

172177
iterator pairs*[T](a: seq[T]): tuple[key: int, val: T] {.inline.} =
173178
## Iterates over each item of `a`. Yields `(index, a[index])` pairs.
174179
var i = 0
175180
let L = len(a)
176181
while i < L:
177182
yield (i, a[i])
178-
inc(i)
183+
unCheckedInc(i)
179184
assert(len(a) == L, "the length of the seq changed while iterating over it")
180185

181186
iterator mpairs*[T](a: var seq[T]): tuple[key: int, val: var T] {.inline.} =
@@ -185,7 +190,7 @@ iterator mpairs*[T](a: var seq[T]): tuple[key: int, val: var T] {.inline.} =
185190
let L = len(a)
186191
while i < L:
187192
yield (i, a[i])
188-
inc(i)
193+
unCheckedInc(i)
189194
assert(len(a) == L, "the length of the seq changed while iterating over it")
190195

191196
iterator pairs*(a: string): tuple[key: int, val: char] {.inline.} =
@@ -194,7 +199,7 @@ iterator pairs*(a: string): tuple[key: int, val: char] {.inline.} =
194199
let L = len(a)
195200
while i < L:
196201
yield (i, a[i])
197-
inc(i)
202+
unCheckedInc(i)
198203
assert(len(a) == L, "the length of the string changed while iterating over it")
199204

200205
iterator mpairs*(a: var string): tuple[key: int, val: var char] {.inline.} =
@@ -204,7 +209,7 @@ iterator mpairs*(a: var string): tuple[key: int, val: var char] {.inline.} =
204209
let L = len(a)
205210
while i < L:
206211
yield (i, a[i])
207-
inc(i)
212+
unCheckedInc(i)
208213
assert(len(a) == L, "the length of the string changed while iterating over it")
209214

210215
iterator pairs*(a: cstring): tuple[key: int, val: char] {.inline.} =
@@ -214,12 +219,12 @@ iterator pairs*(a: cstring): tuple[key: int, val: char] {.inline.} =
214219
var L = len(a)
215220
while i < L:
216221
yield (i, a[i])
217-
inc(i)
222+
unCheckedInc(i)
218223
else:
219224
var i = 0
220225
while a[i] != '\0':
221226
yield (i, a[i])
222-
inc(i)
227+
unCheckedInc(i)
223228

224229
iterator mpairs*(a: var cstring): tuple[key: int, val: var char] {.inline.} =
225230
## Iterates over each item of `a`. Yields `(index, a[index])` pairs.
@@ -229,20 +234,20 @@ iterator mpairs*(a: var cstring): tuple[key: int, val: var char] {.inline.} =
229234
var L = len(a)
230235
while i < L:
231236
yield (i, a[i])
232-
inc(i)
237+
unCheckedInc(i)
233238
else:
234239
var i = 0
235240
while a[i] != '\0':
236241
yield (i, a[i])
237-
inc(i)
242+
unCheckedInc(i)
238243

239244
iterator items*[T](a: seq[T]): lent2 T {.inline.} =
240245
## Iterates over each item of `a`.
241246
var i = 0
242247
let L = len(a)
243248
while i < L:
244249
yield a[i]
245-
inc(i)
250+
unCheckedInc(i)
246251
assert(len(a) == L, "the length of the seq changed while iterating over it")
247252

248253
iterator mitems*[T](a: var seq[T]): var T {.inline.} =
@@ -251,7 +256,7 @@ iterator mitems*[T](a: var seq[T]): var T {.inline.} =
251256
let L = len(a)
252257
while i < L:
253258
yield a[i]
254-
inc(i)
259+
unCheckedInc(i)
255260
assert(len(a) == L, "the length of the seq changed while iterating over it")
256261

257262
iterator items*(a: string): char {.inline.} =
@@ -260,7 +265,7 @@ iterator items*(a: string): char {.inline.} =
260265
let L = len(a)
261266
while i < L:
262267
yield a[i]
263-
inc(i)
268+
unCheckedInc(i)
264269
assert(len(a) == L, "the length of the string changed while iterating over it")
265270

266271
iterator mitems*(a: var string): var char {.inline.} =
@@ -269,7 +274,7 @@ iterator mitems*(a: var string): var char {.inline.} =
269274
let L = len(a)
270275
while i < L:
271276
yield a[i]
272-
inc(i)
277+
unCheckedInc(i)
273278
assert(len(a) == L, "the length of the string changed while iterating over it")
274279

275280

tests/arc/topt_no_cursor.nim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ try:
9191
`=copy`(lan_ip, splitted[1])
9292
echo [lan_ip]
9393
echo [splitted[1]]
94+
{.push, overflowChecks: false.}
9495
inc(i, 1)
96+
{.pop.}
9597
finally:
9698
`=destroy`(splitted)
9799
finally:
@@ -113,7 +115,9 @@ block :tmp:
113115
addInterfaceDecl(c):
114116
:tmpD = `=dup`(sym)
115117
:tmpD
118+
{.push, overflowChecks: false.}
116119
inc(i, 1)
120+
{.pop.}
117121
`=destroy`(shadowScope)
118122
-- end of expandArc ------------------------
119123
--expandArc: check

0 commit comments

Comments
 (0)