Skip to content

perf: cache reflection lookups in DriveItemRequestBuilderExtensions - #3233

Open
Ketan vijayvargiya (ketanvijayvargiya) wants to merge 1 commit into
microsoftgraph:mainfrom
ketanvijayvargiya:perf/cache-reflection-in-driveitem-extensions
Open

Ketan vijayvargiya (ketanvijayvargiya) wants to merge 1 commit into
microsoftgraph:mainfrom
ketanvijayvargiya:perf/cache-reflection-in-driveitem-extensions

Conversation

@ketanvijayvargiya

@ketanvijayvargiya Ketan vijayvargiya (ketanvijayvargiya) commented Nov 14, 2026 •

Copy link
Copy Markdown

Changes proposed in this pull request

  • DriveItemRequestBuilderExtensions finds the request builder's backing fields (request adapter, path parameters, URL template) with GetType().BaseType.GetFields(NonPublic | Instance).FirstOrDefault(...). That scan ran on every call of GetRequestAdapter, GetPathParameters, GetUrlTemplate and UpdateUrlTemplate, so every ItemWithPath(...) and every property read on CustomDriveItemItemRequestBuilder repeated it.
  • The FieldInfo is now looked up once per (builder base type, field type) and kept in a static ConcurrentDictionary. The four helpers call one shared GetBackingField method; the rest of their logic is unchanged (same field is picked, same null handling).
  • Only src/Microsoft.Graph/Extensions/DriveItemRequestBuilderExtensions.cs is 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 of Microsoft.Graph (net10.0) before and after this commit.

_client = new GraphServiceClient(new AnonymousAuthenticationProvider());

[Benchmark]
public object ItemWithPathContent() => _client.Drives["d"].Root.ItemWithPath("a/b/c").Content;
Version Run Mean StdDev Allocated
Before (main) 1 2.009 us 0.103 us 6.36 KB
After (this PR) 1 1.920 us 0.031 us 6.17 KB
Before (main) 2 2.024 us 0.032 us 6.36 KB
After (this PR) 2 1.911 us 0.025 us 6.17 KB

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 existing DriveItemRequestBuilderExtensionsTests. The Functional tests were not run because they need a live tenant.
  • No tests were added or changed.
Microsoft Reviewers: Open in CodeFlow

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.

🟢 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

@ketanvijayvargiya

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

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.

2 participants