This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
commit 65c03d0ed152404cc1413b675a297312be17ef02 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Sun Apr 11 09:25:26 2021 +0200 camel-core - Optimize CamelURIParser when uri is already normalized to reduce object allocations. --- .../java/org/apache/camel/util/CamelURIParser.java | 35 ++++++++++++++++++++++ .../java/org/apache/camel/util/URISupport.java | 8 ++++- .../org/apache/camel/util/CamelURIParserTest.java | 21 +++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/core/camel-util/src/main/java/org/apache/camel/util/CamelURIParser.java b/core/camel-util/src/main/java/org/apache/camel/util/CamelURIParser.java index 7ce4393..27f0d47 100644 --- a/core/camel-util/src/main/java/org/apache/camel/util/CamelURIParser.java +++ b/core/camel-util/src/main/java/org/apache/camel/util/CamelURIParser.java @@ -24,10 +24,30 @@ package org.apache.camel.util; */ public final class CamelURIParser { + public static final String[] URI_ALREADY_NORMALIZED = new String[] {}; + private CamelURIParser() { } /** + * Parses the URI (in fast mode). + * + * If this parser cannot parse the uri then <tt>null</tt> is returned. And instead the follow code can be used: + * + * <pre> + * URI u = new URI(UnsafeUriCharactersEncoder.encode(uri, true)); + * </pre> + * + * @param uri the uri + * + * @return <tt>null</tt> if not possible to parse, if the uri is already normalized, then + * {@link #URI_ALREADY_NORMALIZED} is returned, or an array[3] with scheme,path,query + */ + public static String[] fastParseUri(String uri) { + return doParseUri(uri, true); + } + + /** * Parses the URI. * * If this parser cannot parse the uri then <tt>null</tt> is returned. And instead the follow code can be used: @@ -41,6 +61,10 @@ public final class CamelURIParser { * @return <tt>null</tt> if not possible to parse, or an array[3] with scheme,path,query */ public static String[] parseUri(String uri) { + return doParseUri(uri, false); + } + + private static String[] doParseUri(String uri, boolean fastParse) { int schemeStart = 0; int schemeEnd = 0; int pathStart = 0; @@ -84,6 +108,17 @@ public final class CamelURIParser { String scheme = null; if (schemeEnd != 0) { + + // optimized if there are no query and the uri is already in camel style + if (fastParse && queryStart == 0 && pathStart + 1 < len) { + char ch = uri.charAt(schemeEnd); + char ch2 = uri.charAt(pathStart); + char ch3 = uri.charAt(pathStart + 1); + if (ch == ':' && ch2 == '/' && ch3 == '/') { + return URI_ALREADY_NORMALIZED; + } + } + scheme = uri.substring(schemeStart, schemeEnd); } if (scheme == null) { diff --git a/core/camel-util/src/main/java/org/apache/camel/util/URISupport.java b/core/camel-util/src/main/java/org/apache/camel/util/URISupport.java index 67de003..43785c8 100644 --- a/core/camel-util/src/main/java/org/apache/camel/util/URISupport.java +++ b/core/camel-util/src/main/java/org/apache/camel/util/URISupport.java @@ -28,6 +28,8 @@ import java.util.List; import java.util.Map; import java.util.regex.Pattern; +import static org.apache.camel.util.CamelURIParser.URI_ALREADY_NORMALIZED; + /** * URI utilities. */ @@ -568,8 +570,12 @@ public final class URISupport { */ public static String normalizeUri(String uri) throws URISyntaxException, UnsupportedEncodingException { // try to parse using the simpler and faster Camel URI parser - String[] parts = CamelURIParser.parseUri(uri); + String[] parts = CamelURIParser.fastParseUri(uri); if (parts != null) { + // we optimized specially if an empty array is returned + if (parts == URI_ALREADY_NORMALIZED) { + return uri; + } // use the faster and more simple normalizer return doFastNormalizeUri(parts); } else { diff --git a/core/camel-util/src/test/java/org/apache/camel/util/CamelURIParserTest.java b/core/camel-util/src/test/java/org/apache/camel/util/CamelURIParserTest.java index 57fa805..5e5bc92 100644 --- a/core/camel-util/src/test/java/org/apache/camel/util/CamelURIParserTest.java +++ b/core/camel-util/src/test/java/org/apache/camel/util/CamelURIParserTest.java @@ -119,4 +119,25 @@ public class CamelURIParserTest { assertEquals(null, out2[2]); } + @Test + public void testFastParse() throws Exception { + String[] out1 = CamelURIParser.fastParseUri("file:relative"); + assertEquals("file", out1[0]); + assertEquals("relative", out1[1]); + assertEquals(null, out1[2]); + + String[] out2 = CamelURIParser.fastParseUri("file://relative"); + assertEquals(CamelURIParser.URI_ALREADY_NORMALIZED, out2); + + String[] out3 = CamelURIParser.fastParseUri("file:relative?delete=true"); + assertEquals("file", out3[0]); + assertEquals("relative", out3[1]); + assertEquals("delete=true", out3[2]); + + String[] out4 = CamelURIParser.fastParseUri("file://relative?delete=true"); + assertEquals("file", out4[0]); + assertEquals("relative", out4[1]); + assertEquals("delete=true", out4[2]); + } + }