
How refactoring a macro reduced my Rust project build time from 4 hours to 40sec - iceiix
https://users.rust-lang.org/t/5-hours-to-compile-macro-what-can-i-do/36508
======
garganzol
This is clearly an O(n^2) situation somewhere in LLVM. Usually it is pretty
easy to fix by adding a cache or two; or by changing a linear search to a hash
map.

If OP could produce an LLVM reproduction sample then this would be a good help
for LLVM team.

------
adrianN
Does anyone know what LLVM does that breaks for long functions? Is the
optimization pass doing something quadratic in the number of basic blocks in
the function?

~~~
garganzol
This is an O(n^2) in processing of instructions, blocks or anything related.
Optimizations are specifically prone to this.

------
cespare
I hope this is considered a compiler bug.

~~~
lilyball
Compiling a single function containing over 100,000 lines is a rather
pathological case. And while you could argue it's a compiler bug, it's
honestly not one I'd expect anyone to be spending any time on.

~~~
C1sc0cat
A single function is >100k damm! I though a 1300-1440 line Fortran 77
subroutine was over the top back in the day

~~~
jsjohnst
I’m not sure if this is still the case, but Akamai previously had over 18,000
lines in the _main_ C function in the heavily modified version of Squid they
used for their CDN.

------
MBlume
This is generally good practice. Put runtime logic in functions, then write
macros with just enough syntax sugar to bridge the gap to those functions.

------
lopsidedBrain
TL;DR: Don't autogenerate a large monolithic function. Generate many small
functions instead.

------
Thorrez
It sounds like this is an LLVM problem. So could this same thing happen in C?

~~~
flohofwoe
I think I'm seeing this in C code too with clang. I'm having fairly big code-
generated switch-case statements in my CPU emulators with hundreds to
thousands of case-blocks (Z80: [1], 6502: [2]), and those take up to 7 seconds
to compile with -O3, which is really unusually long for a single C compilation
unit of that size (compared to the same number of lines across many smaller
functions). If the problem has exponential behaviour it could easily blow up
to minutes or hours for larger functions.

[1]
[https://github.com/floooh/chips/blob/d0f690f2598f967f75cb360...](https://github.com/floooh/chips/blob/d0f690f2598f967f75cb360c5f8127791f88bdec/chips/z80.h#L866)

[2]
[https://github.com/floooh/chips/blob/d0f690f2598f967f75cb360...](https://github.com/floooh/chips/blob/d0f690f2598f967f75cb360c5f8127791f88bdec/chips/m6502.h#L713)

