Building a Query System - Arya Dradjica | EuroRust 2026
By EuroRust
Summary
Topics Covered
- Rustc’s Size and Stability Block Architectural Innovation
- Item-Level Dependencies Unlock Compiler Parallelism
- Sixteen Gigabytes of Rust Dependencies Look Redundant
- Parallel Rustc Still Runs Key Phases Serially
- Async Queries Combine Parallelism with Network I/O
Full Transcript
[applause] [music] My name is Arya Gracious and [music] thank you for coming to my talk. I'm so
excited.
So we're going to we're going to keep this quick.
I love optimization. Who's here with me?
[cheering] Hell yeah. Okay.
Hell yeah. Okay.
So optimization is the best thing. It's
just I spend all my time thinking about it. It's the greatest and I've always
it. It's the greatest and I've always been I I've tried to figure out like where what should I spend my time optimizing you know if I only have so much time on this earth what am I going to do? And I decided to write a Rust
to do? And I decided to write a Rust compiler.
Mistake maybe sure.
And for the last year and a half I've been trying to write a Rust compiler mostly myself.
It's called Crabby. The idea here is that I think the high level architecture of Rust C is now its biggest bottleneck.
There are big changes that could be made to a Rust compiler to explore different design decisions or different high level architectures. And we simply can't try
architectures. And we simply can't try these out in Rust C anymore.
It's just too big. There's too many people working on it. There are
stability requirements that it needs to satisfy.
And we still need a place to try these things out. We should still find out
things out. We should still find out would these things be worthwhile if not for Rust C for the next generation of compilers.
And Crabby is a playground where I want to experiment with this.
So today I'll be talking about the query system capillary. Now if you don't know
system capillary. Now if you don't know what a query system is that's fine.
We'll we'll go through this in a second.
But there's going to be a lot of things to go over. A query system is really major part of a compiler and by making different design decisions here, we can explore so many cool
things.
Um I'm mostly excited for just improving compile times in a million ways, improving uh parallelization, uh reducing uh the size of target folders, um
and you know I really want to explore these.
Um but let's talk about crate systems. Who here knows what a crate system is?
Okay, yeah, yeah, yeah, yeah, nice. Um
who here has used a crate system?
This is a trick question.
Okay.
Um well if anybody didn't raise their hands, I'm sorry.
You're so wrong actually.
Um can anybody tell me what this is?
Make.
Thank you. This is a make file because make is actually kind of a crate system.
Um or uh crate systems are make. Take it as you will.
Um we can uh view uh the contents of make file as a dependency graph.
Uh we have a bunch of individual tasks to do or individual uh data units of data that we need to Um and
a build system like make or ninja or whatever you like allows you to build those and work with them incrementally.
Now what does this mean for the Rust compiler? The
Rust compiler has a crate system.
Uh where does that go?
Um here's a little taste of what that might look like. It is very very inaccurate. Don't
like. It is very very inaccurate. Don't
worry about it.
And uh over here I have two crates. I
have a binary called bar and a library called foo.
And if we think about what are the steps that the Rust compiler needs to execute to uh compile all of this for us, uh here's a few of the like high-level things that you can think about, right?
We want to compile the main function.
That's the main thing for a binary.
And in order to do so, um we of course need type check it.
And we will notice that oh, it refers to foo double colon hello.
And we need to go find what that is.
We need to resolve those references. We
need to then finish type checking using this information.
All of these are just individual steps in in a dependency graph exactly the same where you'd find in make or ninja.
But there are of course some differences. Usually you'd use make or
differences. Usually you'd use make or ninja as a standalone tool where you just where you just declare what the dependency graph should look like.
For query systems and rust C, this is a lot more complicated.
You would dynamically find out what dependencies you're going to have. In the previous example, we didn't know upfront that bar main
needed to use foo hello.
We had to dynamically discover these things.
And while with a standard build system, you'd usually work in units of files.
You're compiling foo dot O or reading in foo dot C.
In a query system, you can work with much smaller units of data. You can work with an AST or an individual rust item.
So what does a query system look like?
Well, here's mine.
It's fairly simple. I've tried to make a
fairly simple. I've tried to make a mapping between what a make file might look like and what identical code in my query system would look like.
And as you can see, all of our individual artifacts here, so foo, foo dot O, foo dot C are just functions you call. We've we've marked them with this query attribute.
And that just makes them part of the query system. They're queries now.
query system. They're queries now.
And the implementation of these functions such as in foo, it's called to foo dot O.
Determine what the dependencies here are.
and you can rely on the results from one query to to then query for other things. The the
query system will automatically track all of these calls.
And that's how it dynamically discovers dependencies.
So in this case, I've actually based Capillary on Salsa, which is the query system used by the LSP Rust analyzer.
It's Salsa is a really convenient library to use and so I try to model my API after it.
The main thing here is memoization.
The same thing you'd find for any sort of incremental system where if we try to compile foo twice, the second time it would just use cached data.
So the query attribute macro modifies this function so that at the end of computation, it'll save the results to a cache and the next time you try to call it, it will first check the cache and
try to use the cache result.
So Capillary exists and you can you'll get to see some of the code in a second.
And I'm already using it for Cargo dependency resolution.
But you might be wondering why build a query system?
There are other query systems. Why build a Rust compiler? There are other Rust compilers.
A few. And
I want to take you through some of the high-level things I was thinking about based on my experiences using Rust.
So we'll start with talking about some big projects. Who here works with
big projects. Who here works with a project with let's say more than five crates in there?
Whoa, that's a lot of hands.
So I'm sure that you've had some experiences where maybe the LSP feels really slow or you might even sometimes split up the crates in your codebase to try to compile
things faster. Anybody's had that
things faster. Anybody's had that experience?
Yeah okay.
And so I was actually recently trying to optimize the Rust compiler itself, and very much had the same issue. I
would make a one-line change in one crate in the Rust compiler, in this case the top-level uh Rust C expand, and it would take around seven seconds for just a cargo check,
uh which I find really frustrating. Um,
if a tool takes longer than it takes for me to blink, that's a mistake.
Um, and we should fix that.
Um, and so then I collected some more statistics, and the first thing I noticed is that this is really not a parallel process.
Um, I have 16 CPUs. They're good CPUs, but Rust C just won't use them, guys.
Um, we're only using 1.4 CPUs on average across that entire compiler process.
Um, and you might be wondering, well, all of these we we should be able to compile some of these crates in parallel, and we can as long as we respect the dependency graph.
[snorts] And it's a messy one. Um,
over here, if you follow along the rows, you can see some crates that could be compiled in parallel, and that's where that 1.4 is coming from. Uh, we have three crates here, Rust C build in
macros, Rust C expand queries, Rust C metadata, that are going to be built in parallel. But there's a lot of crates
parallel. But there's a lot of crates which depend on each other, and because of the way that the Rust compiler and cargo work together,
you can only compile a crate after all of its dependencies have been compiled.
And my question was, what if we don't do that?
Um, could we somehow compile a crate before its dependencies are ready? What would
that involve?
Um, so let's take a slightly simpler example because I don't like that dependency graph.
Uh, going back to the same two crates we had before, um now we know that um bar, the binary depends on foo.
And you could just represent that as a simple uh line between bar and foo. But
if you look a little deeper, we can see that actually both of these consist of individual rust items and some of them depend uh on each other.
Over here only one of the items from bar depends on one of the items from foo.
So um when you have a dependency graph like this, this is really an every single edge in here is really an over approximation. We're saying
over approximation. We're saying probably one of at least one of the items in one crate depends on maybe some of the items in the other crate.
Um but maybe we could try to avoid looking at uh work at the granularity of crates.
Maybe we could just try to work on the granularity of rust items. And that that we actually get a lot more opportunities for parallelism.
We can try compiling uh all of these unrelated functions completely independently.
Um and I really want to explore this in Crabby.
Um this is a massive change to try because cargo and rust are independent projects essentially and cargo handles compiling multiple crates and rust does all the
work of compiling one crate.
And in Crabby I'm essentially merging them together.
Crabby as a single process will be able to compile multiple crates in one go.
And then it can parallelize in units of rust items, not at the or not in units of crates.
And um it can it can then build up this more specific dependency graph um and focus on the item level dependencies that we have.
And I think that would get down those compile times especially for large projects where you're just doing simple incremental builds by hopefully an order of magnitude too.
Um so um we're already doing some of this. Of
course, there's no way I can show this to you because one person cannot build a rust compiler in a year.
Um and um I'll it it'll take a while to have um a good demo for this. But I'm already implementing cargo inside Crabby. I'm
already, you know, supporting parsing cargo to Toml and um extracting um uh crates to compile.
Um but the next stop there is going to be name resolution for which I needed a query system. So, I'm hoping to have
query system. So, I'm hoping to have cool things to show in about a year.
Um the second thing we're going to talk about is our friend, our enemy, the target folder. Um
target folder. Um I recently had uh my laptop go through some fun crashes because um
target folders ate up all my disk space.
Um has anybody else here had the same experience?
What?
Okay. Okay. Um well, I then did a little bit of cleanup and now target folders only take up 57 GB.
Yay.
Yay?
Yay?
Um it's it's a little concerning. So, I was wondering, well, what's in there? You
know, target folders feel like this big opaque thing. What's actually in there?
opaque thing. What's actually in there?
So, I pull out the trusty Unix tool NCDU and we just take a look inside. Um and
I'm going to talk about the two uh biggest subfolders of this, uh incremental and dependencies.
Let's start with incremental. Um Rust C always saves uh the data that it uses during compilation. That's the basis for
during compilation. That's the basis for incremental, right? You run cargo check
incremental, right? You run cargo check once, Rust C is going to write down a bunch of stuff about all the compilation steps it did. And if you make any changes on a run cargo and run uh cargo check again,
Rust C only Rust C can reuse most of the data from the previous compilation.
But, it always saves all of the data that it uses during a compilation.
And I was wondering, can we somehow get that 7.7 GB down? That'd be nice.
So, let's take a slightly simpler example.
We go back to the friendly world of make. Who here likes make?
make. Who here likes make?
Good. Those are good numbers.
So, suppose that we have two two different binaries that we're working on in a single project. So, they're both part of the same make file.
But, I'm only going to be working on bar right now. So, I've already compiled
right now. So, I've already compiled foo. So, we have foo.c, we have foo.o,
foo. So, we have foo.c, we have foo.o,
we have foo. They're all done. But, I'm
only modifying bar.c.
And so, every time I run make after make doing any modifications, it's going to recompile bar.o, it's
going to recompile bar, but foo stays stays unmodified because there's I've not changed it at all.
But, if we think about it, we don't need foo.o anymore.
foo.o anymore.
We already have the foo binary. That's
done. That's the thing we actually wanted.
foo.c is the source file.
But, foo.o as an intermediate could be removed.
If I'm only working on bar, we don't need that.
And I think this strategy can extend to Rust as a whole.
So, what if we could remove these intermediate artifacts if they're not being used often?
What if we could set a maximum size on our target folders and let Rust decide what to keep and what to throw away?
And this is something I have implemented in capillary already.
It's fairly simplified, but there's just a trim caches method in there.
And you can tell it to Well, you can call it with a maximum size and it just trim out everything else.
Um, I think this is really possible. I'd
love to see this in a real Rust compiler, and then hopefully I don't uh have some fun SSD cleanup to do.
And then let's move on to dependencies, um which were the other five, six gigabytes in there. So in total across my 57 gigabytes of target folders, 22
gigabytes are in this target/deps folder.
Um, and so I looked around like what's in there? So we're going to quickly, if
in there? So we're going to quickly, if we can, uh I'm just going to look at my code and run There's okay.
Great.
Um, we're getting there? Yeah.
So um here is same target folder we were looking at before.
Um, we can scroll around some of those dependencies.
Um, now we can see a lot that we there are like quite popular packages.
There's Tokyo, you can see Russells, request, uh GIF, um you know, there's hundreds of these.
And if I go through go to um another project, maybe something in here.
Yeah. We're going to see some of the same dependencies. I have Tokyo in here
same dependencies. I have Tokyo in here as well.
And all of these hundreds of tiny dependency files are adding up and taking up that 22 gigabytes.
So uh we're we do some statistics.
We find out Um, there's about 16 gigabytes out of those 22 that look redundant.
Which is really concerning.
Um, now here's an example from from allocator API do.
Um and you can see that the same rlib file is appearing in a bunch of different projects I have.
Um in fact they often have exactly the same checksum.
Um is there some way we could try to deduplicate these?
Um so okay my apologies.
Um my laptop crashed 15 minutes ago. Give
me a second.
Okay.
Yeah so um I think that we could deduplicate these dependency files. Um I think a lot of that 16 GB
files. Um I think a lot of that 16 GB could be reclaimed and I would love to see ways to reduce uh target folder usage.
Um and I really want to try some of these in Cabby.
So hm um the last thing I want to talk about is concurrent queries and I'll explain what that means in a second.
Um here is a simplified version of cargo dependency resolution in Capillary uh in my query system. Can
anybody point out what the problem here is based on the title of this section?
Thank you. Lack of concurrency. Um
so Salsa works the same way, Russi works the same way. When you have uh a query and it tries to query for something else, in this case we're recursively
resolving more packages, only one of those can happen at a time.
So we're going to first start resolving our top level package, then it's going to try resolving the first dependency, finish doing everything there, then the second dependency, and so on and so forth. But all of those dependencies
forth. But all of those dependencies could be resolved concurrently.
Um we're it feels like we're really missing out.
Um and in in a sense that means that queries are actually inherently serial.
They're single-threaded.
Um, and Rusty has the same limitation.
So you might be wondering, well, Arya, Rusty has a parallel front end though.
Things are not single-threaded.
What are we doing?
Um, shall we let's see if well, yeah. Okay. So um
I recorded uh I I profiled Rusty um using its parallel front end uh on one of its compile time benchmarks.
Uh specifically I told it please use four threads.
And as you can see uh well so the yellow bars here indicate where CPU is being used.
And there's this huge starting chunk, okay, uh where we're only using one CPU. This consists
of um parsing, lexing, macro expansion, name resolution, even loading data from your target folder.
And I'd love to do something about that.
Um, in addition it seems that mo for the most part we are ending up with a lot of good CPU usage.
Um, but if you actually do the math okay, we're not really getting close.
Um, we're only we're not even using uh three quarters of all the CPU space that we had because there's so many single-threaded chunks.
Um, and so I want to find a way to use concurrent queries to unlock more parallelism.
Um, I think that if we could execute queries concurrently, you could also then execute them on multiple threads and you would give yourself a lot uh
many more opportunities to uh get work done in parallel.
So I've already implemented this in Capillary.
And it was fairly simple. We just turn everything async.
And somehow that works.
Um you literally just add an async keyword and now we can use all our standard async tools. So over here I'm using
async tools. So over here I'm using futures util and just letting it do stuff in parallel.
The additional benefit is now you can do IO inside your queries, which means that uh my cargo dependency resolution queries can actually fetch data from crates IO
while they're running.
This is something that's otherwise not possible in a crate system and that's I guess um one of the reasons why it'd be really hard to turn cargo into uh a tool that uses a crate system.
Um but we can do that.
So um my sort of overall approach here is um I really want to heavily lean on concurrency.
Um I think uh once you can do uh work concurrently, you can also start distributing things across threads.
Um it uh there's it opened up interesting opportunities for SIMD actually that we're not going to talk about.
Yeah yeah.
And um I'm really excited to see this work.
Um so there's still a bit more work to do.
Um at some point I'm going to stop using uh actual async language feature um because there's uh it it limits uh some
lifetimes actually. But um
lifetimes actually. But um yeah, this works.
Uh so to quickly recap um here are some of the major things I'm trying to do with Crabby. Um I want to
compile things uh simultaneously. I
don't want I I'm trying to remove some of those dependency edges we saw.
Um I want to get down to target folder sizes. That would be crazy.
sizes. That would be crazy.
And um I'd love to see like better use of that cache space. Um
and I hope we can make things more parallel.
to be clear, all of this is just laying groundwork for the future. Crabby is
mostly just a one woman team right now, um although I've received some work from some help from some lovely contributors.
And um I think there's a lot of good reasons to try to explore this.
Um, and so um I am putting out a call for help. Um, if
you have free time, um if you like compilers, uh if you like optimization, um there's a lot of work to do in Crabby and we're basically just building a a Rust compiler from scratch.
And I hope that uh that sounds like a interesting thing for a lot of you.
Um, but thank you very much. [applause]
[music]
Loading video analysis...