Repository navigation
[bgen] Cache raw GetCustomAttributesData() results per provider - #25564
Conversation
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>
There was a problem hiding this comment.
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-
AttributeManagercache mappingICustomAttributeProviderβ rawCustomAttributeDatalist. - Convert
GetAttributes()from static to instance so it can use the cache. - Convert
HasAttribute (ICustomAttributeProvider, string)from static to instance (call sites already use anAttributeManagerinstance).
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
/azp run |
|
Azure Pipelines successfully started running 3 pipeline(s). |
β [PR Build #5c70999] Build passed (Detect API changes) βPipeline on Agent |
β API diff for current PR / commitNET (empty diffs)β API diff vs stableNET (empty diffs)βΉοΈ Generator diffGenerator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes) Pipeline on Agent |
This comment has been minimized.
This comment has been minimized.
β [PR Build #5c70999] Build passed (Build packages) βPipeline on Agent |
β [PR Build #5c70999] Build passed (Build macOS tests) βPipeline on Agent |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
π [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 macOS testsβ
Tests on macOS Monterey (12): All 5 tests passed. Html Report (VSDrops) Download Linux Build VerificationPipeline on Agent |
MetadataLoadContext's
GetCustomAttributesData()returns a freshReadOnlyCollection<CustomAttributeData>on every call. Since bgen queries multiple attribute types per provider (viaGetCustomAttributes<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. ChangedGetAttributes()from static to instance to enable this cache.HasAttribute(string)also changed from static to instance (only one caller, already via instance).