Most active commenters
  • benreesman(5)
  • pjmlp(5)
  • ChadNauseam(3)
  • duped(3)
  • estebank(3)

←back to thread

302 points Bogdanp | 63 comments | | HN request time: 1.588s | source | bottom
Show context
taylorallred ◴[] No.44390996[source]
So there's this guy you may have heard of called Ryan Fleury who makes the RAD debugger for Epic. The whole thing is made with 278k lines of C and is built as a unity build (all the code is included into one file that is compiled as a single translation unit). On a decent windows machine it takes 1.5 seconds to do a clean compile. This seems like a clear case-study that compilation can be incredibly fast and makes me wonder why other languages like Rust and Swift can't just do something similar to achieve similar speeds.
replies(18): >>44391046 #>>44391066 #>>44391100 #>>44391170 #>>44391214 #>>44391359 #>>44391671 #>>44391740 #>>44393057 #>>44393294 #>>44393629 #>>44394710 #>>44395044 #>>44395135 #>>44395226 #>>44395485 #>>44396044 #>>44401496 #
1. lordofgibbons ◴[] No.44391100[source]
The more your compiler does for you at build time, the longer it will take to build, it's that simple.

Go has sub-second build times even on massive code-bases. Why? because it doesn't do a lot at build time. It has a simple module system, (relatively) simple type system, and leaves a whole bunch of stuff be handled by the GC at runtime. It's great for its intended use case.

When you have things like macros, advanced type systems, and want robustness guarantees at build time.. then you have to pay for that.

replies(9): >>44391549 #>>44391582 #>>44391630 #>>44391910 #>>44394240 #>>44395833 #>>44397304 #>>44401934 #>>44402705 #
2. ChadNauseam ◴[] No.44391549[source]
That the type system is responsible for rust's slow builds is a common and enduring myth. `cargo check` (which just does typechecking) is actually usually pretty fast. Most of the build time is spent in the code generation phase. Some macros do cause problems as you mention, since the code that contains the macro must be compiled before the code that uses it, so they reduce parallelism.
replies(3): >>44391716 #>>44392132 #>>44397412 #
3. duped ◴[] No.44391582[source]
I think this is mostly a myth. If you look at Rust compiler benchmarks, while typechecking isn't _free_ it's also not the bottleneck.

A big reason that amalgamation builds of C and C++ can absolutely fly is because they aren't reparsing headers and generating exactly one object file so the linker has no work to do.

Once you add static linking to the toolchain (in all of its forms) things get really fucking slow.

Codegen is also a problem. Rust tends to generate a lot more code than C or C++, so while the compiler is done doing most of its typechecking work, the backend and assembler has a lot of things to chuck through.

replies(6): >>44392553 #>>44392826 #>>44394891 #>>44396127 #>>44396258 #>>44396355 #
4. cogman10 ◴[] No.44391630[source]
Yes but I'd also add that Go specifically does not optimize well.

The compiler is optimized for compilation speed, not runtime performance. Generally speaking, it does well enough. Especially because it's usecase is often applications where "good enough" is good enough (IE, IO heavy applications).

You can see that with "gccgo". Slower to compile, faster to run.

replies(2): >>44392046 #>>44399907 #
5. rstuart4133 ◴[] No.44391716[source]
> Most of the build time is spent in the code generation phase.

I can believe that, but even so it's caused by the type system monomorphising everything. When it use qsort from libc, you are using per-compiled code from a library. When you use slice::sort(), you get custom assembler compiled to suit your application. Thus, there is a lot more code generation going on, and that is caused by the tradeoffs they've made with the type system.

Rusts approach give you all sorts of advantages, like fast code and strong compile time type checking. But it comes with warts too, like fat binaries, and a bug in slice::sort() can't be fixed by just shipping of the std dynamic library, because there is no such library. It's been recompiled, just for you.

FWIW, modern C++ (like boost) that places everything in templates in .h files suffers from the same problem. If Swift suffers from it too, I'd wager it's the same cause.

replies(1): >>44395255 #
6. Zardoz84 ◴[] No.44391910[source]
Dlang compilers does more than any C++ compiler (metaprogramming, a better template system and compile time execution) and it's hugely faster. Language syntax design has a role here.
7. cherryteastain ◴[] No.44392046[source]
Is gccgo really faster? Last time I looked it looked like it was abandoned (stuck at go 1.18, had no generics support) and was not really faster than the "actual" compiler.
replies(1): >>44393453 #
8. tedunangst ◴[] No.44392132[source]
I just ran cargo check on nushell, and it took a minute and a half. I didn't time how long it took to compile, maybe five minutes earlier today? So I would call it faster, but still not fast.

I was all excited to conduct the "cargo check; mrustc; cc" is 100x faster experiment, but I think at best, the multiple is going to be pretty small.

replies(2): >>44392588 #>>44395154 #
9. treyd ◴[] No.44392553[source]
Not only does it generate more code, the initially generated code before optimizations is also often worse. For example, heavy use of iterators means a ton of generics being instantiated and a ton of call code for setting up and tearing down call frames. This gets heavily inlined and flattened out, so in the end it's extremely well-optimized, but it's a lot of work for the compiler. Writing it all out classically with for loops and ifs is possible, but it's harder to read.
replies(1): >>44397082 #
10. ChadNauseam ◴[] No.44392588{3}[source]
Did you do it from a clean build? In that case, it's actually a slightly misleading metric, since rust needs to actually compile macros in order to typecheck code that uses them. (And therefore must also compile all the code that the macro depends on.) My bad for suggesting it, haha. Incremental cargo check is often a better way of seeing how long typechecking takes, since usually you haven't modified any macros that will need to be recompiled. On my project at work, incremental cargo check takes `1.71s`.
replies(2): >>44397210 #>>44402957 #
11. fingerlocks ◴[] No.44392826[source]
The swift compiler is definitely bottle necked by type checking. For example, as a language requirement, generic types are left more or less in-tact after compilation. They are type checked independent of what is happening. This is unlike C++ templates which are effectively copy-pasting the resolved type with the generic for every occurrence of type resolution.

This has tradeoffs: increased ABI stability at the cost of longer compile times.

replies(3): >>44393659 #>>44394911 #>>44397467 #
12. cogman10 ◴[] No.44393453{3}[source]
Digging around, looks like it's workload dependent.

For pure computational workloads, it'll be faster. However, anything with heavy allocation will suffer as apparently the gccgo GC and GC related optimizations aren't as good as cgo's.

replies(1): >>44402614 #
13. willtemperley ◴[] No.44393659{3}[source]
A lot can be done by the programmer to mitigate slow builds in Swift. Breaking up long expressions into smaller ones and using explicit types where type inference is expensive for example.

I’d like to see tooling for this to pinpoint bottlenecks - it’s not always obvious what’s making builds slow.

replies(2): >>44394713 #>>44396157 #
14. Mawr ◴[] No.44394240[source]
Not really. The root reason behind Go's fast compilation is that it was specifically designed to compile fast. The implementation details are just a natural consequence of that design decision.

Since fast compilation was a goal, every part of the design was looked at through a rough "can this be a horrible bottleneck?", and discarded if so. For example, the import (package) system was designed to avoid the horrible, inefficient mess of C++. It's obvious that you never want to compile the same package more than once and that you need to support parallel package compilation. These may be blindingly obvious, but if you don't think about compilation speed at design time, you'll get this wrong and will never be able to fix it.

As far as optimizations vs compile speed goes, it's just a simple case of diminishing returns. Since Rust has maximum possible perfomance as a goal, it's forced to go well into the diminishing returns territory, sacrificing a ton of compile speed for minor performance improvements. Go has far more modest performance goals, so it can get 80% of the possible performance for only 20% of the compile cost. Rust can't afford to relax its stance because it's competing with languages like C++, and to some extent C, that are willing to go to any length to squeeze out an extra 1% of perfomance.

replies(1): >>44402975 #
15. ykonstant ◴[] No.44394713{4}[source]
>I’d like to see tooling for this to pinpoint bottlenecks - it’s not always obvious what’s making builds slow.

I second this enthusiastically.

replies(1): >>44395588 #
16. windward ◴[] No.44394891[source]
>Codegen is also a problem. Rust tends to generate a lot more code than C or C++

Wouldn't you say a lot of that comes from the macros and (by way of monomorphisation) the type system?

replies(1): >>44397739 #
17. windward ◴[] No.44394911{3}[source]
>This is unlike C++ templates which are effectively copy-pasting the resolved type with the generic for every occurrence of type resolution.

Even this can lead to unworkable compile times, to the point that code is rewritten.

18. CryZe ◴[] No.44395154{3}[source]
A ton of that is actually still doing codegen (for the proc macros for example).
19. badmintonbaseba ◴[] No.44395255{3}[source]
It's partly by the type system. You can implement a std::sort (or slice::sort()) that just delegates to qsort or a qsort-like implementation and have roughly the same compile time performance as just using qsort straight.

But not having to is a win, as the monomorphised sorts are just much faster at runtime than having to do an indirect call for each comparison.

replies(2): >>44397138 #>>44401996 #
20. glhaynes ◴[] No.44395588{5}[source]
I'll third it. I've started to see more and more cargo culting of "fixes" that I'm extremely suspicious do nothing aside from making the code bulkier.
21. jstanley ◴[] No.44395833[source]
> Go has sub-second build times even on massive code-bases.

Unless you use sqlite, in which case your build takes a million years.

replies(2): >>44396492 #>>44399849 #
22. the-lazy-guy ◴[] No.44396127[source]
> Once you add static linking to the toolchain (in all of its forms) things get really fucking slow.

Could you expand on that, please? Every time you run dynmically linked program, it is linked at runtime. (unless it explicitly avoids linking unneccessary stuff by dlopening things lazily; which pretty much never happens). If it is fine to link on every program launch, linking at build time should not be a problem at all.

If you want to have link time optimization, that's another story. But you absolutely don't have to do that if you care about build speed.

replies(1): >>44403903 #
23. never_inline ◴[] No.44396157{4}[source]
> Breaking up long expressions into smaller ones

If it improves compile time, that sounds like a bug in the compiler or the design of the language itself.

24. blizdiddy ◴[] No.44396258[source]
Go is static by default and still fast as hell
replies(1): >>44396404 #
25. benreesman ◴[] No.44396355[source]
The meme that static linking is slow or produces anything other than the best executables is demonstrably false and the result of surprisingly sinister agendas. Get out readelf and nm and PS sometime and do the arithematic: most programs don't link much of glibc (and its static link is broken by design, musl is better at just about everything). Matt Godbolt has a great talk about how dynamic linking actually works that should give anyone pause.

DLLs got their start when early windowing systems didn't quite fit on the workstations of the era in the late 80s / early 90s.

In about 4 minutes both Microsoft and GNU were like, "let me get this straight, it will never work on another system and I can silently change it whenever I want?" Debian went along because it gives distro maintainers degrees of freedom they like and don't bear the costs of.

Fast forward 30 years and Docker is too profitable a problem to fix by the simple expedient of calling a stable kernel ABI on anything, and don't even get me started on how penetrated everything but libressl and libsodium are. Protip: TLS is popular with the establishment because even Wireshark requires special settings and privileges for a user to see their own traffic, security patches my ass. eBPF is easier.

Dynamic linking moves control from users to vendors and governments at ruinous cost in performance, props up bloated industries like the cloud compute and Docker industrial complex, and should die in a fire.

Don't take my word for it, swing by cat-v.org sometimes and see what the authors of Unix have to say about it.

I'll save the rant about how rustc somehow manages to be slower than clang++ and clang-tidy combined for another day.

replies(3): >>44396760 #>>44396875 #>>44396975 #
26. vintagedave ◴[] No.44396404{3}[source]
Delphi is static by default and incredibly fast too.
replies(2): >>44399822 #>>44402941 #
27. Groxx ◴[] No.44396492[source]
Yeah, I deal with multiple Go projects that take a couple minutes to link the final binary, much less build all the intermediates.

