jetty.project icon indicating copy to clipboard operation
jetty.project copied to clipboard

Remove thread-local cache `HttpOutput._encoder`

Open sbordet opened this issue 1 year ago • 1 comments

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.

sbordet avatar Feb 09 '24 17:02 sbordet

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.

gregw avatar Feb 09 '24 21:02 gregw

See also #10568.

sbordet avatar Feb 27 '24 17:02 sbordet

Also DateGenerator.__dateGenerator is a thread-local cache.

sbordet avatar Feb 27 '24 17:02 sbordet

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 avatar Feb 28 '24 08:02 gregw

@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.

sbordet avatar Feb 28 '24 12:02 sbordet

Done in https://github.com/jetty/jetty.project/pull/11498/

lorban avatar Mar 21 '24 11:03 lorban