Unofficial Hacker News client - receive Push Notifications on replies.

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

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

Comments (45)

  1. Did it made it more faster or efficient?
    Replies (2)
    1. The last graph on the page should answer your question.
    2. There's no point in being fast or efficient if you're wrong. For instance, I can write an O(n) sorting algorithm that deletes any out of order elements. Or better yet, a O(1) sorting algorithm that declares the array sorted without checking it. You in the market for that?
      Replies (1)
      1. You're very late to market, https://www.dangermouse.net/esoteric/ has had us covered for many years
  2. 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.
    Replies (5)
    1. > I don't know if any other compilers that have made the connection to logic programming

      Which other compilers do you know?

      Replies (2)
      1. 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?

        Replies (1)
        1. Did you mean to reply to me or the parent?
          Replies (1)
          1. That was the answer to

            > Which other compilers do you know?

      2. 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.
    2. type systems correspond to logics so this tends to be how it goes
      Replies (1)
      1. Types are pretty much like values, except at compile time instead of runtime.
        Replies (1)
        1. eh, usually a fairly large difference is that only type resolution implements full unification in most programming languages with type inference.

          there are plenty of languages where types are also just values at runtime for which that's not a particularly helpful conception of things, as well as ones where you have all regular programming facilities at comptime

    3. 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.

      Replies (1)
      1. Even those theorem provers that are not compilers.
    4. If you name it constraint based programming, it all makes more sense.
    5. There are compilers that use Z3 for type checking. I'd say that's even more explicit!
  3. 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.

    Replies (1)
    1. Agreed on both counts. I thought the swap was absent too.
  4. 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...

    Replies (1)
    1. 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.
  5. 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.
  6. Nit: "et cetera" is two words.

    It is a latin phrase that literally translates to "and the rest." "Et" means "and," and "cetera" means "the rest."

    This is a great article! Apologies for being pedantic. I'm just noting this fun fact here in case the author is checking this thread.

    Replies (1)
    1. https://www.merriam-webster.com/dictionary/etcetera

      It's very commonly written as one word in English. Just do a web search for "etcetera" to find many examples.

      Replies (3)
      1. Just write &c.
      2. It doesn't make it incorrect less.
        Replies (2)
        1. I'm sorry did you mean less incorrect?
        2. Sure it does. Language norms change by use, and the more a new/different/"wrong" form is used, the more correct it becomes, until it's widely accepted as canonical.

          Your own writing and speech already uses thousands of such words with changed spelling (when the incorrect became the norm), changing meaning, etcetera (joke intended), from how they were decades or centuries ago.

          Replies (1)
          1. irregardless (is it a word?)
            Replies (2)
            1. You don't know how words work, do you?
            2. Could very well be an accepted one soon, since, as I said "Language norms change by use, and the more a new/different/"wrong" form is used, the more correct it becomes, until it's widely accepted as canonical".
      3. Fair. After doing some research, it seems that the correctness of the single-word form is more contested than I thought.

        If we're citing dictionaries, I'll point out that Wiktionary describes the single-word form as proscribed, i.e. recommended against. [1] And neither the Oxford English Dictionary nor the Cambridge Dictionary even have entries for the single-word form. The Chicago Manual of Style uses it in its two-word form when discussing abbreviations, though it makes no statement about the one-word form as far as I can tell. Same with APA.

        So I'll leave it at that. I had no trouble understanding what the author meant. This was purely a nit about style, which is ultimately just bike shedding.

        [1]: https://en.wiktionary.org/wiki/etcetera

  7. I was curious to figure out exactly why the new trait solver was needed.

    Apparently it's easier to work with and more correct. Future type system changes are blocked on it. These 2 in particular were called out as being blocked by the new trait solver [1]

    [1] https://goals.rust-lang.org/2026/rtn.html

    Replies (1)
    1. More information from the official blog post a few months ago: https://blog.rust-lang.org/2026/08/21/enabling-next-solver-o...

      "As an underapproximation, we currently know of more than 200 issues on GitHub fixed by this change."

  8. I have a rust genomics pipeline where I was seeing a 3 minute “incremental” build that was entirely spent on trait resolution. I did some -Zself-profile analysis and saw the type checker spending >50% of its time re-deriving the same set of obligations over and over (due to a single blanket impl with a stack of associated type). I wasn’t sure if the problem was that it was missing a memoization cache or that it was just searching poorly.

    This is a great way to describe the “lazy” nature of the new solver.

    Are there any benchmarks that include pathological cases of blanket implementations? (It is easy to run into this if you are writing generic numeric code – as I do for a living in bioinformatics)

    Replies (1)
    1. Were you seeing slow trait resolution times with the old or new solver? If the latter I think you're more likely to get a dev response if you open an issue [0] with more details than posting here.

      [0]: https://github.com/rust-lang/rust/issues