go: cmd/compile: for-loops cannot be inlined

Using go devel +ed4a27a

Currently, functions with for-loops are not inline-able, would be nice if they did. Not long ago, I used a non-idiomatic goto in order to cheat around the inliner.

@dr2chase

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 16
  • Comments: 24 (15 by maintainers)

Commits related to this issue

Most upvoted comments

Yes, thanks for pointing out. This is done now for 1.18. The missing cases (range loops and loops with labeled breaks/continues) are now inlineable, so all for loops are now inlineable.

Change https://golang.org/cl/256459 mentions this issue: cmd/compile: allow inlining of "for" loops

Turns out that inlining for loops tickles (at least) two other bugs, don’t start the party yet.

I’m not sure if this is deliberate or an oversight.

A little of both. Some range loops desugar into non-trivial code (e.g., ranging over maps), so I punted on this because I expected it would be too complex… but in retrospect, all of that complexity happens after inlining anyway, so it would probably be pretty trivial actually.

We should be able to easily address this for next release, along with labeled for loops.

I will give it a look, I have a “adjust inlining parameters” CL up for review and I can try adding this one top see if it helps or hurts.

Change https://golang.org/cl/355497 mentions this issue: cmd/compile: allow inlining labeled for-statement and switch-statement

Any chance for this being fixed sometime soon? I would love to undo my re-writes where I wrote my loops using goto instead of for, so they can be inlined.

For reference, the easy part (inlining for loops with no range and no continue/break) inlines 924 additional calls in go install std and gives mixed results on the go1 benchmarks

name                     old time/op    new time/op    delta
BinaryTree17-4              3.00s ± 3%     2.96s ± 4%     ~             (p=0.548 n=5+5)
Fannkuch11-4                2.29s ± 0%     2.29s ± 0%     ~             (p=0.056 n=5+5)
FmtFprintfEmpty-4          50.4ns ± 0%    50.7ns ± 2%     ~             (p=0.556 n=4+5)
FmtFprintfString-4          134ns ± 0%     134ns ± 0%     ~     (all samples are equal)
FmtFprintfInt-4             133ns ± 0%     133ns ± 0%     ~     (all samples are equal)
FmtFprintfIntInt-4          201ns ± 0%     198ns ± 0%     ~             (p=0.079 n=4+5)
FmtFprintfPrefixedInt-4     201ns ± 0%     197ns ± 0%   -1.79%          (p=0.000 n=5+4)
FmtFprintfFloat-4           291ns ± 2%     284ns ± 1%   -2.20%          (p=0.040 n=5+5)
FmtManyArgs-4               845ns ± 0%     841ns ± 1%     ~             (p=0.056 n=5+5)
GobDecode-4                7.81ms ± 1%    7.90ms ± 1%   +1.22%          (p=0.008 n=5+5)
GobEncode-4                6.55ms ± 1%    6.27ms ± 0%   -4.24%          (p=0.008 n=5+5)
Gzip-4                      268ms ± 0%     269ms ± 1%     ~             (p=0.690 n=5+5)
Gunzip-4                   36.8ms ± 0%    36.6ms ± 0%   -0.60%          (p=0.008 n=5+5)
HTTPClientServer-4         58.5µs ± 1%    61.8µs ± 0%   +5.65%          (p=0.008 n=5+5)
JSONEncode-4               17.2ms ± 1%    16.9ms ± 2%   -1.77%          (p=0.016 n=5+5)
JSONDecode-4               51.5ms ± 0%    52.1ms ± 1%   +1.16%          (p=0.008 n=5+5)
Mandelbrot200-4            4.77ms ± 0%    4.74ms ± 1%     ~             (p=0.151 n=5+5)
GoParse-4                  3.58ms ± 0%    3.54ms ± 1%   -1.10%          (p=0.016 n=5+5)
RegexpMatchEasy0_32-4      81.5ns ± 0%    82.1ns ± 0%   +0.74%          (p=0.016 n=4+5)
RegexpMatchEasy0_1K-4       199ns ± 2%     196ns ± 0%   -1.51%          (p=0.048 n=5+5)
RegexpMatchEasy1_32-4      81.2ns ± 1%    82.9ns ± 0%   +2.04%          (p=0.016 n=5+4)
RegexpMatchEasy1_1K-4       360ns ± 2%     356ns ± 1%     ~             (p=0.167 n=5+5)
RegexpMatchMedium_32-4      125ns ± 1%     124ns ± 0%     ~             (p=0.167 n=5+5)
RegexpMatchMedium_1K-4     38.8µs ± 1%    37.9µs ± 1%   -2.54%          (p=0.008 n=5+5)
RegexpMatchHard_32-4       1.98µs ± 0%    1.95µs ± 0%   -1.73%          (p=0.000 n=5+4)
RegexpMatchHard_1K-4       59.7µs ± 0%    58.6µs ± 0%   -1.85%          (p=0.016 n=4+5)
Revcomp-4                   437ms ± 0%     448ms ± 1%   +2.65%          (p=0.008 n=5+5)
Template-4                 62.8ms ± 0%    62.8ms ± 0%     ~             (p=0.841 n=5+5)
TimeParse-4                 328ns ± 0%     295ns ± 0%  -10.17%          (p=0.000 n=5+4)
TimeFormat-4                357ns ± 1%     349ns ± 3%     ~             (p=0.111 n=5+5)