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

Add ByteString ToReadOnlySpan #7487

Conversation

fmg-lydonchandra
Copy link
Contributor

@fmg-lydonchandra fmg-lydonchandra commented Jan 24, 2025

Fixes #7488

Changes

Add ByteString ToReadOnlySpan, which has NO memory copy, compared to ToArray(), when ByteString is compact.

Why?

Currently we do a lot of RabbitMQ IncomingMessage Deserialization by calling ToArray() (which does memory copying)

        var evt = JsonSerializer.Deserialize<T>(message.Message.Bytes.ToArray(), SerializerOptions);

Using ToReadOnlySpan, memory copy can be avoided, as most of our IncomingMessage is compact

Checklist

For significant changes, please ensure that the following have been completed (delete if not relevant):

Latest dev Benchmarks

Include data from the relevant benchmark prior to this change here.

This PR's Benchmarks

Include data from after this change here.

@fmg-lydonchandra
Copy link
Contributor Author

Hi, is this change worth having? if so, will add tests.

@Aaronontheweb
Copy link
Member

Hi, is this change worth having? if so, will add tests.

LGTM - please add some tests

@fmg-lydonchandra
Copy link
Contributor Author

fmg-lydonchandra commented Jan 30, 2025

Hi @Aaronontheweb ,PR Review please.
The unit tests are quite flaky, the .NET Multi-Node Tests fails for unrelated reason to the PR, i think.

@Aaronontheweb
Copy link
Member

Will do

Copy link
Member

@Aaronontheweb Aaronontheweb left a comment

Choose a reason for hiding this comment

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

LGTM

return new ReadOnlySpan<byte>(compactBuffer.Array, compactBuffer.Offset, compactBuffer.Count);
}

return new ReadOnlySpan<byte>(ToArray());
Copy link
Member

Choose a reason for hiding this comment

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

Sucks that we have to allocate here but there's no way around it: Span<T> requires units to be allocated contiguously

@Aaronontheweb Aaronontheweb merged commit c8ebf77 into akkadotnet:dev Jan 30, 2025
9 of 11 checks passed
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.

Add ByteString ToReadOnlySpan()
2 participants