Skip to content

[object writer] Use binary search for seeking SectionData positions - #132089

Draft
adamperlin wants to merge 1 commit into
dotnet:mainfrom
adamperlin:adamperlin/sectiondata-binary-search
Draft

[object writer] Use binary search for seeking SectionData positions#132089
adamperlin wants to merge 1 commit into
dotnet:mainfrom
adamperlin:adamperlin/sectiondata-binary-search

Conversation

@adamperlin

Copy link
Copy Markdown
Contributor

Store buffer offsets separately and use them to locate stream positions in logarithmic time in ObjectWriter\SectionData.cs. Note that from my testing this does add about 2.25 MB overhead working memory to a System.Private.CoreLib R2R compilation (out of 700+ total MB) but hugely reduces a perf trap that could be hit by random seeks over SectionData. This came up as part of my work on #132029; I definitely understand if we don't want the added overhead, but this wasn't too difficult to improve so I thought I'd put up a fix just in case.

Store buffer offsets separately and use them to locate stream positions in logarithmic time. Add targeted ObjectWriter coverage for mixed and empty buffers.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: b522a420-c4c0-4c17-a82f-45a026f6a64e
Copilot AI lite review requested due to automatic review settings August 10, 2026 17:44
@adamperlin adamperlin changed the title Use binary search for SectionData positions [object writer] Use binary search for SectionData positions Aug 10, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@adamperlin adamperlin changed the title [object writer] Use binary search for SectionData positions [object writer] Use binary search for seeking SectionData positions Aug 10, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib
See info in area-owners.md if you want to be subscribed.

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 random seeking within SectionData’s read stream by maintaining a parallel list of per-buffer starting offsets and using a binary search to map a requested stream position to the correct underlying buffer in O(log N) time (instead of walking buffers linearly). This targets a potential perf trap in workloads that perform frequent random seeks during object writing.

Changes:

  • Track per-buffer starting offsets in SectionData and use an “upper bound” binary search to resolve stream positions efficiently.
  • Add a regression test that exercises seeking across many small/empty buffers plus appended data.
  • Expose internals to the test assembly via InternalsVisibleTo.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
src/coreclr/tools/Common/Compiler/ObjectWriter/SectionData.cs Add _bufferOffsets and switch ReadStream.Position seek logic to binary search over offsets.
src/coreclr/tools/aot/ILCompiler.Compiler/ILCompiler.Compiler.csproj Add InternalsVisibleTo to allow test access to internal types like SectionData.
src/coreclr/tools/aot/ILCompiler.Compiler.Tests/ObjectWriterTests.cs New test validating correct Position/ReadByte behavior across many buffers and appended data.
src/coreclr/tools/aot/ILCompiler.Compiler.Tests/ILCompiler.Compiler.Tests.csproj Include the new ObjectWriterTests.cs in the test project build.

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.

2 participants