Optimizations
The generator replaces what Spring does at run time with reflection: finding beans, creating them, reading their properties, proxying them for AOP. It writes plain Dart code for all of it at build time. This section explains the choices that keep that code small and the build fast.
Why
Generated code is code the app ships. A naive generator would describe every class completely, write a proxy class for every bean and put every starter module into every app. That works, but the app carries a lot of code that never runs, and on Flutter and the web that means a bigger download and a slower start.
The build sees the whole application: its beans, its aspects, its calls, its configuration. So it can leave out what the application provably does not use, and say so clearly when something it did not see turns up at run time.
Overview
| Optimization | What it saves | Page |
|---|---|---|
| No reflection | dart:mirrors is not used; everything is plain constructor calls and closures, which work in AOT, Flutter and on the web and survive minification |
How the generation works |
| Code the compiler can tree-shake | Generated references are direct (create: (r) => Greeter(r.dependency<Clock>(name: r'clock'))), descriptions are constants with top-level functions, conditional code is behind compile-time constants, so the Dart compiler removes what nothing reaches |
this page, below |
| Only what runs: introspection | A class is described only for what the application does with it: getters, constructor and setters, or methods | Only what runs: introspection |
| AOP proxy pruning | No proxy class for a bean that no advice can match, and only the advised members in the proxies that stay | AOP proxies |
| Deferred proxies | Starters write no proxy classes for their beans; the application writes only those its aspects need | AOP proxies |
| Modules left out of the build | A property of the application keeps a starter module (a debug panel) out of the app, or out of a release only | Modules left out of the build |
| Starter parts | The generator is a core plus one part per starter, and a part runs only when the application reaches its starter; a part can follow a build key like a module | How the generation works, Parts of the generator by key |
| Build speed | Files are checked by text before they are resolved, only the needed bodies are resolved, extra work runs only when it can remove something | Build speed |
Code the compiler can remove
Dart's compilers for release builds (dart2js, AOT) remove code nothing references: tree shaking. Reflection defeats it, because any class might be looked up by name. Generated code helps it:
- Direct references. A bean is created by a closure that calls its constructor. Nothing looks a class up by name, so a class no bean or call uses is removed.
- Constant descriptions. The introspection of a class without type parameters is a
const Introspectionwhose closures are top-level functions (_$get3,_$create5). A part the build left out has no function at all. - Constant conditions. A module that is only for debug and profile builds is listed under
if (!const bool.fromEnvironment('dart.vm.product')). In a release build that condition is the constantfalse, so the element, the module and everything only it reaches are removed. See Modules left out of the build. - Shared constants. Pruned proxies share one constant set of known advisors per module, and the reason text of
AopProxyFactory.pruned()is written once in the runtime, not in every generated file.
What stays the same
Every optimization keeps the behaviour of the application:
- A bean without matching advice is never wrapped at run time, whatever the AOP options.
- A part of a description that is missing makes the code that needs it fail with a message naming the class, the part and the fix (
add @Introspected() to Book), never with an empty object. - Advice the build did not see (registered in code) makes the startup fail with a message, instead of being skipped silently.
- A module left out of the build is exactly as if the starter did not have it.
Each optimization can be relaxed: @Introspected() describes a class completely, aop_prune: false keeps every proxy, and a build property set to true keeps a module in every build. See Options.