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

c# feature: new method AsSpan() on RepeatedField<T> #6538

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions csharp/src/Google.Protobuf.Test/Collections/RepeatedFieldTest.cs
Expand Up @@ -756,5 +756,33 @@ public void NaNValuesComparedBitwise()
Assert.True(list1.Contains(SampleNaNs.SignallingFlipped));
Assert.False(list2.Contains(SampleNaNs.SignallingFlipped));
}

#if NETCOREAPP2_1
[Test]
public void AsSpan_BasicUsage()
{
var list = new RepeatedField<int> { 10, 11 };
var span = list.AsSpan();

// Span was constructed correctly
Assert.AreEqual(2, span.Length);
Assert.AreEqual(10, span[0]);
Assert.AreEqual(11, span[1]);

// Writing to span writes to RepeatedField
span[0] = 100;
Assert.AreEqual(100, list[0]);
}

[Test]
public void AsSpan_Empty()
{
var list = new RepeatedField<int>();
var span = list.AsSpan();

// Empty Span was constructed correctly
Assert.AreEqual(0, span.Length);
}
#endif
}
}
14 changes: 14 additions & 0 deletions csharp/src/Google.Protobuf/Collections/RepeatedField.cs
Expand Up @@ -543,6 +543,20 @@ public override string ToString()
}
}

#if NETSTANDARD2_0
/// <summary>
/// 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.
Copy link
Contributor

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.

Copy link
Contributor Author

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.

/// </summary>
/// <returns>Span representation of the RepeatedField</returns>
public Span<T> AsSpan()
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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.

{
return array.AsSpan(0, count);
}
#endif

#region Explicit interface implementation for IList and ICollection.
bool IList.IsFixedSize => false;

Expand Down