ArraySegment<byte>
Recently I've been experimenting with Bond as a replacement to protobuf. However, I have run into a snag when trying to hook it into a caching system which uses StackExchange.Redis.
Bond's serializer uses a buffer which exposes the result as an ArraySegment
At the moment I have two methods that technically work, but both have major drawbacks:
- Create a new array based from the ArraySegment. This is poor because it defeats the purpose of a buffer pool. Currently serializing is almost zero alloc, and creating properly sized throw away arrays would destroy that.
- Pass the whole backing array to Redis. This makes sizing the pooled buffers properly much more important, and even in the best case you're still going to be very wasteful.
Is there trick that can be applied that I am missing, or is this something would require changes to the library?
In theory, making it work correctly with ArraySegment isn't that much
work. However, I would have concerns about lifetime management. If you
don't know exactly when redis has written a buffer to the network (which:
isn't immediately), a zero-allocation strategy would have difficulty
knowing when it can recycle the buffer. Of course, if you always waiting or
awaiting, this isn't a problem - but in the general case, both
fire-and-forget and dropped Tasks would allow your calling code to run to
completion (presumably potentially recycling the buffer).
Thoughts?
On 7 April 2016 at 17:06, BoltR [email protected] wrote:
Recently I've been experimenting with Bond as a replacement to protobuf. However, I have run into a snag when trying to hook it into a caching system which uses StackExchange.Redis.
Bond's serializer uses a buffer which exposes the result as an ArraySegment. I can't seem to find a good method of converting that ArraySegment to a RedisValue for transmission.
At the moment I have two methods that technically work, but both have major drawbacks:
Create a new array based from the ArraySegment. This is poor because it defeats the purpose of a buffer pool. Currently serializing is almost zero alloc, and creating properly sized throw away arrays would destroy that.
Pass the whole backing array to Redis. This makes sizing the pooled buffers properly much more important, and even in the best case you're still going to be very wasteful.
Is there trick that can be applied that I am missing, or is this something would require changes to the library?
— You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub https://github.com/StackExchange/StackExchange.Redis/issues/389
Regards,
Marc
In my specific case lifetime management shouldn't be an issue. The ArraySegment and pooling are technically separate concerns.
In the most basic example of using Bond you are creating an OutputBuffer and Serializer<T<T2>> every time you serialize something. One of the parameters on that OutputBuffer is a ArraySegment which holds the result.
Creating the Serializer is slow, and it's recommended to cache them. It's also recommended that you pool your OutputBuffer to reduce GC churn.
However, unlike protobuf-net none of this is done automatically for you. As such, I am maintaining the pool myself. At the time of writing this I just have an await inside of try/finally. I've been more concerned with getting it up and running nicely than having fire-and-forget working.
So really, the mention of pooling was more to give context for the why, rather than how.
That said, and as a side, note this is all on the serializing end of things. In a perfect world I would figure out how to go for zero-allocation on the deserialization side as well. Bond's InputBuffer takes a byte[] of data to work on. So in theory you should have a pool of byte[] to read from, which would be used by StackExchange.Redis to serve results. However, creating an interface for this that would work and/or be useful for the general case becomes a much trickier problem, and is well outside of this "Issue".
We should take another look at this with Memory<T> (and Span<T> internally), which means potentially a lot more #ifdefs on netstandard2.0...
@BoltR Is this still something desired, given the rise of Span<T> here?
Nope, as you have said with the rise of Span<T> it would be more desirable to just use ReadOnlySpan<T> instead.
It also makes a whole lot more sense to do with your announcement of intent to drop support for netstandard1.5.
Sorry if I shouldn't be polluting this Issue with unrelated questions, but I've been extremely busy with projects where this isn't relevant, and have not had time pop into any of the Microsoft Gitter channels to harass people in a very long while. However, you've sort of trigger a bit of a perfect storm for me. What with being reminded about this issue, the project it would potentially be used for on the verge of becoming a priority again, and Redis 5.0RC1 being announced today.
The last time I had actually looked at this myself I ended up spending a few hours hacking up a very minimal Disque client as an opportunity to kick the tires on Span<T> .
With Redis Streams finally getting close, are they something which StackExchange has any interest in eventually supporting?
I acknowledge commands like XREAD don't fit with the multiplexing nature of the library, but I'm going to be selfish and say I'd rather not duplicate work if there is going to end up being StackExchange.Redis.Streams that will be better battle tested anyways!
I don't know that we can use them for Stack Overflow itself (due to supporting Redis on Windows for a while longer - locked at 3.x), but they should absolutely be supported in this library, yes.
I plan for us to start dedicating some work time to libs now that we're caught up and have shipped some projects that have eaten all of our time for over a year now. Of course we'll still welcome contributions helping out! It's safe to say we'll be taking the Span<T>, etc. package references, so no constraints there.
2.0 supports Memory<byte> for RedisValue; this is semantically similar to ArraySegment<byte>, but with the advantage that it isn't limited to just arrays. You can trivially create a Memory<T> from an ArraySegment<T>. This should get you sorted. See https://github.com/StackExchange/StackExchange.Redis/issues/871
cross-referencing: https://github.com/StackExchange/StackExchange.Redis/issues/866