Unofficial Hacker News companion — get notified on comment replies, and more.

Optimizing Rustc's next trait solver (opens external site)

212 points by g0xA52A2A 6 days ago | donsz.nl | 48 comments on HN | Vote on HN (opens Hacker News) |

View full thread · View on Hacker News

Comment thread (10)

  1. Rust compilation is impossible to solve for some reason.

    I have a 4k line project that depends on serde-json, xxhash, crossbeam and rand.

    It takes several seconds to compile in release mode.

    This is insane compared to my 20k line project in C++ which compiles everything and runs all tests in 0.7 seconds.

    The C++ project doesn't depend on libc or any external libraries but that is not a practical option in rust.

    I tried bunch of different options and actually removed some dependencies but it seems like it is not possible to get the rust program to compile fast.

    Does anyone know why it is this slow comparatively?

    Replies (4)
    1. serde, crossbeam, and rand all involve generics so you may be getting a decent amount more code than 4k lines after macro expansion (aka #[derive(Serialize, Hash)) and generic instantiation.

      you might also be able to drop crossbeam for stdlib types and rand for much smaller random library if you don't need it's comprehensive features.

      cargo's release profile also uses opt level 3 and thin-local lto by default. Whether you need those is up to you, hard to say more without knowing your project!

      but yes it is somewhat easy to allow rust projects to creep up in compile times especially with how easy it is to bring in big useful libraries that often come with a lot of features. disabling features where possible is the go to if you have big dependencies.

      Replies (1)
      1. Thanks for the suggestions, I'll look into replacing rand maybe.

        I don't depend on serde, I tried to find an alternative to serde-json but couldn't find anything.

        I like crossbeam a lot because it is multi consumer which means I can build a "work stealing" thread pool very easily.

        It seems like the majority of the time is taken by the linking.

    2. > The C++ project doesn't depend on libc or any external libraries but that is not a practical option in rust.

      Aside from the std library (which you're not paying for in compile time because it's precompiled). Why is this different in Rust vs. C++? Sure, it's inconvenient not to use e.g. serde, but it's equally inconvenient in C++...

      Taffy (https://github.com/DioxusLabs/taffy) which is ~26k LoC + a handful of dependencies compiles in release mode in ~1.6s

      Replies (1)
      1. It is very obvious if you try to actually do something without libc in rust. It is just a major pain. You could theoretically do it but not really in practice.

        Doing `RUSTFLAGS="-C target-cpu=native" cargo install --path . --locked` on my project.

        With lto=`thin` just takes 3.6 seconds on my pc which is very bad if you compare it to the c++ project.

        Also the output binary is 1.3MB which is also supremely bad.

    3. In addition to what PudgePacket has said, and although LOC isn't a great proxy for the project complexity, it doesn't seem like you're counting your dependencies' LOC. Serde has more than 20 kLOC and serde_json more than 15 kLOC. serde_derive pulls in syn which has ~50 kLOC and is famously slow to compile.

      Also the point of the release profile is to trade compilation performance for runtime performance (you'd be better off iterating on the dev profile).

      There are also many other details that interfere and can't be inferred from your comment like: what's your OS, which linker are you using, are you using opt-level 3 on C++, which compiler are you using on C++, are you using heavily using templates in C++ or macros in Rust, etc.

      Replies (1)
      1. It is basically same weight in terms of code style.

        I use clang for compiling c++. But I use a custom build system I built on top of it. I compile with -O2 but using -O3 or even -O5 doesn't make much of a difference.

        I tried pretty hard to make sure both projects output small binaries and compile fast.

        I don't depend on serde because I knew it is bloated but didn't know serde_json was so bloated too.

        I am using latest stable rust on linux x86_64 so I assume it is using lld which is what c++ project uses too.

        I don't think there are any template use in either project except things like hashmaps and slices etc. I don't use traits except to interact with dependencies in rust.

        I use a specific structure in C++ so it is not like regular C++ code but that is just basically impossible to do in rust because I would have to write too much unsafe code and it feels mandatory to use RAII in rust.

        Replies (1)
        1. > or even -O5

          -O4 and higher are (currently) equivalent to -O3

          > I don't depend on serde because I knew it is bloated but didn't know serde_json was so bloated too.

          serde_json depends on serde though?

          > I don't think there are any template use in either project except things like hashmaps and slices etc. I don't use traits except to interact with dependencies in rust.

          If you're interested in investigating further it might be worth trying cargo bloat [0] or similar to get a bit more data about what is consuming space. rustc's self-profiling framework [1] might be another way to get data.

          [0]: https://github.com/razrfalcon/cargo-bloat

          [1]: https://github.com/rust-lang/measureme

          Replies (1)
          1. > serde_json depends on serde though?

            Ahhh, of course.

            Thanks for the suggestions.

            Edit: I was able to reduce the output code size to <600KB from the original 1.3MB after exploring the suggestions. It made the release build time a lot longer but it is worth it for my use case.

    4. Well, rustc doesn't just compile, there's a lot of work to make sure your program is memory-safe etc. Checking the types takes time.