Skip to content

Prevent garbage collection when iterating empty Simple Injector collections #1020

Closed

Description

@dotnetjunkie

Consider the following class:

public sealed class LoggerComposite(IEnumerable<ILogger> loggers) : ILogger
{
    public void Log(LogEntry entry)
    {
        foreach (var logger in loggers) logger.Log(entry);
    }
}

In Simple Injector v5.5, iterating the collection loggers always causes a new enumerator to be created, thus causing garbage collection, even when the collection of loggers is empty.

Because the injected collection is (in most cases) a Simple Injector-defined type (i.e. the internal ContainerControlledCollection<T>), it allows returning a single default enumerator.

This optimization is especially useful in scenarios the majority of cases contains no elements to iterate. This becomes more obvious when the iterated element itself is generic, for instance:

public sealed class CompositeBusinessRule<TEntity>(IEnumerable<IBusinessRule<TEntity>> rules)
    : IBusinessRule<TEntity>
{
    public void Validate(TEntity entity)
    {
        foreach (var rule in rules) rule.Validate();
    }
}

If most of the rules in the system contain no implementations, the Validate method is able to run without causing garbage collection on them.

Unfortunately, garbage collection can't be prevented in case a collection contains elements.

Activity

  1. added this to the v5.6 milestone on Sep 23, 2025
  2. added a commit that references this issue on Sep 23, 2025
    72cfb28
  3. added a commit that references this issue on Apr 16, 2026
    3e9c5f2
  4. modified the milestones: v5.6, v5.5.2 on Apr 21, 2026
  5. added a commit that references this issue on Apr 21, 2026
    ebe77c8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions