helidon-io / helidon

Java libraries for writing microservices
https://helidon.io
Apache License 2.0
3.53k stars 565 forks source link

Lazy implementation of MediaSupport classes #1753

Open romain-grecourt opened 4 years ago

romain-grecourt commented 4 years ago

The code below should be implemented lazily for all MediaSupport implementations:

    @Override
    public Collection<MessageBodyReader<?>> readers() {
        return List.of(newReader());
    }

    @Override
    public Collection<MessageBodyWriter<?>> writers() {
        return List.of(newWriter());
    }
tomas-langer commented 3 years ago

@romain-grecourt could you please elaborate a bit, this issue is quite old and I am not sure it is still relevant. Thanks

romain-grecourt commented 3 years ago

This is about avoiding the creation of new operator instances for every invocation to readers() and writers(). I.e Use a list field and initialize it on the first call to readers() and writers().

DefaultMediaSupport can be improved:

Using LazyValue with create() methods is odd, but it's a decent optimization as the operators are stateless. This pattern is implemented by JacksonSupport, JsonbSupport and JsonpSupport. We should update the javadoc to state that it's get or create rather than just create.