Skip to content
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

Avoid allocating byte[] just to convert to string #3425

Merged

Conversation

paulomorgado
Copy link

@paulomorgado paulomorgado commented Aug 6, 2024

Amazon.Runtime.Internal.Util.StringUtils.FromMemoryStream allocates a byte[] just to invoke Convert.ToBase64String.

Description

If the user provided a MemoryStream instance exposing the internal buffer, the buffer can be directly accessed and the data doesn't need to be copied.

If not, pooled memory can be used.

Motivation and Context

Reduce allocations and memory copying.

Testing

All existing tests in AWSSDK.CoreAndCustomUnitTests.NetStandard ran successfully.

Screenshots (if appropriate)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project
  • My change requires a change to the documentation
  • I have updated the documentation accordingly
  • I have read the README document
  • I have added tests to cover my changes
  • All new and existing tests passed

License

  • I confirm that this pull request can be released under the Apache 2 license

/cc @normj

Added System.Buffers to StringUtils.cs.
Refactored FromMemoryStream to use MemoryStream.TryGetBuffer and ArrayPool<byte> for efficient memory handling.
Modified FromString method signature without changing implementation.
Made minor formatting changes in FromValueTypeList and headerListEntry processing logic to enhance readability.
@paulomorgado paulomorgado changed the base branch from main to v4-development August 6, 2024 17:45
@normj normj merged commit 6e760d5 into aws:v4-development Aug 7, 2024
@normj normj mentioned this pull request Aug 7, 2024
@normj
Copy link
Member

normj commented Aug 7, 2024

Thank you for the PR!

@paulomorgado paulomorgado deleted the performance/FromMemoryStream branch August 8, 2024 10:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants