Repository navigation
Avoid copying the rest of the PE section when reading field RVA data - #136961
Merged
Merged
Conversation
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.
|
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. |
Contributor
|
Tagging subscribers to this area: @dotnet/crossgen-contrib |
MichalStrehovsky
approved these changes
Nov 4, 2026
jkotas
approved these changes
Nov 4, 2026
Contributor
There was a problem hiding this comment.
🟢 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
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.
EcmaFieldExtensions.GetFieldRvaDataread field RVA data withPEReader.GetSectionData(rva).GetContent().GetSectionDatareturns a block that starts at the RVA and extends to the end of the containing section, and the parameterlessGetContent()copies that entire block into a newImmutableArray<byte>. The method then copied justsizebytes 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 otherGetSectionDatacallers in the tools already use this pattern (for example,Win32Resources/ResourceData.cs).GetFieldRvaDatais reached from three places:FieldRvaDataNode.GetDataduring object emissionTypePreinitduring static constructor preinitializationCorInfoImpl.TryReadRvaFieldData(getStaticFieldContent) while the JIT compiles methods, in both ILC and crossgen2