Skip to content

Commit

Permalink
Use String::indexOf(char) intrinsic to speedup decode URI fast-path
Browse files Browse the repository at this point in the history
  • Loading branch information
franz1981 authored and vietj committed Jan 27, 2023
1 parent 07d7411 commit 73202f0
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions src/main/java/io/vertx/core/net/impl/URIDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ public static String decodeURIComponent(String s) {
return decodeURIComponent(s, true);
}

private static int indexOfPercentOrPlus(String s) {
for (int i = 0, size = s.length(); i < size; i++) {
final char c = s.charAt(i);
if (c == '%' || c == '+') {
return i;
}
}
return -1;
}

/**
* Decodes a segment of an URI encoded by a browser.
*
Expand All @@ -55,23 +65,18 @@ public static String decodeURIComponent(String s, boolean plus) {
if (s == null) {
return null;
}

final int size = s.length();
boolean modified = false;
int i;
for (i = 0; i < size; i++) {
final char c = s.charAt(i);
if (c == '%' || (plus && c == '+')) {
modified = true;
break;
}
}
if (!modified) {
int i = !plus ? s.indexOf('%') : indexOfPercentOrPlus(s);
if (i == -1) {
return s;
}
// pack the slowest path away
return decodeAndTransformURIComponent(s, i, plus);
}

private static String decodeAndTransformURIComponent(String s, int i, boolean plus) {
final byte[] buf = s.getBytes(StandardCharsets.UTF_8);
int pos = i; // position in `buf'.
for (; i < size; i++) {
for (int size = s.length(); i < size; i++) {
char c = s.charAt(i);
if (c == '%') {
if (i == size - 1) {
Expand Down

0 comments on commit 73202f0

Please sign in to comment.