News for hackers. Stuff that matters.
Re:C++ vs. Rust Build Times by choeger on Friday January 06, 2023 @02:19AM (#34271882)
I have a hunch that rusts global compilation model (as opposed to C++ separate compilation, as crippled as it is in practice due to header-only libs and link times) should always lead to C++ scaling better with the size of the project.

Simply put: Rust doesn't offer an abstraction past type checking. Rust could potentially introduce its equivalent of PCH, or maybe it already does so in some internal cache, but as long as it uses monomorphization and no PhD student comes up with a clever trick to combine that with separate compilation it will scale worse.

That being said, maybe a more fair comparison wouldn't involve polymorphism in the rust code? C++ doesn't really have polymorphism, it uses templates for a similar feature. So what happens if someone replaces rusts polymorphic functions with macros?

Re:C++ vs. Rust Build Times by winrid on Friday January 06, 2023 @02:37AM (#34271991)
What big Rust projects do is split things up into crates which can be compiled as individual units. still bigger than a single file, but it helps.
Re:C++ vs. Rust Build Times by strager on Friday January 06, 2023 @03:44AM (#34272450)
Yup! And I evaluated some different crate structures: https://quick-lint-js.com/blog/cpp-vs-rust-build-times/#opti...
Re:C++ vs. Rust Build Times by not_the_fda on Friday January 06, 2023 @05:43AM (#34273218)
What big c++ projects do is split things up into libs which can be linked as individual units. still bigger than a single file, but it helps.
Re:C++ vs. Rust Build Times by winrid on Friday January 06, 2023 @11:43AM (#34277011)
I think being snarky is against the rules?

Also creating nested libs as separate compilation units is so difficult with cmake etc I rarely seen a large C++ project do this... it is much easier with Cargo.

Re:C++ vs. Rust Build Times by not_the_fda on Friday January 06, 2023 @01:22PM (#34278712)
Creating nested libs in cmake is trivial and is done all the time.

I'm sorry I was a bit snarky. But the thing is that the Rust evangelists don't understand the domains and types of large scale projects that C++ is used in and how those systems are built. Yet the they come along and keep pushing for Rust without understanding why C++ developers aren't switching and wave their hands at the issues we have with Rust.

Show me a successful multi million line Rust project. Maybe throw in a complex UI. Maybe some GPU computation.

Re:C++ vs. Rust Build Times by winrid on Friday January 06, 2023 @04:16PM (#34281212)
I wouldn't call myself a rust evangelist. I prefer Java because it's productive and I can finish projects in it and not have to touch them for years.

FWIW Rust is great for high performance web services, not so much UI stuff. C++ would be better for that.

Re:C++ vs. Rust Build Times by jimbob45 on Friday January 06, 2023 @02:21AM (#34271889)
What do you mean by abstraction beyond type checking? I’m asking to educate myself.
Re:C++ vs. Rust Build Times by choeger on Friday January 06, 2023 @04:00AM (#34272564)
"Beyond" in the sense of "after". Languages that support separate compilation usually have some sort of artifacts that do not need to be type-checked anymore. Say, you compile an Ocaml module. The result is a compiled (bytecode or native code) file that represents the implementation of the module and doesn't need to be generated ever again. Essentially, the compiler abstracts over a module after it dealt with it. You could the

If you do the same in rust, you need access to the module's implementation for monomorphization. You might skip the type checking of a function of the implementation or even cache the generated code for a monomorphization as optimizations, but generally you have to treat every function as often as it is used. So the best rust could do is akin to C++ precompiled headers whereas a language like OCaml can simply compile every module exactly once.

Note: OCaml pays for this elegance. Its uniform object representation is less efficient than what rust does. It's a trade-off that the rust designers consciously made, I think.

Re:C++ vs. Rust Build Times by scaredginger on Friday January 06, 2023 @05:21AM (#34273069)
What you've said here is mostly correct. The key part you've omitted is that monomorphization is opt-in. One could write Rust like C if they wanted to; they would have to be insane, but they could
Re:C++ vs. Rust Build Times by vegerot on Friday January 06, 2023 @06:49PM (#34283098)
“opt-in” in the sense that I could just multiple versions of my functions if I want to? Agreed!
Re:C++ vs. Rust Build Times by scaredginger on Friday January 06, 2023 @11:11PM (#34285112)
Yeah, or accept a small runtime cost in exchange for a smaller binary
Re:C++ vs. Rust Build Times by vegerot on Friday January 06, 2023 @02:48AM (#34272067)
> maybe a more fair comparison wouldn't involve polymorphism in the rust code?

I think using features of the language is fair game

btw, don't C++ templates also use monomorphization? I agree C++'s implementation is "lower level" (from a compiler perspective) than Rust generics, but that's one of Rust's selling features

Re:C++ vs. Rust Build Times by choeger on Friday January 06, 2023 @04:02AM (#34272573)
Template specialization sometimes looks like monomorphization, but it is really a different thing. I would compare C++ templates more macros, as they tend to do way more (e.g., allow for overload resolution).
Re:C++ vs. Rust Build Times by scaredginger on Friday January 06, 2023 @05:15AM (#34273028)
Please elaborate. I agree C++ templates are more flexible in what they do (SFINAE etc.), but Rust generics and C++ templates still both use monomorphization
Re:C++ vs. Rust Build Times by choeger on Friday January 06, 2023 @02:23PM (#34279552)
I wouldn't say that C++ templates use monomorphization, because there are no polymorphic functions to begin with. Monomorphization, as in "the standard FPL technique", comes after type checking. C++ template specialization precedes type checking.

Of course one can redefine the meaning of "polymorphism" and "monomorphization" to fit C++, but then these words lose nearly all meaning.

Re:C++ vs. Rust Build Times by vegerot on Friday January 06, 2023 @06:48PM (#34283087)
TIL. Thanks!

In my plebeian brain I just thought “monomorphization = static dispatch” / 0-cost abstraction (vs. just writing multiple versions of a function), which both Rust generics and C++ templates fall into.

Now I see that “monomorphization” has a specific definition in FPL

Re:C++ vs. Rust Build Times by scaredginger on Friday January 06, 2023 @09:12PM (#34284323)
I'm not satisfied by your explanation here. What definition of 'polymorphism' are you using? Why would type checking need to precede monomorphisation? Additionally, how do C++ concepts fit into your beliefs?
Re:C++ vs. Rust Build Times by choeger on Saturday January 07, 2023 @03:26AM (#34286249)
This one:

https://en.m.wikipedia.org/wiki/Parametric_polymorphism

Note that it's called "parametric" because OO folks stole the word polymorphism for a completely different thing.

The idea of a polymorphic function is that it acts the same for all possible input values (for at least one parameter). This is captured by its type. If you look at a polymorphic function (say "f(x) = x" with the type "for all 'a. 'a -> 'a ") you can check that type once and you know that monomorphization works for any possible input.

Monomorphization is then the process to replace 'a with a concrete type to facilitate code generation. What it actually does, depends on your language of course. It might for instance decide how to pass arguments or compute offsets from record labels. Crucially, monomorphization in this sense never fails, at least not because you input the wrong type. The user doesn't need to know that monomorphization happens at all. It's an internal design choice of the compiler.

If you look at C++, template specialization is very visible. The template that represents the identity function doesn't have a type. Only after specialization, the user learns whether it was successful or not (e.g., because copying the type is not possible).

I never worked with concepts myself, but to my understanding, they are an attempt to generate more useful error messages. As far as I know, the concept declaration of a template might simply be wrong, there's no automatic check that guarantees that a template can be instantiated if I adhere to the contract, right? So the type check still has to happen after specialization and the user has to consider specialization in their mental model of the code.

Re:C++ vs. Rust Build Times by clappski on Friday January 06, 2023 @03:29AM (#34272360)
C++ has two types of polymorphism;

- Templates (compile time), which are generic bits of code that are monomorphized over every combination of template parameters that they're used with.

- Virtualization (runtime), classic OO style polymorphism with a VTable (I think this similar to Rust's dyn trait?).

Re:C++ vs. Rust Build Times by spoiler on Friday January 06, 2023 @04:07AM (#34272604)
I think they meant to say that neither templates (nor virtual classes) are quite up to the same level of polymorphism that Rust has. Ie Rust type checkes at definition, as opposed to copy-paste and hope for the best (wrgt templates) when trying to compile the code. In other words Cpp templates are closer to Rusts declarative macros than to Rust generics. Hopefully concepts make this better in Cpp.
Re:C++ vs. Rust Build Times by gpderetta on Friday January 06, 2023 @08:27AM (#34274331)
Unfortunately, the concepts we got in C++20 still do not allow for type checking definitions and there isn't a realistic path to get there.

I still think that C++ templates are different than rust macros as they are not a separate pass from type checking.

Re:C++ vs. Rust Build Times by pjmlp on Friday January 06, 2023 @10:15AM (#34275522)
As always in C++ world, there is a workaround with static analysis, which could give errors when template code reaches out to capabilities that aren't part of the concept definition.

Either that, or switch to Circle I guess.

Re:C++ vs. Rust Build Times by gpderetta on Friday January 06, 2023 @10:30AM (#34275749)
Does such a static analysis exist? I think the most realistic solution is to write an archetype class for each concept and instantiate each template with its relevant archetypes.

Now can we automate writing archetypes from concepts (and viceversa)? Maybe in C++64 when we finally get static reflection.

Re:C++ vs. Rust Build Times by pjmlp on Friday January 06, 2023 @11:17AM (#34276521)
Not yet, hence the conditional form.

I really hate how C++/WinRT used the "C++ reflection is around the corner" excuse to kill C++/CX and downgrade the COM development experience back to pre-.NET days.

Apparently it isn't around the corner.

However with type traits and a bit of constexpr if (requires {....}) it is possible to have a kind of poor man's compile time reflection.

Re:C++ vs. Rust Build Times by ChubbyGlasses on Friday January 06, 2023 @03:44AM (#34272452)
This is pure speculation, but I suspect egregious use of proc macros in Rust also inflate compilation times a bit. Just speaking anecdotally, but removing proc macros in one of my hobby projects decreased clean build by about ten seconds. Definitely not a couple magnitudes like the author saw, but not unnoticeable either.
Re:C++ vs. Rust Build Times by strager on Friday January 06, 2023 @04:05AM (#34272592)
Were those proc macros that you wrote, or proc macros in third-party crates?
Re:C++ vs. Rust Build Times by ChubbyGlasses on Friday January 06, 2023 @04:52AM (#34272884)
Mine were mostly third party crates like bitflags and thiserror. Not saying that's what was effecting your build times, I'm not really familiar with your repo (my comment was only directed at my proj), but also it's pretty hard to write a significantly large project without macros; it's just the devil you kinda have to live with.
Re:C++ vs. Rust Build Times by dist1ll on Friday January 06, 2023 @05:13AM (#34273016)
Proc macros are used too often in the Rust ecosystem, even for very simple crates. Library authors and maintainers care too little about compilation times.
Re:C++ vs. Rust Build Times by adwn on Friday January 06, 2023 @05:40AM (#34273189)
I always wonder: If the proc macros were executed JIT-like with the Cranelift backend instead of compiling them with the LLVM backend, wouldn't that drastically reduce build times of proc-macro-using code? The Rust compiler spends most of its time in LLVM, if I recall correctly.
Re:C++ vs. Rust Build Times by strager on Friday January 06, 2023 @05:52AM (#34273294)
I have heard talk of compiling proc macro crates with different rustc flags than other code. But I didn't go down this rabbit hole.
Re:C++ vs. Rust Build Times by epage on Friday January 06, 2023 @08:34AM (#34274383)
Someone recently looked into doing this by default. The problem was the results varied depending on how often your "host dependencies" (proc macro, build.rs) were shared with your "target dependencies" (`dependencies`, `dev-dependencies`). When there was a lot of sharing, it hurt, but otherwise it sped up build times.
Re:C++ vs. Rust Build Times by __jem on Friday January 06, 2023 @11:14AM (#34276477)
The brightest minds in the Rust community are investigating! https://github.com/dtolnay/watt
Re:C++ vs. Rust Build Times by cormacrelf on Friday January 06, 2023 @11:32AM (#34276823)
The most promising proposal so far is to compile proc macros to WASM. Then you precompile them to a single cross-platform WASM binary, publish those to crates.io alongside the source code, and then execute them in a sandbox. You wouldn’t see much benefit until all your chosen proc macros were available in this form, but after a while this would be great, basically no downsides. In fact you can publish proc macros this way today, IIRC with some caveats about compiler versions (?), but it’s manual and a proof of concept only so nobody has done it.

The only thing missing was effort into the idea to make it the default and able to be supported long term. You need a stable WASM ABI that won’t change across rustc versions and for someone to add a runtime to rustc.

Dtolnay (the author of the Watt concept) recently put out a message looking for help putting together an RFC. https://twitter.com/davidtolnay/status/1574913813400330241

Re:C++ vs. Rust Build Times by vegerot on Friday January 06, 2023 @12:54PM (#34278304)
The article breaks down how much time is spent by each part of the compiler (borrow checking, LLVM, macro expansion, etc.) check it out!