Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Remove unnecessary checks in ConsumeNumber (#33294) #34500

Merged
merged 2 commits into from
Jan 18, 2019
Merged

Remove unnecessary checks in ConsumeNumber (#33294) #34500

merged 2 commits into from
Jan 18, 2019

Conversation

tkp1n
Copy link
Contributor

@tkp1n tkp1n commented Jan 10, 2019

Summary

Removes redundant checks in ConsumeNumber / ConsumeNumberMultiSegment which are ensured as postconditions of the wrapped method TryGetNumber / TryGetNumberMultiSegment. This adresses the most obvious optimization of #33294 (and fixes two typos in associated comments).

ConsumeNumber

TryGetNumber returns true if and only if one of the following expressions is true:

  • _consumed < _buffer.Length && JsonConstants.Delimiters.IndexOf(_buffer[_consumed]) >= 0
  • _consumed >= _buffer.Length && IsLastSpan

The first expression represents the standard case; there is data left, and the parsed number is terminated with a delimiter. The second expression is only valid if a "primitive" (payload consisting of only a number) is being parsed.

The checks in ConsumeNumber have been reduced to the required minimum to ensure correctness. Especially the not inexpensive check JsonConstants.Delimiters.IndexOf(_buffer[_consumed]) >= 0 could be removed (replaced with an assertion). I figured the introduced assertions make sense to ensure the postconditions of TryGetNumber don't change unnoticed.

ConsumeNumberMultiSegment

A similar situation presents itself with TryGetNumberMultiSegment. It returns true if and only if one of the following expressions is true:

  • _consumed < _buffer.Length && JsonConstants.Delimiters.IndexOf(_buffer[_consumed]) >= 0
  • _consumed >= _buffer.Length && IsLastSpan && !GetNextSpan()
  • _consumed >= _buffer.Length && IsLastSpan

The first expression represents the standard case; there is data left, and the parsed number is terminated with a delimiter. The second and third expression is only valid if a "primitive" (payload consisting of only a number) is being parsed.

The checks in ConsumeNumberMultiSegment have been reduced to the required minimum to ensure correctness. Especially the not inexpensive check JsonConstants.Delimiters.IndexOf(_buffer[_consumed]) >= 0 could be removed (replaced with an assertion). I figured the introduced assertions make sense to ensure the postconditions of TryGetNumberMultiSegment don't change unnoticed.

/cc @ahsonkhan

@ahsonkhan
Copy link
Member

This adresses #33294

There is still more work pending related to that issue, correct?

Copy link
Member

@ahsonkhan ahsonkhan left a comment

Choose a reason for hiding this comment

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

Otherwise, LGTM.

@tkp1n
Copy link
Contributor Author

tkp1n commented Jan 16, 2019

@dotnet-bot test code coverage please

@ahsonkhan ahsonkhan self-assigned this Jan 17, 2019
@ahsonkhan ahsonkhan added this to the 3.0 milestone Jan 17, 2019
@tkp1n
Copy link
Contributor Author

tkp1n commented Jan 17, 2019

This adresses #33294

There is still more work pending related to that issue, correct?

In #33294 I made some other suggestions that may improve perf. They are more niche and should be thoroughly benchmarked first. I suggest to investigate them seperately and merge this PR as is (once the issue regarding _isNotPrimitive is resolved).

@ahsonkhan
Copy link
Member

I suggest to investigate them seperately and merge this PR as is (once the issue regarding _isNotPrimitive is resolved).

Of course. I am fine with this being merged in, but I was just pointing out that we can't yet close the issue.

@tkp1n
Copy link
Contributor Author

tkp1n commented Jan 18, 2019

If CI is happy as well this is ready to merge

@ahsonkhan
Copy link
Member

@dotnet-bot test Linux x64 Release Build

@ahsonkhan ahsonkhan merged commit d2b28cc into dotnet:master Jan 18, 2019
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
…net/corefx#34500)

* Remove unnecessary checks in ConsumeNumber (dotnet/corefx#33294)

* Remove unnecessary _isNotPrimitive check


Commit migrated from dotnet/corefx@d2b28cc
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants