Skip to content

Conversation

jkotas
Copy link
Member

@jkotas jkotas commented Oct 13, 2025

  • Introduce more efficient one-shot static method and updated callers in CoreLib to use it
  • Optimize bounds checks and allocations

Contributes to #45237

- Introduce more efficient one-shot static method and updated callers in
  CoreLib to use it
- Optimize bounds checks and allocations

Contributes to dotnet#45237
@Copilot Copilot AI review requested due to automatic review settings October 13, 2025 20:12
Copy link
Contributor

@Copilot 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 the Sha1ForNonSecretPurposes struct by introducing a more efficient one-shot static method and updating callers in CoreLib to use it. The optimization reduces allocations, eliminates unnecessary bounds checks, and streamlines the hashing process.

Key changes:

  • Added a new static HashData method that processes data in a single operation without maintaining instance state
  • Updated existing callers to use the new one-shot method instead of the multi-step Start(), Append(), Finish() pattern
  • Optimized internal implementation with better bounds checking and allocation patterns

Reviewed Changes

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

File Description
src/libraries/System.Private.CoreLib/src/System/Reflection/AssemblyNameHelpers.StrongName.cs Replaced multi-step SHA1 hashing with single HashData call
src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs Optimized GUID generation by using HashData and stack allocation for temporary buffers
src/libraries/Common/src/System/Sha1ForNonSecretPurposes.cs Added static HashData method, refactored internal methods for better performance, and improved bounds checking

@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Oct 13, 2025
@jkotas jkotas added area-System.Runtime tenet-performance Performance related issue and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Oct 13, 2025
Copy link
Contributor

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

@jkotas
Copy link
Member Author

jkotas commented Oct 13, 2025

@EgorBot -intel

using System.Reflection;
using BenchmarkDotNet.Attributes;

public class Bench
{
    static AssemblyName an = typeof(object).Assembly.GetName();

    [Benchmark]
    public byte[] GetPublicKeyToken() => ((AssemblyName)an.Clone()).GetPublicKeyToken();
}

@jkotas
Copy link
Member Author

jkotas commented Oct 13, 2025

Perf results

  • 1.6x throughput improvement on a GetPublicKeyToken microbenchmark above
  • Small (<0.5kB) NAOT binary size improvement

@MichalPetryka
Copy link
Contributor

  • Small (<0.5kB) NAOT binary size improvement

IIRC such changes are noise in the diffs due to PE section alignment being 4KB.

@jkotas
Copy link
Member Author

jkotas commented Oct 13, 2025

IIRC such changes are noise in the diffs due to PE section alignment being 4KB.

Right, the noise in MichalStrehovsky/rt-sz#193 suggests that there is a net binary size improvement that is smaller than 0,5kB.

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.

5 participants