Optimizing Rustc's next trait solver

(donsz.nl)

166 points | by g0xA52A2A 4 days ago

6 comments

  • ozgrakkurt 30 minutes ago
    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?

    • PudgePacket 0 minutes ago
      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)).

      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.

  • AlotOfReading 7 hours ago
    It's interesting how much of the next gen compiler improves borrow heavily from logic programming. I don't know if any other compilers that have made the connection to logic programming as explicit as rustc.
    • timhh 3 hours ago
      There are compilers that use Z3 for type checking. I'd say that's even more explicit!
    • tkz1312 6 hours ago
      I mean any compiler that has a typeclass system basically does the same, since there is quits a deep connection between the two.

      Of course any theorem prover will have an even deeper connection to logic in it's compiler and we've been building those since the 70s.

      • auggierose 3 hours ago
        Even those theorem provers that are not compilers.
    • frutiger 7 hours ago
      > I don't know if any other compilers that have made the connection to logic programming

      Which other compilers do you know?

      • rurban 53 minutes ago
        My C compiler rcc uses a handwritten solver in -O3 to solve contracts. I've tried z3 and others, but my own was way faster, no overhead. It's like MySQL vs sqlite in-proc.
      • pjmlp 2 hours ago
        C++ compilers when doing a mix of template and overload resolution?

        Swift compilers when doing the same?

        Haskell compilers when doing the same?

        Idris, F*, Dafny, Koka,..... compilers doing the same?

    • nagaiaida 6 hours ago
      type systems correspond to logics so this tends to be how it goes
      • ahartmetz 1 hour ago
        Types are pretty much like values, except at compile time instead of runtime.
    • nurettin 5 hours ago
      If you name it constraint based programming, it all makes more sense.
  • Game_Ender 1 hour ago
    The article is great but actually lacks data on overall performance. It looks like the big focus was handling pathological performance cases, which they have done a great job on. Overall though this looks like a mild performance regression (~6%) for the compiler based on an October test [0]

    I like that Rust is principled about correctness, but as a mostly moved on long suffering C++ developer it’s disappointing the replacement suffers from the same poor compilation performance. At least rust has a culture of tracking and working on perf even if philosophically the community puts it low down their list.

    0 - https://perf.rust-lang.org/compare.html?start=b5427502dab50c...

    • panstromek 1 hour ago
      This is still work in progress, to be fair. Those results are already outdated, a number of optimizations landed since then. It'll probably take a bit more time to close the gap, but we expect the final result to be mostly similar to the old solver or faster. I'll also caveat that our benchmark suite is skewed towards pathological cases (when it comes to the type system code), so it doesn't necessarily indicate performance on average program.
  • eosis 5 hours ago
    Awesome work, seems an odyssey.

    I think there is a little error in the code snippets near the caching section start where it claims variables are swapped but they are not? Though maybe I have just misunderstood something.

    • RossBencina 4 hours ago
      Agreed on both counts. I thought the swap was absent too.
  • xiaogui917 2 hours ago
    [dead]
  • essfrmexistence 7 hours ago
    Did it made it more faster or efficient?
    • lukeify 7 hours ago
      The last graph on the page should answer your question.