-
Notifications
You must be signed in to change notification settings - Fork 595
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
8.0: implement BasicPublishMemory #990
Merged
michaelklishin
merged 1 commit into
rabbitmq:master
from
bollhals:feature/PublishMemory
Dec 24, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System; | ||
using System.Text; | ||
|
||
namespace RabbitMQ.Client | ||
{ | ||
/// <summary> | ||
/// Caches a string's byte representation to be used for certain methods like <see cref="IModel.BasicPublish(CachedString,CachedString,bool,IBasicProperties,ReadOnlyMemory{byte})"/>. | ||
/// </summary> | ||
public sealed class CachedString | ||
{ | ||
public static readonly CachedString Empty = new CachedString(string.Empty, ReadOnlyMemory<byte>.Empty); | ||
|
||
/// <summary> | ||
/// The string value to cache. | ||
/// </summary> | ||
public readonly string Value; | ||
/// <summary> | ||
/// Gets the bytes representing the <see cref="Value"/>. | ||
/// </summary> | ||
public readonly ReadOnlyMemory<byte> Bytes; | ||
|
||
/// <summary> | ||
/// Creates a new <see cref="CachedString"/> based on the provided string. | ||
/// </summary> | ||
/// <param name="value">The string to cache.</param> | ||
public CachedString(string value) | ||
{ | ||
Value = value; | ||
Bytes = Encoding.UTF8.GetBytes(value); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a new <see cref="CachedString"/> based on the provided bytes. | ||
/// </summary> | ||
/// <param name="bytes">The bytes.</param> | ||
public CachedString(ReadOnlyMemory<byte> bytes) | ||
{ | ||
#if !NETSTANDARD | ||
Value = Encoding.UTF8.GetString(bytes.Span); | ||
#else | ||
unsafe | ||
{ | ||
fixed (byte* bytePointer = bytes.Span) | ||
{ | ||
Value = Encoding.UTF8.GetString(bytePointer, bytes.Length); | ||
} | ||
} | ||
#endif | ||
Bytes = bytes; | ||
} | ||
|
||
/// <summary> | ||
/// Creates a new <see cref="CachedString"/> based on the provided values. | ||
/// </summary> | ||
/// <param name="value">The string to cache.</param> | ||
/// <param name="bytes">The byte representation of the string value.</param> | ||
public CachedString(string value, ReadOnlyMemory<byte> bytes) | ||
{ | ||
Value = value; | ||
Bytes = bytes; | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.
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.
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.
@bollhals what is the benefit of exposing this as method parameters vs exposing the CLR type ReadonlyMemory? At first glance it looks like only convenience right? So would it make sense to make the public Api just accepting the CLR type instead? I'm asking because even with cached string the caller of the API needs to be concerned with caching the instances so the value of this class is really only abstracting the encoding get bytes which I find not enough to expose a custom type. A middle ground could be to expose the type but make it convertible to readonly memory while still switching the public Api to readonly memory
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.
I hope my explanation is reasonable and what I wrote is understandable 😄
This was one thing which I was not 100% certain what the best option might be. In the end I've exposed it as CachedString due to:
Let's switch it around, what impact would you get from exposing just ROM?
Which use cases could you see which taking a ROM is preferable than CachedString?
The intent of this API is better performance due to not having to encode the same string over and over again which is done by caching the encoded value, which implies that the user is caching the value.
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.
Ok got it. These are really the strongest reason to expose a custom type
My thoughts were comming from the experience that when you have a .NET type available to expose a .NET type where it makes sense because it is one API less you have to maintain and potentially evolve. But like mentioned above the two major bulllets seem to indicate a strong custom type makes sense.
Regarding
Could we add some validation into the constructor of that type or at least call it
CachedUTF8String
or something similar?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.
What kind of validation?
Sure we can rename it, but I'm not favoring UTF8 in the name as the type kind of abstracting it away so you don't need to care what it is. Adding it in the name makes it clear what it will use, but for what purpose?