-
Notifications
You must be signed in to change notification settings - Fork 62
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
Json factory methods are very inefficient #154
Comments
the reason here was problematic caching in server environment. EE compliant server must provide some implementation which is typically loaded by bootstrap/server root classloader but also allow usage of different implementation which may be part of some web application (thus loaded by webapp classloader). There is also related #26 |
Not only are they slow, but they will also likely cause a global lock when the ServiceLoader is invoked. I have seen people get burned by this in both JSON-P and JAXP. But I agree with Lukas - at this point, I don't see what can be done other than improving the javadoc to warn people of this. |
Hi everyone. |
I had an additional thought - what would be useful is if here: There were an optional system property look up OVERRIDE_PROVIDER, or something like that, which statically loads a JsonProvider instance to be used universally. This would avoid the provider lookup when specified. This would allow someone who knows they only have the RI in the classpath to fix this performance trap if found in production without having to write additional code to fix the problem. This would also allow products with many developers to set this property in advance and guard against accidental future usage of these methods. |
@jjspiegel do you mean to define implementation discovery ie following way? : Implementation discovery consists of following steps in the order specified (first successful resolution applies):
|
@lukasj yes. And if possible, the Class.forName() on the value would happen outside of the method (static block) to avoid frequent class loading. |
Signed-off-by: Jorge Bescos Gascon <[email protected]>
Signed-off-by: Jorge Bescos Gascon <[email protected]>
Signed-off-by: Jorge Bescos Gascon <[email protected]>
Signed-off-by: Jorge Bescos Gascon <[email protected]>
Hi. While I can understand the concerns about multi-deployment, I just wanted to add some numbers so that people can understand how massive the problem is and why it should be solved. I did some JMH benchmarking this week-end as part of some work I was doing on Quarkus and here are the results:
Using a static provider with
That's 7200+ times faster. I think it's extremely dangerous to provide a cute shortcut (in the form of the Now, not saying there's an easy way out given all this is API but it looks like something that needs to be addressed. |
This got me thinking: Shouldn't it be possible to cache the instance per ClassLoader in a WeakHashMap maybe? |
jakartaee/jsonp-api#154 – Jakarta is slow It speeds up import action flow up to 60-80ms!
The
Json
factory methods (createObjectBuilder()
,createArrayBuilder()
, etc) are very inefficient because they callJsonProvider.provider()
every time and do not cache the result. Some benchmarking we did showed these methods to be an order of magnitude slower than usingJsonBuilderFactory
(which essentially reuses the same provider).These
Json
factory methods are an attractive nuisance because they are very tempting to use (convenient) but result in terrible performance.Is there any reason they do not cache and re-use the provider? That would fix this issue. If for some reason that's not possible then there should at least be a warning in the javadoc steering developers to use
JsonBuilderFactory
.Workaround for Builders and Readers
Use
JsonBuilderFactory
:Workaround for createValue()
It looks like
Json.createValue()
suffers the same problem. I suppose a workaround for that is:The text was updated successfully, but these errors were encountered: