Skip to content

Conversation

@teo-tsirpanis
Copy link
Contributor

The logic of writing scalar constants was extracted to its own method that writes to a span. This method also got used by MetadataBuilder.GetOrAddConstantBlob, avoiding the use of pooled blob builders when writing scalar constants.

The logic of writing scalar constants was extracted to its own method that writes to a span.
This method also got used by `MetadataBuilder.GetOrAddConstantBlob`, avoiding the use of pooled blob builders when writing scalar constants.
Copilot AI review requested due to automatic review settings October 31, 2025 00:03
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Oct 31, 2025
@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-reflection-metadata
See info in area-owners.md if you want to be subscribed.

Copy link
Contributor

Copilot AI left a comment

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 constant blob encoding by eliminating unnecessary allocations. It refactors the GetOrAddConstantBlob method and the WriteConstant methods to use stack-allocated buffers instead of pooled BlobBuilder instances for scalar (non-string) constants.

Key Changes:

  • Introduced a new WriteScalarConstant method that writes scalar constants to a Span<byte> and returns the number of bytes written
  • Refactored GetOrAddConstantBlob to use stack allocation for scalar values
  • Updated both WriteConstant overloads to call WriteScalarConstant for non-string values

Reviewed Changes

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

File Description
MetadataBuilder.Heaps.cs Refactored GetOrAddConstantBlob to use stack-allocated buffer and new WriteScalarConstant method
BlobWriterImpl.cs Added new WriteScalarConstant method and refactored existing WriteConstant overloads to use it

Comment on lines +151 to +152
/// <param name="bytes">The span where the content will be encoded.</param>
/// <param name="value">The constant value.</param>
Copy link

Copilot AI Oct 31, 2025

Choose a reason for hiding this comment

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

The documentation should clarify that bytes must be at least sizeof(ulong) bytes in length to accommodate the largest scalar type. Additionally, it should document the expected behavior when value is null (returns 4 bytes for a zero uint).

Suggested change
/// <param name="bytes">The span where the content will be encoded.</param>
/// <param name="value">The constant value.</param>
/// <param name="bytes">
/// The span where the content will be encoded. Must be at least <c>sizeof(ulong)</c> bytes in length to accommodate the largest scalar type.
/// </param>
/// <param name="value">
/// The constant value. If <paramref name="value"/> is <c>null</c>, writes 4 bytes representing a zero <c>uint</c>.
/// </param>

Copilot uses AI. Check for mistakes.
Comment on lines +259 to +261
Span<byte> bytes = stackalloc byte[sizeof(ulong)];
int written = WriteScalarConstant(bytes, value);
writer.WriteBytes(bytes.Slice(0, written));
Copy link

Copilot AI Oct 31, 2025

Choose a reason for hiding this comment

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

The stack allocation size sizeof(ulong) is duplicated across multiple call sites (lines 259, 272, and in MetadataBuilder.Heaps.cs line 259). Consider defining a named constant like MaxScalarConstantSize to make the code more maintainable and self-documenting.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Reflection.Metadata 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.

1 participant