-
-
Notifications
You must be signed in to change notification settings - Fork 796
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
Improve perf of float and double parsing in TextBuffer #1230
Merged
cowtowncoder
merged 10 commits into
FasterXML:2.18
from
pjfanning:textbuffer-double-float-parse
Apr 20, 2024
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a4febb8
improve perf of float and double parsing in TextBuffer
pjfanning 58a9342
throw exception if content is empty
pjfanning 2ffce6e
update javadoc
pjfanning 64e25b2
review comments
pjfanning 1f2a104
Merge branch '2.18' into textbuffer-double-float-parse
cowtowncoder bab9369
Simplify implementation slightly
cowtowncoder e5e2ee7
Merge branch '2.18' into textbuffer-double-float-parse
cowtowncoder 4100902
More tweaking
cowtowncoder 5c5f01d
Merge branch 'textbuffer-double-float-parse' of github.com:pjfanning/…
cowtowncoder 04114a0
Update release notes
cowtowncoder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -354,12 +354,11 @@ private char[] buf(int needed) | |
private void clearSegments() | ||
{ | ||
_hasSegments = false; | ||
/* Let's start using _last_ segment from list; for one, it's | ||
* the biggest one, and it's also most likely to be cached | ||
*/ | ||
/* 28-Aug-2009, tatu: Actually, the current segment should | ||
* be the biggest one, already | ||
*/ | ||
// Let's start using _last_ segment from list; for one, it's | ||
// the biggest one, and it's also most likely to be cached | ||
|
||
// 28-Aug-2009, tatu: Actually, the current segment should | ||
// be the biggest one, already | ||
//_currentSegment = _segments.get(_segments.size() - 1); | ||
_segments.clear(); | ||
_currentSize = _segmentSize = 0; | ||
|
@@ -525,15 +524,41 @@ public char[] contentsAsArray() throws IOException { | |
/** | ||
* Convenience method for converting contents of the buffer | ||
* into a Double value. | ||
*<p> | ||
* NOTE! Caller <b>MUST</b> validate contents before calling this method, | ||
* to ensure textual version is valid JSON floating-point token -- this | ||
* method is not guaranteed to do any validation and behavior with invalid | ||
* content is not defined (either throws an exception or returns arbitrary | ||
* number). | ||
* | ||
* @param useFastParser whether to use {@code FastDoubleParser} | ||
* @return Buffered text value parsed as a {@link Double}, if possible | ||
* | ||
* @throws NumberFormatException if contents are not a valid Java number | ||
* @throws NumberFormatException may (but is not guaranteed!) be thrown | ||
* if contents are not a valid JSON floating-point number representation | ||
* | ||
* @since 2.14 | ||
*/ | ||
public double contentsAsDouble(final boolean useFastParser) throws NumberFormatException { | ||
public double contentsAsDouble(final boolean useFastParser) throws NumberFormatException | ||
{ | ||
// Order in which check is somewhat arbitrary... try likeliest ones | ||
// that do not require allocation first | ||
|
||
// except _resultString first since it works best with JDK (non-fast parser) | ||
if (_resultString != null) { | ||
return NumberInput.parseDouble(_resultString, useFastParser); | ||
} | ||
if (_inputStart >= 0) { // shared? | ||
return NumberInput.parseDouble(_inputBuffer, _inputStart, _inputLen, useFastParser); | ||
} | ||
if (_currentSize == 0) { // all content in current segment! | ||
return NumberInput.parseDouble(_currentSegment, 0, _currentSize, useFastParser); | ||
} | ||
if (_resultArray != null) { | ||
return NumberInput.parseDouble(_resultArray, useFastParser); | ||
} | ||
|
||
// Otherwise, segmented so need to use slow path | ||
try { | ||
return NumberInput.parseDouble(contentsAsString(), useFastParser); | ||
} catch (IOException e) { | ||
|
@@ -574,14 +599,41 @@ public float contentsAsFloat() throws NumberFormatException { | |
/** | ||
* Convenience method for converting contents of the buffer | ||
* into a Float value. | ||
*<p> | ||
* NOTE! Caller <b>MUST</b> validate contents before calling this method, | ||
* to ensure textual version is valid JSON floating-point token -- this | ||
* method is not guaranteed to do any validation and behavior with invalid | ||
* content is not defined (either throws an exception or returns arbitrary | ||
* number). | ||
* | ||
* @param useFastParser whether to use {@code FastDoubleParser} | ||
* @return Buffered text value parsed as a {@link Float}, if possible | ||
* | ||
* @throws NumberFormatException if contents are not a valid Java number | ||
* @throws NumberFormatException may (but is not guaranteed!) be thrown | ||
* if contents are not a valid JSON floating-point number representation | ||
* | ||
* @since 2.14 | ||
*/ | ||
public float contentsAsFloat(final boolean useFastParser) throws NumberFormatException { | ||
public float contentsAsFloat(final boolean useFastParser) throws NumberFormatException | ||
{ | ||
// Order in which check is somewhat arbitrary... try likeliest ones | ||
// that do not require allocation first | ||
|
||
// except _resultString first since it works best with JDK (non-fast parser) | ||
if (_resultString != null) { | ||
return NumberInput.parseFloat(_resultString, useFastParser); | ||
} | ||
if (_inputStart >= 0) { // shared? | ||
return NumberInput.parseFloat(_inputBuffer, _inputStart, _inputLen, useFastParser); | ||
} | ||
if (_currentSize == 0) { // all content in current segment! | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, fixed in #1313 |
||
return NumberInput.parseFloat(_currentSegment, 0, _currentSize, useFastParser); | ||
} | ||
if (_resultArray != null) { | ||
return NumberInput.parseFloat(_resultArray, useFastParser); | ||
} | ||
|
||
// Otherwise, segmented so need to use slow path | ||
try { | ||
return NumberInput.parseFloat(contentsAsString(), useFastParser); | ||
} catch (IOException e) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was a bug, actually -- should use
_hasSegments
instead! Fixed in #1313.