-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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
Provide a way to reset an ArrayBufferWriter without clearing the underlying memory #49134
Comments
Tagging subscribers to this area: @tannergooding, @pgovind, @GrabYourPitchforks Issue DetailsBackground and MotivationCurrently there is no way to reset an Proposed APInamespace System.Buffers
{
public class ArrayBufferWriter<T> : IBufferWriter<T>
{
+ public void Reset();
}
} Usage ExamplesWhen building short-lived buffers in high performance scenarios (for example, to generate command buffers to be sent over PCIE or network to control physical devices), being able to quickly reset the buffer writer is helpful, and zeroing is an unnecessary expense. Alternative DesignsIt could alternatively be an overload of public void Clear(bool zeroBackingArray); RisksTypes which contain references should probably still be cleared to allow collection.
|
I was just coming here to make exactly the same suggestion. We end up having a custom implementation of ArrayBufferWriter in our project which uses a Reset method. It'd be great if we didn't need that. |
@tannergooding @pgovind any aversion to marking this ready for review? Seems like a straightforward enough proposal. /cc @davidfowl in case you'd find it interesting in your projects. |
I have to ask... do we even need a new API? Could we instead just retcon the design: |
In theory this could be a behavioral break, where people clear an array writer, populate some sparse elements, then advance it again, assuming that all untouched elements will have a default(T) value. Thinking back to #30649. But this is slightly different than that issue, where here we'd not be populating the underlying array with arbitrary uninitialized data. The data would still be initialized by virtue of the fact that it was supplied by the previous caller. So maybe the theoretical break isn't a huge concern? We could always make the change now since it's early preview and monitor feedback. |
@stephentoub Yeah, the implementation of my Reset function in fact has: public void Reset()
{
#if NETFW_OR_NETSTD2
_buffer.AsSpan(0, WrittenCount).Clear();
#else
if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
{
_buffer.AsSpan(0, WrittenCount).Clear();
}
#endif
WrittenCount = 0;
} But given that Clear has been systematically setting the array to 0, it is definitely a breaking change to stop doing that for primitive types. |
The difference with List.Clear is that the unused portion of the array inside of List is not visible to the user of the abstraction. The array underlying ArrayBufferWriter is visible to the outside world |
@GrabYourPitchforks, I don't have an issue here. However, if we can retcon or overload the existing design like Stephen suggested, that would be better 😄 |
How about we mark this ready-for-review, bring it to API review, and discuss the compat concerns there regarding the behavioral change? Either collectively we can say "new API approved!" or "let's repurpose the existing API." Either way it's forward progress on this issue. |
namespace System.Buffers;
public partial class ArrayBufferWriter<T> : IBufferWriter<T>
{
public void ResetWrittenCount();
} |
Background and Motivation
Currently there is no way to reset an
ArrayBufferWriter<T>
without zeroing the underlying memory, which can be expensive and an entirely unnecessary cost often.Proposed API
namespace System.Buffers { public class ArrayBufferWriter<T> : IBufferWriter<T> { + public void Reset(); } }
Usage Examples
When building short-lived buffers in high performance scenarios (for example, to generate command buffers to be sent over PCIE or network to control physical devices), being able to quickly reset the buffer writer is helpful, and zeroing is an unnecessary expense.
Alternative Designs
It could alternatively be an overload of
Clear
:Risks
Types which contain references should probably still be cleared to allow collection.
The text was updated successfully, but these errors were encountered: