Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[RESTEASY-2659] Performance improvements (#2479)
- Avoid unnecessary use of URI.create - rather do more lazily
- Multivalued maps will 99% of the time have 1 or maybe 2 entries for a key so don't allocate memory for 10
- Avoid unnecessary matcher creation when decoding when a string does not contain % in it - which is the more likely case for URI's to Rest resources

These changes increase throughput by about 2% on a simple Rest service.

Co-authored-by: Paul Carter-Brown <paul.carter-brown@jini.guru>
  • Loading branch information
bcluap and Paul Carter-Brown committed Jul 28, 2020
1 parent c05bc41 commit cfc1109
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
Expand Up @@ -16,7 +16,7 @@ public class MultivaluedMapImpl<K, V> extends HashMap<K, List<V>> implements Mul
{
public void putSingle(K key, V value)
{
List<V> list = new ArrayList<V>();
List<V> list = new ArrayList<V>(2);
list.add(value);
put(key, list);
}
Expand Down Expand Up @@ -75,7 +75,7 @@ public final List<V> getList(K key)
{
List<V> list = get(key);
if (list == null)
put(key, list = new ArrayList<V>());
put(key, list = new ArrayList<V>(2));
return list;
}

Expand Down
Expand Up @@ -81,17 +81,6 @@ protected void initialize(String absoluteUri, String contextPath, InitData initD
this.matchingPath = initData.getMatchingPath();
}

private void processUris() {
requestURI = URI.create(absoluteString);
absolutePath = queryIdx < 0 ? requestURI : URI.create(absoluteString.substring(0, queryIdx));
baseURI = absolutePath;
String tmpContextPath = contextPath;
if (!tmpContextPath.endsWith("/")) tmpContextPath += "/";
if (!tmpContextPath.startsWith("/")) tmpContextPath = "/" + tmpContextPath;
String baseString = absoluteString.substring(0, pathStart);
baseString += tmpContextPath;
baseURI = URI.create(baseString);
}

protected void initialize(CharSequence absoluteUri, String queryString, String contextPath)
{
Expand Down Expand Up @@ -273,8 +262,7 @@ public String getMatchingPath()
*/
public void setRequestUri(URI relative)
{
if (baseURI == null) processUris();
setUri(baseURI, relative);
setUri(getBaseUri(), relative);
}

public String getPath()
Expand All @@ -301,7 +289,9 @@ public List<PathSegment> getPathSegments(boolean decode)

public URI getRequestUri()
{
if (requestURI == null) processUris();
if (requestURI == null) {
requestURI = URI.create(absoluteString);
}
return requestURI;
}

Expand All @@ -312,7 +302,9 @@ public UriBuilder getRequestUriBuilder()

public URI getAbsolutePath()
{
if (absolutePath == null) processUris();
if (absolutePath == null) {
absolutePath = queryIdx < 0 ? getRequestUri() : URI.create(absoluteString.substring(0, queryIdx));
}
return absolutePath;
}

Expand All @@ -323,7 +315,14 @@ public UriBuilder getAbsolutePathBuilder()

public URI getBaseUri()
{
if (baseURI == null) processUris();
if (baseURI == null) {
String tmpContextPath = contextPath;
if (!tmpContextPath.endsWith("/")) tmpContextPath += "/";
if (!tmpContextPath.startsWith("/")) tmpContextPath = "/" + tmpContextPath;
String baseString = absoluteString.substring(0, pathStart);
baseString += tmpContextPath;
baseURI = URI.create(baseString);
}
return baseURI;
}

Expand Down
Expand Up @@ -214,6 +214,9 @@ public static String encodeQueryParam(String value)

public static String decodePath(String path)
{
if (path.indexOf('%') == -1) {
return path;
}
Matcher matcher = encodedCharsMulti.matcher(path);
int start=0;
StringBuilder builder = new StringBuilder();
Expand Down

0 comments on commit cfc1109

Please sign in to comment.