Replace .Clone() with explicit array allocation and copy in Arrays#662
Open
paulomorgado wants to merge 1 commit intobcgit:masterfrom
Open
Replace .Clone() with explicit array allocation and copy in Arrays#662paulomorgado wants to merge 1 commit intobcgit:masterfrom
.Clone() with explicit array allocation and copy in Arrays#662paulomorgado wants to merge 1 commit intobcgit:masterfrom
Conversation
Refactored array cloning methods to avoid .Clone() in favor of explicit allocation and copying. Uses GC.AllocateUninitializedArray<T> on .NET 5.0+ for performance, falling back to new T[] otherwise. Buffer.BlockCopy is used for primitive types, and Array.Copy for generic buffers. This change improves control over array cloning and may reduce unnecessary zero-initialization overhead.
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.
Replace
.Clone()with explicit array allocation and copy inArraysSummary
Refactors all array cloning methods in
crypto/src/util/Arrays.csto replace the(T[])array.Clone()pattern with explicit allocation and copy. This gives the runtime more precise information about the operation, avoids unnecessary zero-initialization overhead on .NET 5.0+, and uses the most efficient copy path for each element type.Describe your changes
Clone(T[] data)overloads (bool,byte,short,ushort,int,uint,long,ulong): replaced(T[])data.Clone()withGC.AllocateUninitializedArray<T>(on .NET 5.0+, falling back tonew T[]on older targets) followed byBuffer.BlockCopy, which operates on raw bytes and is highly optimized for blittable/primitive types.InternalCopyBuffer<T>: replaced(T[])buf.Clone()withGC.AllocateUninitializedArray<T>+Array.Copy, since the generic constraint doesn't guarantee a blittable type suited forBuffer.BlockCopy.Benchmarks
Benchmark results
Benchmarked on: Windows 11, 13th Gen Intel Core i9-13900K, .NET 10.0.3 / .NET Framework 4.8.1.
The new approach is 7× faster for small arrays (16 elements) on .NET 10.0, and consistently 20–50% faster across all sizes on .NET Framework 4.8.1. Memory allocation is identical. No behavioral change — null inputs continue to return null.
How has this been tested?
Using the existing test.
Checklist before requesting a review
See also Contributing Guidelines.