Skip to content

[bgen] Cache raw GetCustomAttributesData() results per provider - #25564

Merged
rolfbjarne merged 2 commits into
mainfrom
dev/rolf/bgen-perf-rawattr-cache
Jun 2, 2026
Merged

[bgen] Cache raw GetCustomAttributesData() results per provider#25564
rolfbjarne merged 2 commits into
mainfrom
dev/rolf/bgen-perf-rawattr-cache

Conversation

@rolfbjarne

@rolfbjarne rolfbjarne commented May 28, 2026

Copy link
Copy Markdown
Member

MetadataLoadContext's GetCustomAttributesData() returns a fresh ReadOnlyCollection<CustomAttributeData> on every call. Since bgen queries multiple attribute types per provider (via GetCustomAttributes<T>, HasAttribute<T>, HasAttribute(string), IsNullable), the same provider's raw attribute list was being allocated and discarded many times over.

Add a Dictionary<ICustomAttributeProvider, IList<CustomAttributeData>> that caches the raw result per provider. Changed GetAttributes() from static to instance to enable this cache. HasAttribute(string) also changed from static to instance (only one caller, already via instance).

Baseline average (3 runs): 24.7s, 9164 MB total, gen0=1660, gen1=513
After average (3 runs):    22.1s, 6990 MB total, gen0=1265, gen1=417
Time:   -2.6s (-10.5%)
Memory: -2174 MB (-23.7%)
GC:     gen0 -395 (-23.8%), gen1 -96 (-18.7%)

MetadataLoadContext's GetCustomAttributesData() returns a fresh
ReadOnlyCollection<CustomAttributeData> on every call. Since bgen queries
multiple attribute types per provider (via GetCustomAttributes<T>,
HasAttribute<T>, HasAttribute(string), IsNullable), the same provider's
raw attribute list was being allocated and discarded many times over.

Add a Dictionary<ICustomAttributeProvider, IList<CustomAttributeData>>
that caches the raw result per provider. Changed GetAttributes() from
static to instance to enable this cache. HasAttribute(string) also
changed from static to instance (only one caller, already via instance).

Baseline average (3 runs): 24.7s, 9164 MB total, gen0=1660, gen1=513
After average (3 runs):    22.1s, 6990 MB total, gen0=1265, gen1=417
Time:   -2.6s (-10.5%)
Memory: -2174 MB (-23.7%)
GC:     gen0 -395 (-23.8%), gen1 -96 (-18.7%)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 28, 2026 10:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes bgen’s reflection attribute access by caching GetCustomAttributesData() results per ICustomAttributeProvider, avoiding repeated allocations when multiple attribute queries are performed for the same provider during generation.

Changes:

  • Add a per-AttributeManager cache mapping ICustomAttributeProvider → raw CustomAttributeData list.
  • Convert GetAttributes() from static to instance so it can use the cache.
  • Convert HasAttribute (ICustomAttributeProvider, string) from static to instance (call sites already use an AttributeManager instance).

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@rolfbjarne
rolfbjarne enabled auto-merge (squash) May 28, 2026 17:28
@rolfbjarne

Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 3 pipeline(s).

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

✅ [PR Build #5c70999] Build passed (Detect API changes) ✅

Pipeline on Agent
Hash: 5c709999a42bce321a7f7b24ab6d2de65a42194a [PR build]

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

✅ API diff for current PR / commit

NET (empty diffs)

✅ API diff vs stable

NET (empty diffs)

ℹ️ Generator diff

Generator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes)

Pipeline on Agent
Hash: 5c709999a42bce321a7f7b24ab6d2de65a42194a [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

✅ [PR Build #5c70999] Build passed (Build packages) ✅

Pipeline on Agent
Hash: 5c709999a42bce321a7f7b24ab6d2de65a42194a [PR build]

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

✅ [PR Build #5c70999] Build passed (Build macOS tests) ✅

Pipeline on Agent
Hash: 5c709999a42bce321a7f7b24ab6d2de65a42194a [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

🚀 [CI Build #5c70999] Test results 🚀

Test results

✅ All tests passed on VSTS: test results.

🎉 All 193 tests passed 🎉

Tests counts

✅ cecil: All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (iOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (MacCatalyst): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (macOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (Multiple platforms): All 1 tests passed. (⚠️ Html Report Publish failed ⚠️) Download
✅ dotnettests (tvOS): All 1 tests passed. Html Report (VSDrops) Download
✅ framework: All 2 tests passed. Html Report (VSDrops) Download
✅ fsharp: All 4 tests passed. [attempt 3] Html Report (VSDrops) Download
✅ generator: All 5 tests passed. Html Report (VSDrops) Download
✅ interdependent-binding-projects: All 4 tests passed. Html Report (VSDrops) Download
✅ introspection: All 6 tests passed. Html Report (VSDrops) Download
✅ linker (iOS): All 11 tests passed. Html Report (VSDrops) Download
✅ linker (MacCatalyst): All 11 tests passed. Html Report (VSDrops) Download
✅ linker (macOS): All 11 tests passed. Html Report (VSDrops) Download
✅ linker (tvOS): All 11 tests passed. Html Report (VSDrops) Download
✅ monotouch (iOS): All 20 tests passed. Html Report (VSDrops) Download
✅ monotouch (MacCatalyst): All 23 tests passed. [attempt 3] Html Report (VSDrops) Download
✅ monotouch (macOS): All 23 tests passed. [attempt 5] Html Report (VSDrops) Download
✅ monotouch (tvOS): All 20 tests passed. Html Report (VSDrops) Download
✅ msbuild: All 2 tests passed. Html Report (VSDrops) Download
✅ sharpie: All 1 tests passed. Html Report (VSDrops) Download
✅ windows: All 3 tests passed. Html Report (VSDrops) Download
✅ xcframework: All 4 tests passed. Html Report (VSDrops) Download
✅ xtro: All 1 tests passed. Html Report (VSDrops) Download

macOS tests

✅ Tests on macOS Monterey (12): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Ventura (13): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Sonoma (14): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Sequoia (15): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Tahoe (26): All 5 tests passed. Html Report (VSDrops) Download

Linux Build Verification

Linux build succeeded

Pipeline on Agent
Hash: 5c709999a42bce321a7f7b24ab6d2de65a42194a [PR build]

@rolfbjarne
rolfbjarne merged commit 194f285 into main Jun 2, 2026
55 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants