Skip to content

Reduce memory allocations when creating SourceRepository instances - #7126

Merged
zivkan merged 1 commit into
devfrom
dev-zivkan-SourceRepository-allocations
Feb 12, 2026
Merged

zivkan merged 1 commit into
devfrom
dev-zivkan-SourceRepository-allocations

Conversation

@zivkan

@zivkan zivkan commented Feb 11, 2026

Copy link
Copy Markdown
Member

Bug

Fixes:

Description

As the title says, reduce allocations when creating SourceRepository instances. It takes a list of resource providers, groups them by the resource type, and creates a dictionary of type to list of providers.

I created a simple benchmark, and here are the results:

Method Mean Error StdDev Gen0 Gen1 Allocated
Original 11.890 us 0.1388 us 0.1298 us 2.7618 0.1984 45.19 KB
Changed 6.441 us 0.0906 us 0.0847 us 1.3657 0.0916 22.4 KB

roughly:

  • 20 KB improvement from removing the .OrderBy(..).ThenBy(..) LINQ expression, and using a list with the right initial capacity and then using List<>.Sort
  • 1KB improvement by creating the provider cache dictionary with the correct initial size
  • 1KB from removing the ordered queue/list, and sorting the items list a second time in-place
  • I forgot to check how much the unnecessary .ToArray() in the Init method was causing, but I guess this was most of the remaining memory savings, as there isn't really much else left in the PR.

PR Checklist

  • Meaningful title, helpful description and a linked NuGet/Home issue
  • Added tests
  • Link to an issue or pull request to update docs if this PR changes settings, environment variables, new feature, etc. N/A

@zivkan
zivkan merged commit e323999 into dev Feb 12, 2026
16 checks passed
@zivkan
zivkan deleted the dev-zivkan-SourceRepository-allocations branch February 12, 2026 10:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants