jakartaee / jakartaee/jsonp-api
Json factory methods are very inefficient
- Dominant language
- Java
- Stars
- 160
- Forks
- 64
- Avg merge
- 4d 5h
- Merged PRs (30d)
- 4
Description
The `Json` factory methods (`createObjectBuilder()`, `createArrayBuilder()`, etc) are very inefficient because they call `JsonProvider.provider()` every time and do not cache the result. Some benchmarking we did showed these methods to be an order of magnitude slower than using `JsonBuilderFactory` (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`:
```
private static final JsonBuilderFactory jsonFactory = Json.createBuilderFactory(null);
. . .
JsonObject jo = jsonFactory.createObjectBuilder()
.add(. . .
```
**Workaround for createValue()**
It looks like `Json.createValue()` suffers the same problem. I suppose a workaround for that is:
```
private static final JsonProvider provider = JsonProvider.provider();
JsonValue value = provider.createValue(5);
```
Contributor guide
Research direction
Start with the Json factory methods named in the issue, including createObjectBuilder(), createArrayBuilder(), and createValue(), and trace their use of JsonProvider.provider(). Determine whether the provider can be cached and reused safely; done means the methods avoid repeated provider lookup, or their Javadoc clearly directs users to JsonBuilderFactory or a cached JsonProvider if that is not possible.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- api
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100