Thread-local successor for object pooling

Alan Bateman Alan.Bateman at oracle.com
Tue Nov 1 09:07:48 UTC 2022


On 31/10/2022 20:48, Volkan Yazıcı wrote:
> Below is a list of TL usages in the Log4j code base. Note that some 
> serve other purposes than object pooling, yet I wanted to share them 
> anyway for completeness.
>
> - Log `Message`s and `LogEvent`s – all `log.info 
> <https://urldefense.com/v3/__http://log.info__;!!ACWV5N9M2RV99hQ!OqcqqIuxZfWxWBhWsDZzPFYspDFPSQtFJtbMBN-kTfQJKLcl3c_ngGsOanxY0iQgfz-oFogKSZ9WaVo6eg$>()` 
> calls get converted into a `LogEvent` wrapping a `Message`
>
> - Context Stack and Context Data (aka. NDC and MDC) stacks and lists, 
> respectively. TLs, besides providing an efficient way to mutate these 
> data structures, also serve another important purpose: storing the 
> logging context along the lifetime of an _operation_, which can be an 
> HTTP request-response, database transaction, etc. Ideally, these 
> should all be replaced with dynamic/scoped variables. Nevertheless, 
> there will (might?) be still a need for object pooling to avoid 
> instantiating a new MDC/NDC each time. Though this can be efficiently 
> implemented without needing TLs.

Scoped values are all about reading a value in a bounded period of 
execution by a thread so it would be good to try it with these examples. 
That said, you say "mutate these data structures" whereas the value of a 
ScopedValue should really be immutable and thread-safe, esp. when a task 
using scoped values is refactored into concurrent subtasks.


>
> - `StringBuilder`s – used in various layout implementations for 
> encoding the user input into a `ByteBuffer`. The user input is first 
> written to a `StringBuilder`, then the `StringBuilder#getChars()` is 
> used to dump its contents to a `ByteBuffer`, and finally a 
> `CharsetEncoder` is used to encode these characters to another target 
> `ByteBuffer`[1]. As you might guess, both the input and the output 
> `ByteBuffer`s are also received from TLs.
Assuming these aren't direct buffers, it would be useful to see if this 
caching has a positive or negative impact when using recent JDK releases.

>
> - Recursion prevention and depth limiting – again, a use case that 
> better be addressed with dynamic variables, though Java is not there yet.

Yes but only if there isn't any inheritance. Inheritance is an area of 
this API that needs more feedback.

-Alan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/loom-dev/attachments/20221101/37063a0e/attachment.htm>


More information about the loom-dev mailing list