ppkarwasz commented on code in PR #2275:
URL: https://github.com/apache/logging-log4j2/pull/2275#discussion_r1484206125


##########
log4j-api/src/main/java/org/apache/logging/log4j/message/ReusableParameterizedMessage.java:
##########
@@ -32,10 +33,18 @@
 @PerformanceSensitive("allocation")
 public class ReusableParameterizedMessage implements ReusableMessage, 
ParameterVisitable, Clearable {
 
-    private static final int MIN_BUILDER_SIZE = 512;
     private static final int MAX_PARAMS = 10;
     private static final long serialVersionUID = 7800075879295123856L;
-    private transient ThreadLocal<StringBuilder> buffer; // non-static: 
LOG4J2-1583
+
+    private static final StringBuilderRecycler STRING_BUILDER_RECYCLER = 
StringBuilderRecycler.of(
+            Constants.MAX_REUSABLE_MESSAGE_SIZE,
+            // This value indicates the maximum recursion depth before the 
recycler starts creating new instances.
+            // Consider a `ParameterizedMessage` containing an argument such 
that its `toString()` causes another (i.e.,
+            // recursive) `ParameterizedMessage` formatting. This value 
indicates the depth we support garbage-free
+            // formatting in such nested formatting situations. When this 
depth is exceeded, code still works, but
+            // starts generating garbage due to new `StringBuilder` 
allocations.
+            3,
+            Constants.ENABLE_THREADLOCALS);

Review Comment:
   In the case of `ReusableParameterizedMessage` is recursion even possible? 
The `buffer` field is an instance field and `getFormattedMessage` does not call 
itself recursively.
   
   I would simplify this with:
   ```java
   private transient StringBuilder buffer;
   ```
   
   No thread local is needed: if `ReusableParameterizedMessage` is used 
concurrently on two threads, the problem lies elsewhere.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to