-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Optimize Sha1ForNonSecretPurposes #120674
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
- Introduce more efficient one-shot static method and updated callers in CoreLib to use it - Optimize bounds checks and allocations Contributes to dotnet#45237
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 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 |
Tagging subscribers to this area: @dotnet/area-system-runtime |
@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();
} |
Perf results
|
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. |
Contributes to #45237