-
Notifications
You must be signed in to change notification settings - Fork 15.6k
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
c# feature: new method AsSpan() on RepeatedField<T> #6538
Conversation
Provides very efficient access to the underlying data at the expense of foregoing null checks.
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed (or fixed any issues), please reply here with What to do if you already signed the CLAIndividual signers
Corporate signers
ℹ️ Googlers: Go here for more info. |
@googlebot I signed it! |
CLAs look good, thanks! ℹ️ Googlers: Go here for more info. |
This seems as an overkill. What is the use case where using the indexer is not sufficient? I don't thing we should add an extra API (that will need to be supported forever) unless this is a common use case that will be used by many. |
Bulk operations. My use case is adding lots of primitives to repeated fields to construct a response. RepeatedField's indexer is 12x slower than a raw array, and 4.5x slower than Span. | Raw array | 1.3ms | On a related note, I need to look further into whether or not this will actually work with C# 6. I ran into some errors creating the benchmark and needed to update the language to 7, which the maintainers aren't ready to do yet. |
/// Creates a new span over the RepeatedField. This is the most | ||
/// efficient way to read and write values to the RepeatedField because | ||
/// there are no internal null or bounds checks. You are responsible | ||
/// for ensuring no null values are written to the span. |
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 think that the lack of null check is dangerous and relying on users to always do the right thing is also dangerous.
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.
C++ allows you to memcpy into RepeatedField. But that's C++. I suppose C# users expect the null checks and such.
/// for ensuring no null values are written to the span. | ||
/// </summary> | ||
/// <returns>Span representation of the RepeatedField</returns> | ||
public Span<T> AsSpan() |
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.
Let's say I wanted to populate the repeated field with 1 million values. How do I resize the empty RepeatedField to the right size using the public API?
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 have another PR for that, but I will need to modify it slightly. It preallocates the array but doesn't increase count, so, as you point out, it isn't going to work. It will still be nice to have for reads.
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.
It would be good to first know what it the full API you're proposing. Isolated like this, it doesn't make much sense.
Btw, we just merged #6317, so this PR might be out of date.
Would an optimized version of AddRange() help you then? (have you tried using AddRange()?) |
I would also like to have support for public ReadOnlySpan<T> this[Range range]
{
get
{
(int start, int length) = range.GetOffsetAndLength(array.Length);
return new ReadOnlySpan<T>(array, start, length);
}
} |
This PR is out of date now that #7351 has been merged. |
After a long time, there's still some unsolved issues with this PR (e.g. it cannot ensure that the values won't become null, there's no way of setting size when filling a repeated field, should we only allow read only access and provide a different way for populating the contents, etc.). |
Btw, a RepeatedField has a Also note that exposing the data as read-only ( One idea for safe span-based initialization: |
@jtattermusch has this PR stranded completely? What's the status? I have issues with |
Provides very efficient access to the underlying data at the expense of foregoing null checks.
#6217