Hearth - an ecosystem for fast and user-friendly code-generating libraries (macros)

I took the liberty of creating this thread to post announcements on new developments in Hearth, Kindlings, and related libraries. :slight_smile:


What

Hearth is a library which I created to to help the community creating high quality fast user friendly libraries with macro-heavy code generation.

It already started to gather an ecosystem of macro-based libraries build on top of it:

  • Kindlings - faster and easier-to-debug derivations/replacements for: Avro4s, Circe Generic Extra, MacWire, Quicklens, Scala Mock, …with the same API for Scala 2.13 and Scala 3! (migration enablers!)
  • Chimney (since 2.0.0-M4) - a library for generating mappings e.g. between domain objects and API/DTO objects
  • Refined compat - compatibility layer that let you use compile-time literals validation on Scala 3 (cross-compiling with Scala 2 - another migration enabler!)
  • Difflcious - better diffs for your tests migrated to Hearth as its backend!
  • cats-eo (existential optics) - uses Hearth and Kindlings to derive optics

Users already report lower compilation times and unblocked migrations to Scala 3!

Why

In Scala, we use metaprogramming for all sort of things, line generating: JSON codecs, Avro codecs, domain-DTO coversions, Cats type classes (Eq, Order, Show, …), optics, mocks, dependency injection.

Historically people raised complaints that:

  • this process is slow to compile
  • this process generates slow code
  • and that it’s impossible to debug or figure out what kind of code is generated, to ensure that it is correct or performant

We also accumulated all cargo-cult rituals over the years:

  • that macros are slow… but Shapeless and Mirrors are not
  • that so-called automatic-derivation has to be slow and semiautomatic is always fast
  • that the only only way to implement these is via implicit shenanigans and every senior developer has to learn how to maintain and debug those

I’ve challenged all of these with my presentation and benchmarks to show how principled macros can be used to deliver
fast-to-compiler and fast-to-run code that can be easily debugged and understand, with no need for 2 separate implicit modes. I argued, that
the way most type-class derivations we have in Scala is flawed by design, and that we can do better.

However, macros were still hard and while some people got convinced that sanely-automatic derivation is a way, they couldn’t implement it.

How

That’s why I introduced Hearth during Scala Days 2025,
and expanded on it during Scalar 2026.

Hearth was designed to make 80% of typical work, that needs to happen in macro, at a 20% difficulty, while maintaining a shared logic for Scala 2 macros and Scala 3 macros!

To demonstrate, that it can be used in practice (and how it can be used):

  1. I’ve re-implemented several derivations: Circe’s Generic Extra, Jsoniter Scala, Avro4s, Cats’ Kittens, Quicklens, Scala Mock, Macwire, … and more in Kindlings.
    I’m using benchmarks to make sure that all of these are about as performant (Jsoniter Scala is a challenge!) or more performant that the originals (virtually everything else) in runtime.
    Each derivation uses sanely-automatic derivation approach to keep derivation logic lean and easy to debug: we’re aggregating errors and providing logs and final code on demand!
  2. Provided a compatibility layer for Refined library - Refined compat allows you to replace 1 import,
    that provided compile-time validation of literals, and your code can be running on both Scala 2.13 and Scala 3 (without it, you have to give up the compiletime literals validation)
  3. Now, the newest version of Chimney - 2.0.0-M4, was also migrated to use Hearth as its backend

I decided to start this thread since today I am announcing a release of the newest versions of:

Basically: migrating Chimney to Hearth gave us the opportunity to measure compile-time before/after migration, benchmark the resulting code
and use that feedback to improve all libraries that use Hearth as its backend. So these new released bring mostly performance improvements
and a few bufixes. :slight_smile:

7 Likes