From 14836dade81bfb0fa7463859b4c788ad425de33c Mon Sep 17 00:00:00 2001 From: Dongie Agnir Date: Fri, 2 Feb 2024 12:26:34 -0800 Subject: [PATCH] Revert "Performance improvement for sigv4 signing. (#4867)" This reverts commit 0ab7f75933656de2ccd4ab1c2029380b341771d8. --- .../internal/signer/V4CanonicalRequest.java | 70 +++++++------------ 1 file changed, 25 insertions(+), 45 deletions(-) diff --git a/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/V4CanonicalRequest.java b/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/V4CanonicalRequest.java index d856b48d260d..fb6d06f7009c 100644 --- a/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/V4CanonicalRequest.java +++ b/core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/V4CanonicalRequest.java @@ -181,10 +181,7 @@ public static List>> getCanonicalHeaders(Map>> 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(":"); @@ -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); } /** @@ -375,17 +365,7 @@ private static String getCanonicalQueryString(SortedMap> 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'; } /**