Compilation speed depends on what you do with a language. "Fast" is not an absolute, and for most people it depends heavily on community habits. Rust habits tend to favor extreme optimizability and/or extreme compile-time guarantees, and that's obviously going to be slower than simpler code.

28. jelder ◴[] No.44396760{3}[source]
CppCon 2018: Matt Godbolt “The Bits Between the Bits: How We Get to main()"

https://www.youtube.com/watch?v=dOfucXtyEsU

29. jrmg ◴[] No.44396875{3}[source]
…surprisingly sinister agendas.

Dynamic linking moves control from users to vendors and governments at ruinous cost in performance, props up bloated industries...

This is ridiculous. Not everything is a conspiracy!

replies(4): >>44396943 #>>44398118 #>>44399499 #>>44400236 #
30. k__ ◴[] No.44396943{4}[source]
That's an even more reasonable fear than trusting trust, and people seem to take that seriously.
31. duped ◴[] No.44396975{3}[source]
I think you're confused about my comment and this thread - I'm talking about build times.
replies(1): >>44398148 #
32. estebank ◴[] No.44397082{3}[source]
For loops are sugar around an Iterator instantiation:

  for i in 0..10 {}
translates to roughly

  let mut iter = Range { start: 0, end: 10 }.into_iter();
  while let Some(i) = iter.next() {}
33. estebank ◴[] No.44397138{4}[source]
This is a pattern a crate author can rely on (write a function that uses genetics that immediately delegates to a function that uses trait objects or converts to the needed types eagerly so the common logic gets compiled only once), and there have been multiple efforts to have the compiler do that automatically. It has been called polymorphization and it comes up every now and then: https://internals.rust-lang.org/t/add-back-polymorphization/...
34. estebank ◴[] No.44397210{4}[source]
Side note: There's an effort to cache proc macro invocations so that they get executed only once if the item they annotate hasn't changed: https://github.com/rust-lang/rust/pull/129102

