[backport] arc: improve compile time of (nested) loops (#18890)

This commit is contained in:
Clyybber 2021-09-24 14:59:48 +02:00 • committed by GitHub
commit f7d642f2f3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -308,6 +308,11 @@ when true:
# We unroll every loop 3 times. We emulate 0, 1, 2 iterations # We unroll every loop 3 times. We emulate 0, 1, 2 iterations
# through the loop. We need to prove this is correct for our # through the loop. We need to prove this is correct for our
# purposes. But Herb Sutter claims it is. (Proof by authority.) # purposes. But Herb Sutter claims it is. (Proof by authority.)
#
# EDIT: Actually, we only need to unroll 2 times
# because Nim doesn't have a way of breaking/goto-ing into
# a loop iteration. Unrolling 2 times is much better for compile
# times of nested loops than 3 times, so we do that here.
#[ #[
while cond: while cond:
body body
@ -347,12 +352,12 @@ when true:
# 'while true' is an idiom in Nim and so we produce # 'while true' is an idiom in Nim and so we produce
# better code for it: # better code for it:
withBlock(nil): withBlock(nil):
for i in 0..2: for i in 0..1:
c.gen(n[1]) c.gen(n[1])
else: else:
withBlock(nil): withBlock(nil):
var endings: array[3, TPosition] var endings: array[2, TPosition]
for i in 0..2: for i in 0..1:
c.gen(n[0]) c.gen(n[0])
endings[i] = c.forkI(n) endings[i] = c.forkI(n)
c.gen(n[1]) c.gen(n[1])