Copilot commented on code in PR #4136:
URL: https://github.com/apache/logging-log4j2/pull/4136#discussion_r3333546950


##########
log4j-api/src/main/java/org/apache/logging/log4j/message/StructuredDataMessage.java:
##########
@@ -356,10 +356,26 @@ public final void asString(final Format format, final 
StructuredDataId structure
     }
 
     private void asXml(final StructuredDataId structuredDataId, final 
StringBuilder sb) {
+
         sb.append("<StructuredData>\n");
-        sb.append("<type>").append(type).append("</type>\n");
-        sb.append("<id>").append(structuredDataId).append("</id>\n");
+
+        // Encode type
+        sb.append("<type>");
+        int start = sb.length();
+        sb.append(type);
+        StringBuilders.escapeXml(sb, start);
+        sb.append("</type>\n");
+
+        // Encode ID
+        sb.append("<id>");
+        start = sb.length();
+        sb.append(structuredDataId);
+        StringBuilders.escapeXml(sb, start);

Review Comment:
   `StructuredDataId` implements `StringBuilderFormattable`, but 
`sb.append(structuredDataId)` will call `toString()` and allocate a temporary 
`String`. Since this method is already doing in-place XML escaping, you can 
avoid the allocation by using `StringBuilders.appendValue(sb, 
structuredDataId)` before `escapeXml` (same pattern used in `asString(...)`).



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