Accelerating Dart code generation

Quick Overview

Dart code generation build times are significantly accelerated by implementing Dart 3.18's new language feature, Augmentations, which allows code generators like jsonserializable to reduce boilerplate by adding necessary functions directly to the original class part file, resulting in performance improvements up to 3.8x faster for incremental builds across various packages like builtvalue, freezed, and gorouterbuilder.

Key Points: Augmentations, a new Dart language feature, allow code generators to significantly reduce boilerplate by injecting code (like JSON serialization/deserialization methods) directly into the original class's part file. Performance benchmarks show substantial speedups: jsonserializable saw incremental build times improve by 3.8x faster using the new method compared to older buildrunner versions. Specific package performance improvements include builtvalue being 3.6x faster, freezed being 3.8x faster, and gorouterbuilder being 2.4x faster. The overhead of code generation is drastically reduced; for a project with 3,000 generated libraries, code generation runs twice as fast, and the build time graph shows a flatter curve for the newer buildrunner versions. Augmentations support parts with imports and primary constructors, further streamlining how generated code interacts with the original source code. The speaker demonstrated using and noted that the console output looks cleaner with the new approach.

Context: This video, presented by John Ryan from the Flutter team as part of 'Flutter Flight Plans,' focuses on major performance improvements coming to Dart code generation tools, particularly those relying on the buildrunner ecosystem. The core topic revolves around how upcoming language features in Dart, specifically Augmentations, will help reduce the boilerplate code generated for tasks like JSON serialization and data class creation, thereby speeding up the development cycle.

Detailed Analysis

The video details how upcoming Dart language features, specifically Augmentations, will dramatically speed up code generation processes, primarily benefiting packages like jsonserializable, builtvalue, freezed, and gorouterbuilder. The presenter shows that with the old method, generating JSON serialization/deserialization functions required writing boilerplate code in a separate part file ( ). With Augmentations, code generators can use the keyword to inject necessary methods directly into the original class's part file, bypassing the need for manual boilerplate or complex generated files for certain functions. This dramatically reduces the amount of code processed during compilation. Performance testing demonstrated this improvement: for a project with 100 libraries, the build time dropped from 105 seconds to 12 seconds on an M1 Mac when using the optimized code generation, confirming user reports of significant speed gains (up to 3.8x faster incremental builds). The presenter also highlights other upcoming features that support this trend: Parts with imports and Primary constructors, which simplify class definitions and boilerplate reduction further. The conclusion encourages developers to upgrade their package to benefit from these optimizations.

Raw markdown version of this recap