Skip to content

Commit 140226a

Browse files
authored
Improve performance of encoding Uris (#660)
* Improved performance of UriParser.normalisePath * Improved performance of UriParser.extractScheme * Improved performance of UriParser.encode * Improved performance of UriParser.encode Make tests compile and uncommented optimisation * Fix CRLF to LF
1 parent d3970c6 commit 140226a

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/UriParser.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ public static String encode(final String decodedStr, final char[] reserved) {
217217
}
218218
final StringBuilder buffer = new StringBuilder(decodedStr);
219219
encode(buffer, 0, buffer.length(), reserved);
220+
if (buffer.length() == decodedStr.length()) { // No encoding happened
221+
return decodedStr;
222+
}
220223
return buffer.toString();
221224
}
222225

commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/UriParserBenchmark.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,18 @@
2929
public class UriParserBenchmark {
3030

3131
private static final String PATH_TO_NORMALIZE = "file:///this/../is/a%2flong%2Fpath/./for testing/normlisePath%2fmethod.txt";
32+
private static final String PATH_TO_ENCODE = "file:///this/is/path/to/encode/for/testing/encode.perf";
33+
private static final char[] ENCODE_RESERVED = new char[] {' ', '#'};
3234

3335
@Benchmark
3436
public void normalisePath() throws FileSystemException {
3537
final StringBuilder path = new StringBuilder(PATH_TO_NORMALIZE);
3638
UriParser.fixSeparators(path);
3739
UriParser.normalisePath(path);
3840
}
41+
42+
@Benchmark
43+
public void encode() throws FileSystemException {
44+
UriParser.encode(PATH_TO_ENCODE, ENCODE_RESERVED);
45+
}
3946
}

0 commit comments

Comments
 (0)