Repository navigation
perf: cache reflection lookups in DriveItemRequestBuilderExtensions - #3233
Open
Ketan vijayvargiya (ketanvijayvargiya) wants to merge 1 commit into
Conversation
Ketan vijayvargiya (ketanvijayvargiya)
requested a review
from a team
as a code owner
November 14, 2026 01:51
Copilot started reviewing on behalf of
Ketan vijayvargiya (ketanvijayvargiya)
November 14, 2026 01:52
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues block approval of the backing-field lookup caching change.
No open issues 🎉
What changed in this PR
This PR reduces repeated reflection work in drive-item request builders by caching backing-field lookups. It changes no generated code or public API signatures.
Changes:
- Adds a thread-safe cache keyed by builder base type and field type.
- Routes all four backing-field helpers through the shared lookup method while retaining their existing field access logic.
| File | Description |
|---|---|
src/Microsoft.Graph/Extensions/DriveItemRequestBuilderExtensions.cs |
Caches reflection metadata to reduce repeated field scans and allocations. |
Review effort: Balanced
Author
|
@microsoft-github-policy-service agree |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes proposed in this pull request
DriveItemRequestBuilderExtensionsfinds the request builder's backing fields (request adapter, path parameters, URL template) withGetType().BaseType.GetFields(NonPublic | Instance).FirstOrDefault(...). That scan ran on every call ofGetRequestAdapter,GetPathParameters,GetUrlTemplateandUpdateUrlTemplate, so everyItemWithPath(...)and every property read onCustomDriveItemItemRequestBuilderrepeated it.FieldInfois now looked up once per (builder base type, field type) and kept in a staticConcurrentDictionary. The four helpers call one sharedGetBackingFieldmethod; the rest of their logic is unchanged (same field is picked, same null handling).src/Microsoft.Graph/Extensions/DriveItemRequestBuilderExtensions.csis touched. No generated code and no public API change.Benchmark
To be upfront: the end-to-end gain is small. Building the request builder chain is dominated by other work (
ToGetRequestInformation,UriBuilder, string handling), so caching the field lookup removes only a slice of it.BenchmarkDotNet 0.15.2,
[MemoryDiagnoser], .NET 10.0.14, x64, Windows 11, Release build ofMicrosoft.Graph(net10.0) before and after this commit.Roughly 5% less time and 3% less allocation (about 190 bytes) per call. The time difference is close to the noise of a single run; the allocation difference is the same in every run.
Testing
dotnet build Microsoft.Graph.sln -c Release: succeeded, 0 errors, all target frameworks (netstandard2.0, netstandard2.1, net8.0, net10.0).dotnet test tests/Microsoft.Graph.DotnetCore.Test -c Release --filter "FullyQualifiedName!~Functional": 96 passed, 0 failed, 0 skipped on net8.0 and on net10.0. This includes the existingDriveItemRequestBuilderExtensionsTests. The Functional tests were not run because they need a live tenant.Microsoft Reviewers: Open in CodeFlow