Skip to content

Performance improvement for sigv4 signing. #4891

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 1 commit into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
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);
canonicalHeaders.forEach(header -> {
result.append(header.left());
result.append(":");
Expand Down Expand Up @@ -246,35 +249,45 @@ 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 valueLength = value.length();
if (valueLength == 0) {
return;
}

if (lengthBefore == result.length()) {
return;
int start = 0;
// Find first non-whitespace
while (isWhiteSpace(value.charAt(start))) {
++start;
if (start >= valueLength) {
return;
}
}

int lastNonWhitespaceChar = result.length() - 1;
while (isWhiteSpace(result.charAt(lastNonWhitespaceChar))) {
--lastNonWhitespaceChar;
// 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;
}
}
}

result.setLength(lastNonWhitespaceChar + 1);
if (!lastWasWhitespace) {
result.append(value, lastWordStart, valueLength);
}
}

/**
Expand Down Expand Up @@ -365,7 +378,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
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,36 @@ public void canonicalRequest_withSpacedHeaders_shouldStripWhitespace() {
assertEquals("PUT\n/\n\nfoo:bar baz\n\nfoo\nsha-256", cr.getCanonicalRequestString());
}

@Test
public void canonicalRequest_withEmptyHeaders_shouldSucceed() {
SdkHttpRequest request = SdkHttpRequest.builder()
.protocol("https")
.host("localhost")
.method(SdkHttpMethod.PUT)
.putHeader("foo", "")
.build();
V4CanonicalRequest cr = new V4CanonicalRequest(request, "sha-256",
new V4CanonicalRequest.Options(true,
true));

assertEquals("PUT\n/\n\nfoo:\n\nfoo\nsha-256", cr.getCanonicalRequestString());
}

@Test
public void canonicalRequest_withWhitespaceHeaders_shouldSucceed() {
SdkHttpRequest request = SdkHttpRequest.builder()
.protocol("https")
.host("localhost")
.method(SdkHttpMethod.PUT)
.putHeader("foo", " ")
.build();
V4CanonicalRequest cr = new V4CanonicalRequest(request, "sha-256",
new V4CanonicalRequest.Options(true,
true));

assertEquals("PUT\n/\n\nfoo:\n\nfoo\nsha-256", cr.getCanonicalRequestString());
}

@Test
public void canonicalRequest_WithNullParamValue_shouldIncludeEquals() {
SdkHttpRequest request = SdkHttpRequest.builder()
Expand Down