Uploaded image for project: 'Wicket'
  1. Wicket
  2. WICKET-7041

Reduce allocations when rendering component headers

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 9.12.0
    • 10.0.0-M1, 8.15.0, 9.13.0
    • wicket-core
    • None

    Description

      We noticed a very substantial amount of allocations coming from Component#internalRenderHead:

      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

      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);
      }
      

      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.

      Attachments

        1. image-2023-04-10-16-56-52-061.png
          518 kB
          Thomas Heigl

        Activity

          People

            thomas.heigl Thomas Heigl
            thomas.heigl Thomas Heigl
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: