[ 
https://issues.apache.org/jira/browse/WICKET-7041?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17710294#comment-17710294
 ] 

ASF GitHub Bot commented on WICKET-7041:
----------------------------------------

theigl commented on code in PR #572:
URL: https://github.com/apache/wicket/pull/572#discussion_r1162053099


##########
wicket-core/src/main/java/org/apache/wicket/response/LazyStringResponse.java:
##########
@@ -0,0 +1,101 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.response;
+
+import org.apache.wicket.request.Response;
+import org.apache.wicket.util.string.AppendingStringBuffer;
+
+
+/**
+ * Response object that writes to a StringWriter. If the StringResponse is 
later converted to a
+ * String via toString(), the output which was written to the StringResponse 
will be returned as a
+ * String.
+ * 
+ * @author Thomas Heigl
+ */
+public class LazyStringResponse extends Response
+{
+
+       /** StringWriter to write to */
+       private AppendingStringBuffer out;
+
+       /**
+        * @see Response#write(CharSequence)
+        */
+       @Override
+       public void write(final CharSequence string)
+       {
+               if (out == null)
+               {
+                       out = new AppendingStringBuffer(128);

Review Comment:
   Done





> Reduce allocations when rendering component headers
> ---------------------------------------------------
>
>                 Key: WICKET-7041
>                 URL: https://issues.apache.org/jira/browse/WICKET-7041
>             Project: Wicket
>          Issue Type: Improvement
>          Components: wicket-core
>    Affects Versions: 9.12.0
>            Reporter: Thomas Heigl
>            Assignee: Thomas Heigl
>            Priority: Major
>         Attachments: image-2023-04-10-16-56-52-061.png
>
>
> We noticed a *very* substantial amount of allocations coming from 
> Component#internalRenderHead:
> !image-2023-04-10-16-56-52-061.png|width=716,height=612!
> This is the relevant part of the code:
> https://github.com/apache/wicket/blame/9b4c5fe9a26174a06d612837297eca90ef8711cc/wicket-core/src/main/java/org/apache/wicket/Component.java#L2642
> {code:java}
> StringResponse markupHeaderResponse = new StringResponse();
> Response oldResponse = getResponse();
> RequestCycle.get().setResponse(markupHeaderResponse);
> try
> {
>       // Make sure the markup source strategy contributes to the header first
>       // to be backward compatible. WICKET-3761
>       getMarkupSourcingStrategy().renderHead(this, container);
>       CharSequence headerContribution = markupHeaderResponse.getBuffer();
>       if (Strings.isEmpty(headerContribution) == false)
>       {
>               response.render(StringHeaderItem.forString(headerContribution));
>       }
> }
> finally
> {
>       RequestCycle.get().setResponse(oldResponse);
> }
> {code}
> The code *always* allocates a new StringResponse for rendering potential 
> markup header contributions. Internally, this creates a new 
> AppendingStringBuffer with capacity 128.
> In my application, we do not use <wicket:head> tags at all. So 99.9% of these 
> allocations are completely unnecessary.
> Ideally, I'd like to get rid of the temporary StringResponse, but I don't 
> know if thats possible without breaking things.
> Another solution would be to create a LazyStringResponse that initializes the 
> internal String buffer on first use.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to