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

Implement asynchronous support in BufferingJsonReader #2264

Merged

Conversation

gathogojr
Copy link
Contributor

Issues

This pull request partially fulfills #2019.

Description

Implement asynchronous support in BufferingJsonReader

Checklist (Uncheck if it is not completed)

  • Test cases added
  • Build and test with one-click build and test script passed

Additional work necessary

If documentation update is needed, please add "Docs Needed" label to the issue and provide details about the required document change in the issue.

bool result = await this.ReadInternalAsync()
.ConfigureAwait(false);

if (this.asyncInnerReader.NodeType == JsonNodeType.StartObject)
Copy link
Member

Choose a reason for hiding this comment

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

Maybe invert this to have the nesting level reduced.

.ConfigureAwait(false);
if (!readErrorStringPropertyResult.Item1)
{
return Tuple.Create(false, error);
Copy link
Member

Choose a reason for hiding this comment

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

Not sure if nullable would be better for these cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@marabooy To explain the logic, the only time this method returns the second component of the tuple as null is before entering this while loop. Within this loop, we return the error object with as many properties we have read before coming across some unintelligible property. For example, if successfully read code, message, target, and innererror properties and the come across details and we're unhappy, we don't return a tuple with false as first component and null as second component. Instead, the second component will contain the error object with the properties we had read successfully populated.

if (this.isBuffering)
{
Debug.Assert(this.currentBufferedNode != null, "this.currentBufferedNode != null");
return Task.FromResult(this.currentBufferedNode.Value);
Copy link
Contributor

Choose a reason for hiding this comment

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

Out of curiosity, you opted not to use ValueTask in this case because this is a public method or because the synchronous code-path is less frequent than the async one?

@gathogojr gathogojr force-pushed the feature/bufferingjsonreader-async branch 2 times, most recently from 196deee to 19cc4d6 Compare November 30, 2021 04:46

if (value == null)
{
result = new MemoryStream(new byte[0]);
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 we can use Array.Empty<byte>() to avoid allocating an empty array each time. Consider using it in the sync method as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@habbes Array.Empty<byte>() is not available in net45 and netstandard1.1. I don't know if it'd be worthwhile to do this:

#if NETSTANDARD2_0
                result = new MemoryStream(Array.Empty<byte>());
#else
                result = new MemoryStream(new byte[0]);
#endif

Copy link
Contributor

Choose a reason for hiding this comment

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

@gathogojr this is an issue that is causing too many warnings. Will probably fix it in 8.x where we remove support for net45

if (this.bufferedNodesHead == null)
{
// capture the current state of the reader as the first item in the buffer (if there are none)
object value = await this.asyncInnerReader.GetValueAsync()
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems a lot of these methods are async because of getting the value. I wonder if we could avoid some of the overhead when the value is immediately available. But that's probably a discussion for another day.

{
this.AssertAsynchronous();

if (!this.isBuffering && this.asyncInnerReader is IJsonStreamReaderAsync asyncStreamReader)
Copy link
Contributor

Choose a reason for hiding this comment

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

I see this check for IJsonStreamReaderAsync in a few different places. I wonder if it makes sense to have a field where this is stored so that the casting only has to happen once. I also wonder if that field should be set in the constructor. I can see there being different use cases where it makes sense to do it on the fly versus doing it in the constructor. Any thoughts?


object value = await this.GetValueAsync()
.ConfigureAwait(false);
return value is string || value == null || this.NodeType == JsonNodeType.StartArray || this.NodeType == JsonNodeType.StartObject;
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think the subsequent conditions are predicated on this first one for any reason, so I'm wondering if it makes sense to do the cast last due to short circuit evaluation

Copy link
Contributor Author

@gathogojr gathogojr Dec 1, 2021

Choose a reason for hiding this comment

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

@corranrogue9 Does a cast actually happen here given that value is already boxed and string is a reference type? In addition, supposing StartArray and StartObject occur rarely, do we not end up almost always doing the "cheap" comparisons as well as evaluating value is string expression?

@gathogojr gathogojr force-pushed the feature/bufferingjsonreader-async branch 2 times, most recently from 7217b6c to f6e08da Compare November 30, 2021 18:40
@gathogojr gathogojr force-pushed the feature/bufferingjsonreader-async branch 2 times, most recently from 428f491 to 00f1b67 Compare December 1, 2021 10:07
@gathogojr gathogojr force-pushed the feature/bufferingjsonreader-async branch from 00f1b67 to ec90ede Compare December 1, 2021 11:39
@pull-request-quantifier-deprecated

This PR has 679 quantified lines of changes. In general, a change size of upto 200 lines is ideal for the best PR experience!


Quantification details

Label      : Extra Large
Size       : +670 -9
Percentile : 89.3%

Total files changed: 5

Change summary by file extension:
.cs : +670 -9

Change counts above are quantified counts, based on the PullRequestQuantifier customizations.

Why proper sizing of changes matters

Optimal pull request sizes drive a better predictable PR flow as they strike a
balance between between PR complexity and PR review overhead. PRs within the
optimal size (typical small, or medium sized PRs) mean:

  • Fast and predictable releases to production:
    • Optimal size changes are more likely to be reviewed faster with fewer
      iterations.
    • Similarity in low PR complexity drives similar review times.
  • Review quality is likely higher as complexity is lower:
    • Bugs are more likely to be detected.
    • Code inconsistencies are more likely to be detetcted.
  • Knowledge sharing is improved within the participants:
    • Small portions can be assimilated better.
  • Better engineering practices are exercised:
    • Solving big problems by dividing them in well contained, smaller problems.
    • Exercising separation of concerns within the code changes.

What can I do to optimize my changes

  • Use the PullRequestQuantifier to quantify your PR accurately
    • Create a context profile for your repo using the context generator
    • Exclude files that are not necessary to be reviewed or do not increase the review complexity. Example: Autogenerated code, docs, project IDE setting files, binaries, etc. Check out the Excluded section from your prquantifier.yaml context profile.
    • Understand your typical change complexity, drive towards the desired complexity by adjusting the label mapping in your prquantifier.yaml context profile.
    • Only use the labels that matter to you, see context specification to customize your prquantifier.yaml context profile.
  • Change your engineering behaviors
    • For PRs that fall outside of the desired spectrum, review the details and check if:
      • Your PR could be split in smaller, self-contained PRs instead
      • Your PR only solves one particular issue. (For example, don't refactor and code new features in the same PR).

How to interpret the change counts in git diff output

  • One line was added: +1 -0
  • One line was deleted: +0 -1
  • One line was modified: +1 -1 (git diff doesn't know about modified, it will
    interpret that line like one addition plus one deletion)
  • Change percentiles: Change characteristics (addition, deletion, modification)
    of this PR in relation to all other PRs within the repository.


Was this comment helpful? 👍  :ok_hand:  :thumbsdown: (Email)
Customize PullRequestQuantifier for this repository.


if (value == null)
{
result = new MemoryStream(new byte[0]);
Copy link
Contributor

Choose a reason for hiding this comment

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

@gathogojr this is an issue that is causing too many warnings. Will probably fix it in 8.x where we remove support for net45

@gathogojr gathogojr merged commit d643a05 into OData:master Dec 3, 2021
@gathogojr gathogojr deleted the feature/bufferingjsonreader-async branch December 3, 2021 09:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants