Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

AlexRadch
Copy link
Contributor

Close #49134 issue.

@ghost
Copy link

ghost commented Jun 24, 2023

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

@ghost ghost added the community-contribution Indicates that the PR has been added by a community member label Jun 24, 2023
@ghost
Copy link

ghost commented Jun 24, 2023

Tagging subscribers to this area: @dotnet/area-system-buffers
See info in area-owners.md if you want to be subscribed.

Issue Details

Close #49134 issue.

Author: AlexRadch
Assignees: -
Labels:

area-System.Buffers, new-api-needs-documentation

Milestone: -

public void ResetWrittenCount()
{
Debug.Assert(_buffer.Length >= _index);
if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@terrajobst, @bartonjs, @davidfowl, if the only difference between Clear and ResetWrittenCount is whether the memory is zeroed, would it makes sense to not special case references and just always not zero? That enables a caller to skip zeroing even for references if that's the behavior they want, or to themselves choose whether to Clear or ResetWrittenCount based on whether the T has references. Basically, should this entire method just be _index = 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you do not clear the references, then such objects will not be released immediately, but only together with the buffer. If the buffer is cached somewhere for a long time, a memory leak may occur. I do not know how this situation is possible and whether it makes sense then to add another method for clearing the entire buffer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you do not clear the references, then such objects will not be released immediately

That is why Clear clears. The only benefit to the new API is that it doesn't clear. If for things with references we still always clear, the new API has no benefit. Hence my question.

I do not know how this situation is possible and whether it makes sense then to add another method for clearing the entire buffer.

I don't think that's desirable. You can always just drop the ArrayBufferWriter itself. And my point was that by having this not zero, we give the consumer the flexibility.

The only argument I see for zeroing here as well is basically "The 99.9% use case for this type is with bytes and chars, and that's where perf matters; for the remaining 0.1% that might include references, we're sure the cost of zeroing doesn't matter and we want to make the API safer."

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 suggest the next solution:

  1. This method always resets written count and does not clear memory.
  2. To clear all buffer memory next code can be used in user code:
    buffer.Advance(buffer.FreeCapacity);
    buffer.Clear();

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@terrajobst, @bartonjs, @davidfowl, are you ok with where we landed here, that the entirety of the method is:

public void ResetWrittenCount() => _index = 0;

even if T is or contains references? I think it makes sense, but I want to make sure others agree.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My only issue with it is that you can't recover from it by calling Clear. You have to either advance back to the full position and then call Clear, or drop the whole object.

It probably won't end up mattering, since I didn't see anything on grep.app using ArrayBufferWriter for anything other than byte or char. So, in that sense, I'm OK with it, I suppose :)

Copy link
Member

@adamsitnik adamsitnik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AlexRadch thank you for your contribution! Overall the implementation LGTM, but we need to improve the comments and reduce code duplication in tests.

@ghost ghost added the needs-author-action An issue or pull request that requires more info or actions from the author. label Jul 17, 2023
@adamsitnik adamsitnik self-assigned this Jul 17, 2023
@ghost ghost added the no-recent-activity label Jul 31, 2023
@ghost
Copy link

ghost commented Jul 31, 2023

This pull request has been automatically marked no-recent-activity because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will remove no-recent-activity.

Copy link
Member

@jeffhandley jeffhandley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once the suggested changes are addressed, this is approved for merging into RC1 before the snap. We have at least one internal team that will validate the behavior of this new API in their scenarios during .NET 8.

This would not be approved for RC2, so we want to get this in before the RC1 snap.

@ghost ghost removed the no-recent-activity label Aug 10, 2023
@ghost ghost removed the needs-author-action An issue or pull request that requires more info or actions from the author. label Aug 10, 2023
Copy link
Member

@adamsitnik adamsitnik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you both @AlexRadch and @jozkee !

@adamsitnik adamsitnik merged commit bb9ba7a into dotnet:main Aug 11, 2023
@adamsitnik adamsitnik added this to the 8.0.0 milestone Aug 11, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Sep 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Buffers community-contribution Indicates that the PR has been added by a community member new-api-needs-documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide a way to reset an ArrayBufferWriter without clearing the underlying memory
6 participants