4 ms·
There is actually a major problem with long functions - they take a long time to compile, due to superlinear complexity in computation time as a function of fun
by Ono-Sendai 2y ago
There is actually a major problem with long functions - they take a long time to compile, due to superlinear complexity in computation time as a function of function length. In other words breaking up a large function into smaller function can greatly reduce compile times.
- badmintonbaseba 2y agoThat honestly feels like a minor problem, and not something to optimize for. Also an aggressively inlining compiler will experience exactly the same problem. AFAIK at least clang always inlines a static (as in internal linkage) function if it's used only once in the translation unit, no matter how large it is.
- Ono-Sendai 2y agoVisual studio doesn't do that inlining. And it is a significant problem, I have had to refactor my code into multiple functions because of it.
- badmintonbaseba 2y agoIt might be a significant problem, but not in the code, but the compiler. Fair enough, you are working around a compiler issue.
- Ono-Sendai 2y agoIf you consider any superlinear complexity a 'compiler issue' I guess.
- badmintonbaseba 2y agoIt absolutely is, if it makes compile times unreasonable for reasonable code. Compilers have to make trade-offs like this all the time, they can't use overly excessive optimizations.
- Ono-Sendai 2y agoI dunno. O(n^2) is for sure a bug. But O(nlogn) I think is reasonable.
- badmintonbaseba 2y agoO(nlogn) is probably reasonable. Why break up a long function then if you are experiencing O(nlogn) scaling of compile time on function size?
- Ono-Sendai 2y agoBecause it can still result in compile times I find excessive. For example breaking up a function that takes 5 seconds to compile into a bunch of functions that take 1 to 2 seconds in total.
- tightbookkeeper 2y agoIf you are willing to make code worse to micro optimize compile times (not even sure this is true) then you should not use any modern language with complex type checking (rust, swift, C#, etc).
- Ono-Sendai 2y agoWriting a medium to large program in C++, you really need to fight long compile times or they can get out of hand. That affects the way you write code quite a lot, or it should at least. I've heard Rust and Swift also suffer from long compile times.
- tightbookkeeper 2y agoAgreed. But For C++ template combinatorics are going to dominate any slow down due to function length.