Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Do not clone empty arrays in CloneByteArray#93231

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
vcsjones merged 2 commits intodotnet:mainfromvcsjones:no-alloc-clone-empty
Oct 9, 2023

Conversation

@vcsjones
Copy link
Member

CloneByteArray is used heavily in S.S.Cryptography to create defensive copies of byte arrays. This is to avoid exposing any kind of mutability to callers.

Clone however appears tocreate a newbyte[] for empty byte arrays every time. Empty arrays are not uncommon. It could be for a CNG property, it could be for key parameters, or otherAsnEncodedData.

If its empty, just return the identity to save the allocation.

@ghost
Copy link

Tagging subscribers to this area: @dotnet/area-system-security,@bartonjs,@vcsjones
See info inarea-owners.md if you want to be subscribed.

Issue Details

CloneByteArray is used heavily in S.S.Cryptography to create defensive copies of byte arrays. This is to avoid exposing any kind of mutability to callers.

Clone however appears tocreate a newbyte[] for empty byte arrays every time. Empty arrays are not uncommon. It could be for a CNG property, it could be for key parameters, or otherAsnEncodedData.

If its empty, just return the identity to save the allocation.

Author:vcsjones
Assignees:vcsjones
Labels:

area-System.Security

Milestone:-

@vcsjones
Copy link
MemberAuthor

Failure is tracked in#93229. Merging.

@vcsjonesvcsjones merged commit63a375e intodotnet:mainOct 9, 2023
@vcsjonesvcsjones deleted the no-alloc-clone-empty branchOctober 9, 2023 19:59
@vcsjonesvcsjones added this to the9.0.0 milestoneOct 9, 2023
Comment on lines +48 to +53
returnsrcswitch
{
returnnull;
}

return(byte[])(src.Clone());
null=>null,
{Length:0}=>src,
_=>(byte[])src.Clone(),
};
Copy link
Member

Choose a reason for hiding this comment

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

FWIW, it could also have been:

returnsrcis notnull?src.AsSpan().ToArray():null;

and let span's ToArray handle the empty case via Array.Empty.

@ghostghost locked asresolvedand limited conversation to collaboratorsNov 9, 2023
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.

Reviewers

@stephentoubstephentoubstephentoub left review comments

@bartonjsbartonjsbartonjs approved these changes

+1 more reviewer

@filipnavarafilipnavarafilipnavara approved these changes

Reviewers whose approvals may not affect merge requirements

Assignees

@vcsjonesvcsjones

Projects

None yet

Milestone

9.0.0

Development

Successfully merging this pull request may close these issues.

4 participants

@vcsjones@filipnavara@stephentoub@bartonjs

[8]ページ先頭

©2009-2025 Movatter.jp