Skip to content

perf: encode only the path in ItemWithPath instead of building request information - #3234

Open
Ketan vijayvargiya (ketanvijayvargiya) wants to merge 2 commits into
microsoftgraph:mainfrom
ketanvijayvargiya:perf/itemwithpath-skip-request-information
Open

Ketan vijayvargiya (ketanvijayvargiya) wants to merge 2 commits into
microsoftgraph:mainfrom
ketanvijayvargiya:perf/itemwithpath-skip-request-information

Conversation

@ketanvijayvargiya

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

Copy link
Copy Markdown

Changes proposed in this pull request

  • ItemWithPath (both overloads) called ToGetRequestInformation(), expanded the URL template into a full request URI, appended the path through UriBuilder, built a Uri and split the whole URL on :. All of that was only used to URL-encode the path argument; the request information itself was thrown away.
  • The path is now encoded on its own in a small private EncodePath helper. It uses the same encoder as before (UriBuilder escapes the path when it is assigned) and keeps the same "last non-empty : separated part" rule, so the produced URL template is unchanged.
  • Only src/Microsoft.Graph/Extensions/DriveItemRequestBuilderExtensions.cs is touched. No generated code and no public API change.

One behaviour note: ItemWithPath no longer builds the request URI, so a builder whose URI cannot be resolved would now fail when the request is created or sent instead of inside ItemWithPath.

Same output

A console check ran both overloads over 299,143 random paths (spaces, %, #, ?, :, \, quotes, brackets, non-ASCII, tabs) against main and against this branch and hashed the resulting UrlTemplate and URI of a child builder for every path. Both builds give the same SHA-256, and neither throws for any of the paths. This was run on .NET 10 only.

Benchmark

BenchmarkDotNet 0.15.2, [MemoryDiagnoser], .NET 10.0.14, x64, Windows 11, Release build of Microsoft.Graph (net10.0), main vs this branch.

_client = new GraphServiceClient(new AnonymousAuthenticationProvider());
_root = _client.Drives["d"].Root;
_item = _client.Drives["d"].Items["i"];
Benchmark main This PR Time Allocated main Allocated this PR
_root.ItemWithPath("a/b/c") 1,642 ns 183 ns -89% 4.86 KB 640 B
_item.ItemWithPath("a/b/c") 1,739 ns 195 ns -89% 5.17 KB 704 B
_client.Drives["d"].Root.ItemWithPath("a/b/c").Content 1,896 ns 413 ns -78% 6.36 KB 2.13 KB

These are builder-construction costs in nanoseconds; next to the HTTP call that follows they are small in absolute terms. The gain matters mostly for code that builds many path-based requests, for example when filling batches.

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": 106 passed, 0 failed, 0 skipped on net8.0 and on net10.0 (96 existing tests plus 10 new cases). The Functional tests were not run because they need a live tenant.
  • Added two theories to DriveItemRequestBuilderExtensionsTests, one per ItemWithPath overload, for paths that contain colons (a:b, a:b::, /::, folder:name/file.txt, item/with/path:). Each asserts the request URL and the Children builder URL. The expected values were taken from main. No existing test was changed.

Other links

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.

🟡 Changes recommended

The trailing-colon trimming and last-segment behavior lack regression coverage for colon-containing paths through both ItemWithPath overloads.

1 open issue
What changed in this PR

This PR reduces construction overhead for path-based drive-item requests by encoding the path directly, without changing public API signatures or generated code.

Changes:

  • Replaces full request-URI construction in both ItemWithPath overloads with a shared private EncodePath helper.
  • Retains the last non-empty colon-separated segment rule.
  • Defers failures from unresolvable builder URIs until request creation or sending.
File Description
src/​Microsoft.Graph/​Extensions/​DriveItemRequestBuilderExtensions.cs Adds path-only encoding and uses it in both ItemWithPath overloads.

Review effort: Balanced

Comment on lines +73 to +75
while (end > 0 && encoded[end - 1] == ':')
end--;
var start = end == 0 ? 0 : encoded.LastIndexOf(':', end - 1) + 1;

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Added in 0e6c3cb: two theories, one per ItemWithPath overload, covering a:b, a:b::, /::, folder:name/file.txt and item/with/path:. Each asserts the request URL and the Children builder URL. The expected values were taken from main, so they pin the previous behaviour.

Note that for a path such as a:b only b is kept. That is how main behaves today and this PR does not change it; I can open a separate issue if it is not intended.

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