Skip to content

Performance improvement for sigv4 signing. #4867

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,10 @@ 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) {
StringBuilder result = new StringBuilder(512);
// 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a note about why we chose this number?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

canonicalHeaders.forEach(header -> {
result.append(header.left());
result.append(":");
Expand Down Expand Up @@ -246,35 +249,42 @@ private static String getCanonicalRequestString(String httpMethod, String canoni
* Matcher object as well.
*/
private static void addAndTrim(StringBuilder result, String value) {
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 {
result.append(ch);
isStart = false;
previousIsWhiteSpace = false;
int start = 0;
int valueLength = value.length();

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

if (lengthBefore == result.length()) {
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;
}
} else {
if (lastWasWhitespace) {
// Start of new word, add space
result.append(' ');
lastWordStart = i;
lastWasWhitespace = false;
}
}
}

int lastNonWhitespaceChar = result.length() - 1;
while (isWhiteSpace(result.charAt(lastNonWhitespaceChar))) {
--lastNonWhitespaceChar;
if (!lastWasWhitespace) {
result.append(value, lastWordStart, valueLength);
}

result.setLength(lastNonWhitespaceChar + 1);
}

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

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

/**
Expand Down