镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Avoid copying the rest of the PE section when reading field RVA data - #136961

Merged
jkotas merged 2 commits into
dotnet:mainfrom
filipnavara:ilc-fieldrva-copy
Nov 4, 2026
Merged

jkotas merged 2 commits into
dotnet:mainfrom
filipnavara:ilc-fieldrva-copy

Conversation

@filipnavara

@filipnavara filipnavara commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

EcmaFieldExtensions.GetFieldRvaData read field RVA data with PEReader.GetSectionData(rva).GetContent(). GetSectionData returns a block that starts at the RVA and extends to the end of the containing section, and the parameterless GetContent() copies that entire block into a new ImmutableArray<byte>. The method then copied just size bytes out of it.

So every call allocated and copied everything from the field to the end of its section. With many RVA fields in one large section (static array initializers, ReadOnlySpan<byte> literals, UTF-8 string literals), the total work grows quadratically with the size of the section.

This change reads only the bytes the field needs with GetReader(0, size).ReadBytes(size). The other GetSectionData callers in the tools already use this pattern (for example, Win32Resources/ResourceData.cs).

GetFieldRvaData is reached from three places:

  • FieldRvaDataNode.GetData during object emission
  • TypePreinit during static constructor preinitialization
  • CorInfoImpl.TryReadRvaFieldData (getStaticFieldContent) while the JIT compiles methods, in both ILC and crossgen2

PEMemoryBlock.GetContent() without arguments copies everything from the
field RVA to the end of the section. For assemblies with many RVA fields
in a large section this is quadratic; on a large app FieldRvaDataNode
GetData took 3.2s sequentially. Read only the bytes the field needs.
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 4, 2026
@azure-pipelines

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

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/crossgen-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.

🟢 Approval recommended

No active review issues remain for the change that reduces allocation and copying when reading field RVA data.

No open issues 🎉

What changed in this PR

This PR reduces unnecessary allocation and copying when compiler tools read field RVA data through the shared type system used by NativeAOT and crossgen2.

Changes:

  • Reads only the field’s required bytes instead of copying the remainder of its PE section.
  • Removes the intermediate copy while retaining the existing section-length validation.
File Description
src/​coreclr/​tools/​Common/​TypeSystem/​Ecma/​EcmaField.cs Uses a bounded reader in GetFieldRvaData to return only the requested field bytes.

Review effort: Balanced

@jkotas
jkotas merged commit 8c15663 into dotnet:main Nov 4, 2026
99 of 102 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-ReadyToRun community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants