-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Optimizes opt* functions #337
Merged
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a8d4e47
adjustments to opt methods in reference to https://github.com/stleary…
bd4b180
Support for float to BigDecimal in optBigDecimal
c46774c
* Update opt* methods for JSONArray
fcdb867
grr, forgot to save changes on last commit
0c7bd72
fixes for javadoc
382f62e
* Prevent exceptions in cases where the value is not a string.
c28a2bd
* reverts changes to getDouble and related optDouble and optFloat
1ab5260
* Adds methods getNUmber and getFloat to JSONArray and JSONObject
a7f8ff2
correct string check for JSONObject optBigDecimal and optBigInteger
849b392
updates the getNumber/optNumber to not return invalid Doubles
04d6e83
* Missed JSONArray optFloat and optDouble for the revert
757b6ed
Merge branch 'master' of github.com:stleary/JSON-java into OptimizeOpt
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
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.
Introducing a
BigDecimal
to parse adouble
will add too much overhead for no reason, if you try to represent adouble
from aString
it will stay as is, if it was indeed adouble
then nothing will be gained or lost, you are only adding more GC overhead.Same thing for your other
BigDecimal
introduction when working withfloat
ordouble
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.
No,
Double.parse
will throw an exception if the number can not be represented as a double.From the javadocs:
Double.valueOf
seems to do something closer to what you are trying to accomplish, but it would be the same amount of garbage collection.new BigDecimal(String)
will always parse the value as long as it's a valid decimal number (i.e. not "abc"). ThedoubleValue
call will then do a narrowing conversion which may change the value to -Inf or +Inf.Lastly, this change would be out of scope for this PR. the
get*
methods were not modified here, only the opt*. If you feel we should evaluate it further, feel free to open another pull request with the changes and notes to performance impacts.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.
new BigDecimal(...)
andDouble.parseDouble(...)
have the same behavior, an input that fails forparseDouble
will also fail with new BigDecimal, the JavaDoc have different wording but the end result will be the same.As for relevance, you are trying to make opt* operations faster and yet you are introducing an expensive and unneeded instantiation when the return value is a native (small d)
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.
The JavaDoc is what to go by. If someone is running on a different VM with a different implementation, they may not function the same.
The BigDecimal conversion is not that expensive. I'd like to see some stats on it before making a change like that. We have no guarantees on the input. Just because someone asks for an
int
does not mean I would want to useInteger.parse
.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.
Also, I'm not "introducing" the big decimal conversion. It was there before the refactor.
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.
but then
int
typecast would be wrong for my proposed shortcut but it can be easily mitigated by checking if the double value is betweenInteger.MIN_VALUE
andInteger.MAX_VALUE
and if it isn't throw an exception.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.
No, it's an opt* function. The entire point is that it does not throw an exception. I'm not disagreeing that the parsing performance can be improved. But I think it needs more than a cursory glance and wishful thinking.
This PR is focusing strictly on the request made in #337 which was to mitigate the Exception handling impact.
Any parsing impacts should be in a new PR after this one is merged.
Any narrowing coercion has the potential to change the number. This is a given and also why I added the optNumber call to the API.
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.
Narrowing will not change the double for long, maybe for int, but then it is the same for BigDecimal calls, the reason I commented into this particular change is because of what I'm seeing:
Am I reading this incorrectly? I'm seeing
Double.parseDouble(...)
changed tonew BigDecimal(...)
in this commit.Narrowing will cause loss always, but the same will happen with
BigDecimal
hence my point of not bothering with such overhead at all.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.
let me check that. If I did change it, I'll likely change it back.
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.
Yes, I must have changed that by accident when I was testing stuff and didn't notice. I'll revert that change back and update the respective optDouble and optFloat calls that I copied that to.
As for the narrowing, it is handled differently between BigDecimal and Double. Also, not all Long values fit in a double. see the test case changes @ stleary/JSON-Java-unit-test#71 that I updated today. A Double only has 53 bits for it's mantissa which will not let any value over 2^53 be represented correctly.