You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by "Thomas Heigl (Jira)" <ji...@apache.org> on 2020/04/30 07:24:00 UTC

[jira] [Commented] (WICKET-6776) Cache Urls returned from ServletWebRequest

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

Thomas Heigl commented on WICKET-6776:
--------------------------------------

I solved this in my application by extending {{ServletWebRequest}} like this:

{code}
public class BaseWebRequest extends ServletWebRequest {

	private Url clientUrl;
	private Url url;

	public BaseWebRequest(HttpServletRequest httpServletRequest, String filterPrefix) {
		super(httpServletRequest, filterPrefix);
	}

	@Override
	public Url getClientUrl() {
		if (clientUrl == null) {
			clientUrl = new ImmutableUrl(super.getClientUrl());
		}
		return clientUrl;
	}

	@Override
	public Url getUrl() {
		if (url == null) {
			url = new ImmutableUrl(super.getUrl());
		}
		return url;
	}

	static final class ImmutableUrl extends Url {

		public ImmutableUrl(Url url) {
			super(url);
		}

		@Override
		public void setFragment(String fragment) {
			throw new UnsupportedOperationException();
		}

		@Override
		public void setQueryParameter(String name, Object value) {
			throw new UnsupportedOperationException();
		}

		@Override
		public void setProtocol(String protocol) {
			throw new UnsupportedOperationException();
		}

		@Override
		public void setContextRelative(boolean contextRelative) {
			throw new UnsupportedOperationException();
		}

		@Override
		public void setPort(Integer port) {
			throw new UnsupportedOperationException();
		}

		@Override
		public void setHost(String host) {
			throw new UnsupportedOperationException();
		}
	}

}
{code}

Urls are mutable so I'm putting an immutable wrapper around them to guard against accidental changes.

> Cache Urls returned from ServletWebRequest
> ------------------------------------------
>
>                 Key: WICKET-6776
>                 URL: https://issues.apache.org/jira/browse/WICKET-6776
>             Project: Wicket
>          Issue Type: Improvement
>          Components: wicket-core
>    Affects Versions: 8.7.0, 9.0.0-M5
>            Reporter: Thomas Heigl
>            Priority: Major
>
> Profiling in production showed that {{ServletWebRequest.getUrl()}} and {{ServletWebRequest.getClientUrl()}} are called many times during request processing and allocate a lot of objects.
> Each call to {{ServletWebRequest.getUrl()}} creates a new {{Url}} object that internally creates two new {{ArrayLists}}.
> {{ServletWebRequest.getClientUrl()}} creates a new URL and involves a lot of complex parsing logic.
> Both methods are invoked during request mapping. We have about 350 registered mappers, so these methods are called *at least* 350 times on each request.
> Both values should be cached.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)