- 
                Notifications
    
You must be signed in to change notification settings  - Fork 5.2k
 
          [SRM] Deduplicate and optimize BlobWriterImpl.WriteConstant.
          #121223
        
          New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
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.
| 
           Tagging subscribers to this area: @dotnet/area-system-reflection-metadata  | 
    
There was a problem hiding this 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 
WriteScalarConstantmethod that writes scalar constants to aSpan<byte>and returns the number of bytes written - Refactored 
GetOrAddConstantBlobto use stack allocation for scalar values - Updated both 
WriteConstantoverloads to callWriteScalarConstantfor 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 | 
| /// <param name="bytes">The span where the content will be encoded.</param> | ||
| /// <param name="value">The constant value.</param> | 
    
      
    
      Copilot
AI
    
    
    
      Oct 31, 2025 
    
  
There was a problem hiding this comment.
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).
| /// <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> | 
| Span<byte> bytes = stackalloc byte[sizeof(ulong)]; | ||
| int written = WriteScalarConstant(bytes, value); | ||
| writer.WriteBytes(bytes.Slice(0, written)); | 
    
      
    
      Copilot
AI
    
    
    
      Oct 31, 2025 
    
  
There was a problem hiding this comment.
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.
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.