Skip to content

Commit

Permalink
Merge pull request #2889 from aws/dongie/revert-2888
Browse files Browse the repository at this point in the history
Revert "Performance improvement for sigv4 signing. (#4867)"
  • Loading branch information
dagnir authored Feb 2, 2024
2 parents 837d372 + 14836da commit 7075421
Showing 1 changed file with 25 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,7 @@ public static List<Pair<String, List<String>>> getCanonicalHeaders(Map<String, L
* Each header-value pair is separated by a newline.
*/
public static String getCanonicalHeadersString(List<Pair<String, List<String>>> canonicalHeaders) {
// 2048 chosen experimentally to avoid always needing to resize the string builder's internal byte array.
// The minimal DynamoDB get-item request at the time of testing used ~1100 bytes. 2048 was chosen as the
// next-highest power-of-two.
StringBuilder result = new StringBuilder(2048);
StringBuilder result = new StringBuilder(512);
canonicalHeaders.forEach(header -> {
result.append(header.left());
result.append(":");
Expand Down Expand Up @@ -249,42 +246,35 @@ private static String getCanonicalRequestString(String httpMethod, String canoni
* Matcher object as well.
*/
private static void addAndTrim(StringBuilder result, String value) {
int start = 0;
int valueLength = value.length();

// Find first non-whitespace
while (isWhiteSpace(value.charAt(start))) {
++start;
if (start > valueLength) {
return;
}
}

// Add things word-by-word
int lastWordStart = start;
boolean lastWasWhitespace = false;
for (int i = start; i < valueLength; i++) {
char c = value.charAt(i);

if (isWhiteSpace(c)) {
if (!lastWasWhitespace) {
// End of word, add word
result.append(value, lastWordStart, i);
lastWasWhitespace = true;
int lengthBefore = result.length();
boolean isStart = true;
boolean previousIsWhiteSpace = false;

for (int i = 0; i < value.length(); i++) {
char ch = value.charAt(i);
if (isWhiteSpace(ch)) {
if (previousIsWhiteSpace || isStart) {
continue;
}
result.append(' ');
previousIsWhiteSpace = true;
} else {
if (lastWasWhitespace) {
// Start of new word, add space
result.append(' ');
lastWordStart = i;
lastWasWhitespace = false;
}
result.append(ch);
isStart = false;
previousIsWhiteSpace = false;
}
}

if (!lastWasWhitespace) {
result.append(value, lastWordStart, valueLength);
if (lengthBefore == result.length()) {
return;
}

int lastNonWhitespaceChar = result.length() - 1;
while (isWhiteSpace(result.charAt(lastNonWhitespaceChar))) {
--lastNonWhitespaceChar;
}

result.setLength(lastNonWhitespaceChar + 1);
}

/**
Expand Down Expand Up @@ -375,17 +365,7 @@ private static String getCanonicalQueryString(SortedMap<String, List<String>> ca
}

private static boolean isWhiteSpace(char ch) {
switch (ch) {
case ' ':
case '\t':
case '\n':
case '\u000b':
case '\r':
case '\f':
return true;
default:
return false;
}
return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\u000b' || ch == '\r' || ch == '\f';
}

/**
Expand Down

0 comments on commit 7075421

Please sign in to comment.