-
Notifications
You must be signed in to change notification settings - Fork 593
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
8.0: implement BasicPublishMemory #990
Conversation
be89a31
to
2702284
Compare
Copying and pasting some benchmark results from @bollhals shared in #972: Update from a local running version on this:
Obviously the gain increases the larger the string (Exchange / RoutingKey) is. (Benchmark is with "Exchange_OR_RoutingKey"). Another benchmark to serialize into the 3 frames of a message:
It my does not seem huge, but it increases the throughput on the most relevant hotpath. |
This also cannot be backported to |
/// <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 |
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:
- You should only use this method if you actually cache the string (Otherwise you have worse performance)
- Exposing ROM enables customer to send non UTF8 values, which might cause more harm than good. (In which case would you have a valid ROM but not a string? (Stackalloc is also not possible since it's Memory not Span))
- In the future (comment from other PR) we could start returning the CachedString in other methods like BasicConsume or QueueDeclare, which then again could directly be used again in follow up methods for better performance.
- CachedString ist more clearly explaining the intent of the method than ROM + comments explaining it has to be a UTF8 sting
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
- You should only use this method if you actually cache the string (Otherwise you have worse performance)
- Exposing ROM enables customer to send non UTF8 values, which might cause more harm than good. (In which case would you have a valid ROM but not a string? (Stackalloc is also not possible since it's Memory not Span))
Which use cases could you see which taking a ROM is preferable than CachedString?
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
- CachedString ist more clearly explaining the intent of the method than ROM + comments explaining it has to be a UTF8 string
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.
CachedString ist more clearly explaining the intent of the method than ROM + comments explaining it has to be a UTF8 string
Could we add some validation into the constructor of that type or at least call it CachedUTF8String or something similar?
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?
Proposed Changes
fixes #972
Types of Changes
Checklist
CONTRIBUTING.md
documentFurther Comments
Also see perf benchmarks at #972 (comment)