jetty.project
jetty.project copied to clipboard
Remove thread-local cache `HttpOutput._encoder`
Jetty version(s) 12+
Jetty Environment ee10, ee9
Description
Using thread-local caches is now an anti-pattern, since for virtual threads they will never cache.
The encoder, if necessary, can be cached as a field in HttpOutput for multiple calls to print(), and nulled out / recycled when HttpOutput is recycled.
I'm just going to say this for posterity and I told you so rights...... I think time will show that it is virtual threads that is the anti-pattern.
See also #10568.
Also DateGenerator.__dateGenerator is a thread-local cache.
Perhaps we should generally replace thread local caches with a Concurrent pool based cache. Since this uses thread id to start it's iteration, this avoids contention but would allow entries to be reused by virtual threads as the id is modulo pool size.
@gregw ConcurrentPool should be a valid alternative, but I think we should analyze case by case.
For example, for HttpOutput._encoder we can just use a field, as HttpOutput is recycled.
For DateGenerator we cannot use a field (too much contention on a single object), perhaps ConcurrentPool or perhaps something simpler like an atomic update.
Done in https://github.com/jetty/jetty.project/pull/11498/