There are multiple caveats on providing this to users (we can't assume that macro invocations are idempotent, so the new behavior would have to be opt in, and this only benefits incremental compilation), but it's in our radar.

35. phplovesong ◴[] No.44397304[source]
Thats not really true. As a counter example, Ocaml has a very advanced type system, full typeinference, generics and all that jazz. Still its on par, or even faster to compile than Go.
36. ◴[] No.44397412[source]
37. slavapestov ◴[] No.44397467{3}[source]
> This has tradeoffs: increased ABI stability at the cost of longer compile times.

Nah. Slow type checking in Swift is primarily caused by the fact that functions and operators can be overloaded on type.

Separately-compiled generics don't introduce any algorithmic complexity and are actually good for compile time, because you don't have to re-type check every template expansion more than once.

replies(2): >>44399893 #>>44399968 #
38. jandrewrogers ◴[] No.44397739{3}[source]
Modern C++ in particular does a lot of similar, albeit not identical, codegen due to its extensive metaprogramming facilities. (C is, of course, dead simple.) I've never looked into it too much but anecdotally Rust does seem to generate significantly more code than C++ in cases where I would intuitively expect the codegen to be similar. For whatever reason, the "in theory" doesn't translate to "in practice" reliably.

I suspect this leaks into both compile-time and run-time costs.

39. benreesman ◴[] No.44398118{4}[source]
I didn't say anything was a conspiracy, let alone everything. I said inferior software is promoted by vendors on Linux as well as on MacOS and Windows with unpleasant consequences for users in a way that serves those vendors and the even more powerful institutions to which they are beholden. Sinister intentions are everywhere in this business (go read the opinions of the people who run YC), that's not even remotely controversial.

If fact, if there was anything remotely controversial about a bunch of extremely specific, extremely falsifiable claims I made, one imagines your rebuttal would have mentioned at least one.

I said inflmatory things (Docker is both arsonist and fireman at ruinous cost), but they're fucking true. That Alpine in the Docker jank? Links musl!

40. benreesman ◴[] No.44398148{4}[source]
You said something false and important and I took the opportunity to educate anyone reading about why this aspect of their computing experience is a mess. All of that is germane to how we ended up in a situation where someone is calling rustc with a Dockerfile and this is considered normal.
replies(1): >>44400814 #
41. computably ◴[] No.44399499{4}[source]
Bad incentives != conspiracy
42. zenlot ◴[] No.44399822{4}[source]
FreePascal to the game please
replies(1): >>44401261 #
43. infogulch ◴[] No.44399849[source]
Try https://github.com/ncruces/go-sqlite3 it runs sqlite in WASM with wazero, a pure Go WASM runtime, so it builds without any CGo required. Most of the benchmarks are within a few % of the performance of mattn/go-sqlite3.
44. fingerlocks ◴[] No.44399893{4}[source]
You’re absolutely right. I realized this later but it was too late to edit the post.
45. pclmulqdq ◴[] No.44399907[source]
Go defaults to an unoptimized build. If you want it to run heavy optimization passes, you can turn those on with flags. Rust defaults to doing most of those optimizations on every build and allows you to turn them off.
46. choeger ◴[] No.44399968{4}[source]
Separate compilation also enables easy parallelization of type checking.
47. trinix912 ◴[] No.44400236{4}[source]
Had they left "governments" out of there it would've been almost fine, but damn I didn't know it's now governments changing DLLs for us!
replies(1): >>44400720 #
48. benreesman ◴[] No.44400720{5}[source]
https://en.wikipedia.org/wiki/Equation_Group

https://en.wikipedia.org/wiki/Advanced_persistent_threat

https://en.wikipedia.org/wiki/Operation_Olympic_Games

https://simple.wikipedia.org/wiki/Stuxnet

https://en.wikipedia.org/wiki/Cozy_Bear

https://en.wikipedia.org/wiki/Fancy_Bear

https://en.wikipedia.org/wiki/Tailored_Access_Operations

49. duped ◴[] No.44400814{5}[source]
Seems like you still misunderstand both the comment and context and getting overly emotional/conspiratorial. You might want to work on those feelings.
replies(1): >>44401034 #
50. benreesman ◴[] No.44401034{6}[source]
No one is trying to take anyone's multi-gigabyte pile of dynamic library closure to deploy what should be a few hundred kilobytes of arbitrarily portable, secure by construction, built to last executable.

But people should make an informed choice, and there isn't any noble or high minded or well-meaning reason to try to shout that information down.

Don't confidently assert falsehoods unless you're prepared to have them refuted. You're entitled to peddle memes and I'm entitled to reply with corrections.

51. tukantje ◴[] No.44401261{5}[source]
Will the real FORTRAN please stand up?
52. teleforce ◴[] No.44401934[source]
>When you have things like macros, advanced type systems, and want robustness guarantees at build time.. then you have to pay for that.

Go and Dlang compilers were designed by those that are really good at compiler design and that's why they're freaking fast. They designed the language around the compiler constraints and at the same managed to make the language intuitive to use. For examples, Dlang has no macro and no unnecessary symbols look-up for the ambiguous >>.

Because of these design decisions both Go and Dlang are anomaly for fast compilation. Dlang in particular is notably more powerful and expressive compared to C++ and Rust even with its unique hybrid GC and non-GC compilation.

In automotive industry it's considered a breakthrough and game changing achievement if you have a fast transmission for seamless auto and manual transmission such as found in the latest Koenigsegg hypercar [1]. In programming industry however, nobody seems to care. Walter Bright the designer of Dlang has background in mechanical engineering and it shows.

[1] Engage Shift System: Koenigsegg new hybrid manual and automatic gearbox in CC850:

https://www.topgear.com/car-news/supercars/heres-how-koenigs...

replies(1): >>44402967 #
53. rstuart4133 ◴[] No.44401996{4}[source]
All true, but given the number of "Rust compile time is slow" posts that blame the compiler I think it's safe to say most programmers don't understand the real underlying trade-off that's causes it.

Not all programmers of course - if you look at std there are many places that split types into generic and non-generic parts so the compiler will reuse as much code as possible, but it does come at the cost of additional complexity. Worse if you aren't already aware of why they are doing it, the language does a marvellous job of hiding the reason that complexity is there. I'd wager a novice Rust programmer is as befuddled by it as a JavaScript programmer coming across his first free() call in C.

I have this dream of a language like Rust that makes the trade-off plain, so the programmer is always aware of "this is a zero cost abstraction - you're just making it plain via the type system your doing the right thing" and "I'm going to have to generate a lot of code for this". Then go a step further and put the types and source you want to export to other libraries in a special elf section in the .so so you don't need the source to link against it, then go another step further and make the programmer using the .so explicitly instantiate any stuff that does require a lot of code generated so he is aware of what is happening.

That said, I don't think it would help the compile time problem in most cases. C++ already does something close by forcing you to put exported stuff in .h files, and they ended up with huge .h files and slow compiles anyway.

Nevertheless doing that would make for a Rust like language, that, unlike Rust, supported an eco-system of precompiled libraries just like C does. Rust is so wedded to transparent monomorphisation it looks near impossible now.

54. haiku2077 ◴[] No.44402614{4}[source]
As of about five years ago gccgo was slower for almost all workloads: https://meltware.com/2019/01/16/gccgo-benchmarks-2019.html
55. bjoli ◴[] No.44402705[source]
Chez scheme handles macros that expand to macros that expand to tens of thousands of lines of code in a breeze. Macros don't have to be slow.
56. pjmlp ◴[] No.44402941{4}[source]
And D, Active Oberon, Eiffel,....

Go got famous compile times, because for a decade a new generation educated in scripting languages and used to badly configured C and C++ projects, took for innovation, what was actually a return to old values in compiler development.

57. pjmlp ◴[] No.44402957{4}[source]
That is something I never have to care on my C++ projects, because I always make use of binary libraries, unless I am forced to compile from source.

Unfortunately that doesn't seem to ever be a scenario cargo will support out of the box.

replies(1): >>44409017 #
58. pjmlp ◴[] No.44402967[source]
It isn't an anomaly, it was pretty standard during the 1990's, until C and C++ took over all other compiled languages, followed by a whole generation educated in scripting languages.
59. pjmlp ◴[] No.44402975[source]
C++23 modules compile really fast now, moreso when helped with incremental compilation, incremental linking, and a culture that has not any qualms depending on binary libraries.
60. 1718627440 ◴[] No.44403903{3}[source]
Reading your comment is sounds like the opposite would be true, because so much linking would be needed to be done at runtime. But that perception fails to realize, that when claiming an executable is linked dynamically, most symbols were also statically linked. It is only the few public exported symbols that are dynamically linked, because there are deemed to be a reasonable separate concern, that should be handled by someone elses codebase.

I think lazily linking is the default even if you don't use dlopen, i.e. every symbol gets linked upon first use. Of course that has the drawback, that the program can crash due to missing/incompatible libraries in the middle of work.

replies(1): >>44405021 #
61. AdelaideSimone ◴[] No.44405021{4}[source]
A lot of vendors use non-lazy binding for security reasons, and some platforms don't support anything other than RTLD_NOW (e.g., Android).

Anyway, while what you said is theoretically half-true, a fairly large number of libraries are not designed/encapsulated well. This means almost all of their symbols are exported dynamically, so, the idea that there are only "few public exported symbols" is unfortunately false.

However, something almost no one ever mentions is that ELF was actually designed to allow dynamic libraries to be fairly performant. It isn't something I would recommend, as it breaks many assumptions on Unices, (while you don't get the benefits of LTO) you can achieve code generation almost equivalent to static linking by using something like "-fno-semantic-interposition -Wl,-Bsymbolic,-z,now". MaskRay has a good explanation on it: https://maskray.me/blog/2021-05-16-elf-interposition-and-bsy...

62. ChadNauseam ◴[] No.44409017{5}[source]
The actual reason that you don't have to care about this on your C++ project is because C++ doesn't let you define macros in C++, you can only define them in the preprocessor language. Therefore no compilation is needed to execute them.
replies(1): >>44410977 #
63. pjmlp ◴[] No.44410977{6}[source]
I never write macros in C++, other than header guards, for years now.

I belong to the school of thought that C style macros in C++ should be nuked.

Exception being source code I don't own.