Skip to content

Conversation

@BorisDog
Copy link
Owner

@BorisDog BorisDog commented Jan 5, 2021

Follow up for mongodb#388.
I think we should try to reset the buffer automatically, and not expose this to a user.

  1. Auto reset buffer added. Trying to reset automatically after each token read, with respecting bookmarks.
  2. Added JsonBuffer config options

if (_jsonReaderSettings.AutoBufferReset && _bookmarks.Count == 0)
{
_buffer.ResetBuffer();
}
Copy link
Owner Author

Choose a reason for hiding this comment

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

Try to reset after reading a token (buffer will reset only if MinResetBufferSize chars read)

Copy link
Collaborator

Choose a reason for hiding this comment

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

There is no requirement that ReturnToBookmark be called (it might or it might not).

Therefore, there is no guarantee that the Count will ever go back to zero.

I think we need a new approach.

_buffer.Position = jsonReaderBookmark.Position;

// TODO BD throw if not found?
_bookmarks.Remove(jsonReaderBookmark);
Copy link
Owner Author

Choose a reason for hiding this comment

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

Show we disallow custom created bookmarks (maybe as an JsonReaderSettings option)?

/// TODO BD
/// </summary>
public int MinResetBufferSize { get; set; } = 512;

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we use an approach that doesn't involve adding any new public APIs?

Copy link
Owner Author

Choose a reason for hiding this comment

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

We can, but:

  1. Would not it be beneficial to provide tuning options for advanced JsonReader.cs users?
  2. Wanted to introduce "killswitch" with JsonReaderSettings.AutoBufferReset

Copy link
Collaborator

Choose a reason for hiding this comment

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

We have a "no knobs" principle at MongoDB.

In practice that really means "as few knobs as possible".

Copy link
Owner Author

@BorisDog BorisDog Jan 13, 2021

Choose a reason for hiding this comment

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

I see, would we want to leave JsonReaderSettings.AutoBufferReset if we were to proceed with this approach?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I haven't reviewed carefully besides pointing out that we would prefer not to change the public API at all if possible.

Can we find an approach that "just works" without requiring any public API changes?

Copy link
Owner Author

Choose a reason for hiding this comment

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

Yes, the suggested change should just work. JsonReaderSettings.AutoBufferReset is proposed for additional safety. Whether we can do without it depends on our confidence of the proposed changes.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think you should remove these new settings (knobs).

They are not required.

The ticket is about clearing the buffer when possible to reduce memory consumption, not about making more things configurable.

@BorisDog BorisDog requested a review from jyemin January 27, 2021 17:19
@jyemin jyemin requested review from DmitryLukyanov and removed request for jyemin January 27, 2021 17:41
@DmitryLukyanov DmitryLukyanov changed the title csharp1525 CSHARP-1525: JsonBuffer can exhaust memory. Jan 27, 2021
Copy link
Collaborator

@DmitryLukyanov DmitryLukyanov left a comment

Choose a reason for hiding this comment

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

Initial review. I will take a look at the changes itself after Robert

/// <summary>
/// Initializes a new instance of the <see cref="JsonBuffer" /> class.
/// </summary>
/// <summary>Initializes a new instance of the <see cref="JsonBuffer" /> class.</summary>
Copy link
Collaborator

Choose a reason for hiding this comment

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

let's use the default formatting for xml comment:

/// <summary>
/// ...
/// </summary>

/// <summary>Initializes a new instance of the <see cref="JsonBuffer" /> class.</summary>
/// <param name="reader">The reader.</param>
public JsonBuffer(TextReader reader)
/// <param name="readChunkSize"></param>
Copy link
Collaborator

Choose a reason for hiding this comment

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

missed params

/// <summary>
/// TODO BD
/// </summary>
public bool AutoBufferReset { get; set; } = true;
Copy link
Collaborator

Choose a reason for hiding this comment

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

in the current styling guide, we don't use auto properties

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also, properties should be placed after constructor

@BorisDog BorisDog requested a review from rstam February 12, 2021 17:57
/// TODO BD
/// </summary>
public int MinResetBufferSize { get; set; } = 512;

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think you should remove these new settings (knobs).

They are not required.

The ticket is about clearing the buffer when possible to reduce memory consumption, not about making more things configurable.

if (_jsonReaderSettings.AutoBufferReset && _bookmarks.Count == 0)
{
_buffer.ResetBuffer();
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

There is no requirement that ReturnToBookmark be called (it might or it might not).

Therefore, there is no guarantee that the Count will ever go back to zero.

I think we need a new approach.

@BorisDog BorisDog closed this Feb 18, 2021
@BorisDog BorisDog deleted the csharp1525 branch April 14, 2022 20:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants