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

Opt performance enhancement #334

Closed
TheMatthew opened this issue May 15, 2017 · 6 comments
Closed

Opt performance enhancement #334

TheMatthew opened this issue May 15, 2017 · 6 comments

Comments

@TheMatthew
Copy link

Hi, I am currently using your lib to parse json logs that can be very large. I was wondering if it would be possible to make opts have an early failure. The reason I am asking is while

try{
return getThing(string);
}catch(exception e){
return defaultValue;
}

is super readable, the throw catch exception is very slow. It would speed things up a lot having a simple check in front of the field to return if there is no field defined from the get go.

Thanks for such a great and easy to use lib.
:)

@johnjaylward
Copy link
Contributor

the try/catch would need to stay in case of exceptions we don't actually plan on. But a pull request to enhance the opt* functions to be more performant would be welcome. Test cases should also be updated if needed in the separate test case project.

@stleary
Copy link
Owner

stleary commented May 15, 2017

Sure, you can propose a pull request if you have a code change in mind. Please see the FAQ more guidance on submitting code changes (https://github.com/stleary/JSON-java/wiki/FAQ). For performance enhancements, it will be helpful if you can provide some timing results to quantify the change.

@TheMatthew
Copy link
Author

TheMatthew commented May 16, 2017

I am checking if I can contribute to the project. Until then, here is an example of code that can do it. It is published in open source under EPL so what you do with it is up to you. ;)
TheMatthew/spacecompass@41bd1b0

johnjaylward pushed a commit to johnjaylward/JSON-java that referenced this issue May 16, 2017
@guidomedina
Copy link

guidomedina commented May 18, 2017

Hi all, that's a good catch, generally speaking relaying on exceptions for normal code flow is most likely to be slower than doing some checks like in this case if the value is absent.

Throwing exceptions will render stack traces and other things involved which will usually make the code slower by a factor, this can be easily proven, though might be unnecessary as it is a well known fact.

There are many articles about this but this is one example:

http://stackoverflow.com/questions/1546514/java-exceptions-as-control-flow

@johnjaylward
Copy link
Contributor

@guidomedina please don't spam

@stleary
Copy link
Owner

stleary commented May 18, 2017

This issue has been superseded by pull request #337, please post future comments there.

@stleary stleary closed this as completed May 18, 2017
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

No branches or pull requests

4 participants