Skip to content

2.5.7

Latest

Choose a tag to compare

@NikolayPianikov NikolayPianikov released this 29 Oct 14:32
· 102 commits to master since this release

1. Faster binding of setups and faster builds

Every call that bound a setup without explicit type arguments, such as .Transient(() => ...) or .To(ctx => ...), made the C# compiler run type inference over all 378 overloads for 2 to 64 implementation types. Those overloads are now extension methods, so they are only looked up when no regular method applies.

  • The same code keeps working unchanged; only the binding speed changes.
  • Binding of a setup became several times faster, both in the generator and in the compilation of your project.
  • Type attribute bindings are looked up without walking member bodies, which shortens generation further.

2. Smaller generated code for shared dependencies

A graph whose instances share dependencies (a diamond) used to be written out once per dependency path, so the generated code could grow exponentially with the depth of the graph.

  • A transient instance used in several places is now created by a local function, so the code is written once; the function is marked for aggressive inlining, so the JIT can still keep those instances on the stack. Fixes #167: generated code grows exponentially with the depth of shared transient dependencies.
  • A shared instance is now created in one place even when its own declaration is small but the dependencies written together with it are not, which covers diamonds of per-resolve instances and of singletons without a lock.
  • Shared instances are also created once under accumulators such as Owned<T> of a disposable, including when the accumulator is declared in an enclosing scope, and inside the lambdas of consumer delegates that override values. Fixes #171: generation time and output explode when a graph with shared dependencies is reached through a Func<T1, T> delegate.
  • The new LocalFunctions hint (Auto, On, Off) controls when the code of an instance is moved to a local function: Auto (default) does it when repeating the code would be large, On whenever possible, and Off keeps the code in place except where a local function is required for correctness.

3. Collection bindings and tags

4. Setups, generated code and default values

5. New diagnostic: a stored lifetime built from a delegate argument

An argument of a generated Func<..., T> delegate rewrites the whole dependency subgraph of T. A singleton or scoped dependency inside that subgraph is created once, so it silently keeps the value of the first call, and when the same dependency is also reachable outside the delegate its value is ignored entirely. Fixes #168: a singleton reached through a Func<T1, T> delegate is built with the delegate argument of the first call.

  • The new DIW015 diagnostic reports this case without changing the generated code, so explicit ctx.Override calls stay silent.
  • Fix it by using transient or per-resolve dependencies for values that vary per call, by passing the value to the code that consumes it, or by resolving it from a root argument when it is constant for the whole root.

Closed issues

All eight issues were reported by EmilienDup with a minimal reproduction and a precise description of the observed and the expected behavior; thank you for the detailed analysis.

  • #164 - default value of an optional bool constructor parameter is emitted as True (CS0103)
  • #165 - the same using alias in two setup files is copied twice into the generated composition (CS1537)
  • #166 - a factory returning a tuple literal without element names is rejected for a named tuple contract (DIE011)
  • #167 - generated code grows exponentially with the depth of shared transient dependencies
  • #168 - a singleton reached through a Func<T1, T> delegate is built with the delegate argument of the first call
  • #169 - binding precedence is reversed through a chain of DependsOn setups
  • #170 - IEnumerable<IHandler<int>> injected into the default IHandler<int> binding contains the consumer itself
  • #171 - generation time and output explode when a graph with shared dependencies is reached through a Func<T1, T> delegate

Full Changelog: 2.5.6...2.5.7