I like cabal a lot. I think it’s a pretty good build tool. And the standardisation around the packaging specification format is great. By and large, it does the job.

However, cabal (like many language-specific build tools) can struggle with what I’ll call gnarly codebases. A gnarly codebase tends to have many of the following features:

  1. It is very large.
  2. It changes very frequently.
  3. It has code split across many packages and components.
  4. It has a “messy” dependency graph. A messy dependency graph is one in which the modules and components are not “neatly” layered up, and changes often span many components.

Gnarly codebases get that way organically, and they’re most commonly found inside corporations. However, OSS codebases can definitely get gnarly! For example, the HLS codebase is (in my opinion) fairly gnarly. I’m going to mostly be focussing on OSS codebases here, but I’m hoping that what we’ve produced will be useful in industry too (we’re starting to use it at CircuitHub, for example).

cabal starts to creak on gnarly codebases.

  • On large codebases
    • no-op builds are slow
    • clean builds are slow
  • On rapidly changing codebases
    • rebasing is frequent, and tends to invalidate most local compilation results, leading to large rebuilds
  • On component-ized codebases
    • changes to a single component force complete rebuilds of downstream components
  • On messy codebases
    • changes are not well localised and tend to sprawl across component boundaries, leading to large rebuilds

Can we do better? Yes we can. By replacing cabal’s built-in build system, we’re able to get very substantial speed improvements. On the CircuitHub codebase, we see approximately 2x speedups on clean builds, and 10x+ improvements on incremental builds (depending on what you’re doing). In some cases incremental rebuilds can even beat :reload in ghci! Later I’ll show some more extensive results from some open-source projects.

Replacing cabal’s build system

cabal can be thought of as having two layers:

  1. “Configuring” a project. This means solving dependencies, applying configuration, working out how to build each component.1
  2. “Building” a project. Actually going through and using ghc to turn those components into interfaces, object files, etc.

The problems with gnarly codebases are basically all problems with the building part of cabal. This is not a problem unique to cabal. Many language-specific build tools struggle with being a really good build tool – it’s not a trivial problem! cabal has had a decent amount of work put in here (e.g. the support for semaphores has improved its cross-package parallelism quite a bit), but it still falls short of what should be possible.

So the question is: can we retain the configuration part of cabal, which lets us declare our project and packages in the way we always have done, while actually building them more efficiently? This is the idea of Simon Marlow’s cabal-buck2 project. It uses cabal to configure a project, and you can then build it with buck2 using the rules in haskell-buck2.2 cabal buck2 is a cabal “external command” that produces build files that are then used by buck2.

buck2

Why buck2? buck2 has a bunch of properties we want:

  • It’s very fast
  • It has some fancy build system features that will turn out to be useful
  • It has good support for various kinds of build incrementality
  • It has a good system for build caching and remote building
  • It isn’t tied to any particular language so is well-suited to polyglot codebases

Using buck2 to build Haskell projects is not a new idea. Notably Mercury (with Tweag) have done a lot of work in this direction. A lot of the ideas that I’ve added to cabal-buck2 are, uh, borrowed from their implementation. See Andreas Herrmann’s HIW talk, and then Ian-Woo Kim’s HIW talk from a year later for a much more in-depth discussion of the underlying ideas than I’m going to give here! I also recommend The postmodern build system for a good overview of the landscape.

The Mercury buck2 implementation is public, but it has a few problems:

  • Not primarily targeting the open-source ecosystem. It’s a mirror of a very active internal project. It is likely to be driven largely by their internal needs, which isn’t quite what we want for an OSS project.
  • Narrow GHC support. Not only does it only really support one or two versions of GHC, it has to be a patched GHC!
  • No cabal-based project generation
  • Different performance/complexity tradeoffs

So we’ve ended up with a cut-down clean-room implementation of a lot of the same ideas, but:

  1. It’s a primarily OSS project, aimed to be very easily usable by any OSS Haskell codebase
  2. It has very wide (tested!) GHC support: we support everything from GHC 9.6-9.14, with no patches required
  3. It has much lighter-weight setup (you don’t need to provide your own worker binary)

Our buck2 rules are also intended to support some slightly unusual use cases, such as building GHC itself (which is a rather special project).

Module-level granularity

Apart from the general nice properties of buck2, the key thing that solves a lot of the problems with gnarly codebases is making the build graph work at the module level. Package and component boundaries force a lot of waiting in cabal builds. Within a single component, builds are done with ghc --make, which already does a pretty good job. Indeed, as we will see later, on projects that consist of just a few very large packages, cabal already does quite well (at least for clean builds).

If we are able to break down the component-level boundaries, we can regain a lot of parallelism, since we can just use whatever the “true” dependency graph given by the modules is. The dream goal is to be able to say that it doesn’t matter how you divide up your modules between packages and components, you should get equally fast and incremental builds regardless.

I’ve been pitching in to add this to cabal-buck2. It’s not conceptually too hard to do, following the basic ideas pioneered by Mercury:

  1. Build each module one-by-one using GHC’s one-shot mode
  2. Use a persistent worker (buck2 supports this) to avoid the fixed cost overheads associated with launching a GHC process each time

Doing this also really relies on a key feature of buck2, which is that it supports dynamic action graphs. That is, we can turn a static target (“build library L”) into a dynamic graph of actions (“build module M”, “build module N”) as part of the build, which means we can dynamically construct the module dependency graph by asking GHC what it is!

The other nice feature of buck2 that we rely on quite a bit is the ability for actions to produce “dep-files” that specify which of their inputs they actually used. The action will then only be re-run if one of those inputs changes. This means that we can refine dependencies even more after we build for the first time. This means that e.g. transitive dependencies which aren’t actually used can be ignored, letting us halt recompilation sooner.

The worker

There are a lot of possible design options for the worker. Mercury’s is quite sophisticated. Ours is designed to be dependency-free (so it can be built transparently by buck2 as part of project setup), and to minimize memory usage while still getting decent performance.

The main trickiness is around memory. Initially, we kept one GHC “session” around per component that we were building, set up with all the flags and interfaces it needs. But module-level granularity means that you can end up with many components in-progress, and this leads to high and unpredictable memory usage.

In the end, we have been able to reduce this a lot by sharing almost everything between sessions. We can do this because buck2 workers live only for a single build, and so we know that a single worker will never need to see two different versions of a given interface or shared library.3

The worker is also a nice case for property-based testing. Since we have a canonical implementation that it’s supposed to match (ghc -c), we can generate scenarios and check that we get byte-identical output both ways.

Extra metadata

There are two places we need some information that cabal doesn’t currently give us:

  1. We don’t know which build-depends dependencies are used for Template Haskell. A dependency being used for TH is especially expensive since we need its full shared library, not just its interfaces. So it would be useful to know which ones are really needed.
  2. We don’t know which build-tool-depends executables are used at build time vs runtime – cabal puts them on the PATH in both cases! For annoying buck2 reasons, we end up building tools that are used at build time twice, so it is helpful to know if a tool is instead used at runtime only, as is common for tests.

These are arguably missing fields in the Cabal package spec, and so we’ve opted to let users specify them with Cabal extension fields, x-buck2-splice-depends and x-buck2-run-tool-depends. Hopefully we can get them added to Cabal properly in due course!

Template Haskell

Template Haskell is, as usual, a pain to deal with.

For cross-package TH, GHC expects to have the linked shared library for the package available to look up symbols from. This means that if a module M in package A exports a TH function f, and module N uses f, then module N effectively depends on the whole of A, including the step of linking it into a shared library. This is much worse than the usual case of depending on a function from M, where we only need M’s interface in order to start compiling N.

We don’t have a clean solution to this yet. In principle we should be able to make do with just the object code for M – GHC does this in --make mode for home modules – but it’s not trivial to do across components. For now we rely on users refining their Template Haskell dependencies (with x-buck2-splice-depends) to avoid depending on more shared libraries than necessary.

We do already use the module-level dependencies to help a bit here. With ExplicitLevelImports in GHC 9.14+, we can identify exactly which packages a module uses for TH. That means that we can start the module as soon as all of its specific library dependencies are built, rather than waiting for all the ones that are used anywhere in the component.

Caching

buck2’s caching is a bit more thorough than cabal’s, which means we get some nice things for free:

  1. Making a fresh worktree of a Haskell codebase can get a warm cache (with cabal, even if you copy over dist-newstyle, it won’t be usable since it’s not relocatable)
  2. If you switch compilation modes or flags (e.g. to turn on profiling) you will get cache hits when you return to previously used configurations (cabal will rebuild your local packages each time)

The one downside is that surprisingly buck2 does not do any persistent caching unless you are running a local cache daemon. Otherwise it will keep things only in the memory of the local buck2 daemon, which will be lost if e.g. you restart your computer. Running a local cache daemon like bazel-remote is not hard, but it is an additional step that you need to do to really be able to use buck2 as you would expect.

buck2’s caching is much less hermetic than other things you might be used to (like Nix). It does sometimes assume that e.g. a binary with the same name is the same binary, which is not necessarily true (cabal’s local caching does the same!). If you use Nix-provided tools, then the store paths will appear in the action digests, which should be pretty solid, but I’m not yet sure I trust that it’s hermetic enough to be safely used across heterogeneous developer machines, or at least we need to think carefully about the caveats. I suspect it’s fine for e.g. sharing a cache across GitHub Actions runs or similar.

Results

Enough yapping! How does it perform? Really very well. The following tables show the results of various scenarios. They’re just run on my laptop (GHC 9.12, 8 jobs), so shouldn’t be taken too literally, but the overall picture is clear.

There are a number of scenarios:

  • generate is running cabal buck2 to generate the build configuration
  • clean is a clean build of the whole project
  • noop is a noop build
  • hub-comment is a build after adding a comment to a “hub” module in the dependency graph
  • hub-comment[interfaces] is the same but using the [interfaces] sub-target we added which skips linking (i.e. good for typechecking only)
  • hub-abi is a build after making an ABI-changing edit to the same “hub” module
  • hub-abi[interfaces] is the same using [interfaces]
  • leaf is a build after making an edit to a leaf module
  • leaf[interfaces] is the same using [interfaces]

Each was built several ways:

  • cabal is using cabal build
  • buck2-component is using buck2 with component-level granularity. This is an intermediate point - using buck2 but without the module-level support.
  • buck2-module is using buck2 with module-level granularity
  • repl is using a cabal multi-repl and :reload-ing

amazonka

amazonka is something of a worst-case for cabal buck2, it has a few very large packages. In many ways it is not that gnarly, just big! So cabal already does a pretty good job here. This benchmark is only on a subset of amazonka, but it includes the massive EC2 package.

amazonka build times

  cabal buck2-component buck2-module repl
generate   5.7s 5.6s  
clean 139.5s 182.2s 118.0s 89.6s
noop 3.3s 0.0s 0.0s 1.6s
hub-comment 17.9s 9.4s 2.4s 1.7s
hub-comment [interfaces]     2.5s  
hub-abi 246.0s 238.6s 217.5s 69.7s
hub-abi [interfaces]     192.0s  
leaf 6.9s 0.5s 0.4s 1.6s
leaf [interfaces]     0.5s  

haskell-language-server

HLS is quite a gnarly codebase! Note that the leaf module was not an executable so [interfaces] was no different.

Many of the cases where buck2 doesn’t do great here are due to unfortunate arrangements of Template Haskell, which I think it would usually be possible to unpick and get much better performance. HLS exports some TH functions from its large ghcide package, so anything which uses the TH from ghcide effectively gains a dependency on the whole ghcide component. Splitting the TH out to its own component would entirely solve this.

HLS build times

  cabal buck2-component buck2-module repl
generate   14.9s 15.2s  
clean 255.4s 95.3s 71.3s 79.0s
noop 16.1s 0.0s 0.0s 3.8s
hub-comment 116.4s 13.1s 3.1s 5.9s
hub-comment [interfaces]     3.1s  
hub-abi 212.8s 62.9s 45.6s 10.8s
hub-abi [interfaces]     19.8s  
leaf 23.0s 2.8s 1.1s 3.7s

cabal

cabal is somewhat in the middle.

cabal times

  cabal buck2-component buck2-module repl
generate   4.5s 2.7s  
clean 108.6s 90.9s 70.0s 33.4s
noop 2.8s 0.0s 0.0s 0.8s
hub-comment 22.7s 7.0s 2.4s 1.3s
hub-comment [interfaces]     2.3s  
hub-abi 47.3s 30.5s 20.2s 4.9s
hub-abi [interfaces]     10.8s  
leaf 5.9s 0.2s 0.3s 0.8s
leaf [interfaces]     0.1s  

The upshot of this is:

  • module-level granularity is well worth it
  • buck2 has excellent noop performance
  • buck2-module is always faster for clean builds, and sometimes drastically faster, and almost always drastically faster for incremental builds
  • For fast feedback, buck2 with [interfaces] is often competitive with :reload in a multi-repl, and occasionally faster!

Beating the repl is a fun goal here. That shouldn’t really be possible – ghc --make does a pretty good job of scheduling work, and the repl should have significant work pre-cached in memory. It’s quite surprising to me that we are often able to rival it. Using buck2 directly is much more flexible than using a big multi-repl, since you don’t have to commit to the set of packages you want to work with, and you don’t have to hold all the memory when you’re not actively building. So if we can get reasonably competitive in terms of speed, I think that can make buck2 best-in-class for iterative development.

Conclusion

cabal-buck2 is usable today for open-source or commercial Haskell projects to get faster builds in development and CI. Give it a try and let us know how you get on!

  1. Historically, cabal actually had separate configure and build commands. But the decision was taken (sensibly) to make cabal configure-on-demand, so that users don’t have to think about it. But conceptually it’s still something that happens. ↩

  2. This is not a tremendously novel idea, for example haskell.nix does effectively this to turn a cabal project into Nix derivations, although in the end it builds those using the Cabal library’s build interface for maximum compatibility. ↩

  3. This is not quite true: some modules will be built with flags that change how interfaces are read. These do need their own sessions, so we still have some session splitting based on flag usage. ↩