From 35eaec7f521b2de38e47ddf99005b3d70712d7be Mon Sep 17 00:00:00 2001 From: kzhsw <120431159+kzhsw@users.noreply.github.com> Date: Sat, 3 Feb 2024 09:27:34 +0800 Subject: [PATCH 1/3] Optimize performance for S3Escaper.encode Replace `String.replaceAll` with StringBuilder for better performance. See also benchmark at . Close https://github.com/minio/minio-java/issues/1528 --- api/src/main/java/io/minio/S3Escaper.java | 44 +++++++++++++---------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/api/src/main/java/io/minio/S3Escaper.java b/api/src/main/java/io/minio/S3Escaper.java index be3d34b11..481fb4100 100644 --- a/api/src/main/java/io/minio/S3Escaper.java +++ b/api/src/main/java/io/minio/S3Escaper.java @@ -28,24 +28,32 @@ public static String encode(String str) { return ""; } - return ESCAPER - .escape(str) - .replaceAll("\\!", "%21") - .replaceAll("\\$", "%24") - .replaceAll("\\&", "%26") - .replaceAll("\\'", "%27") - .replaceAll("\\(", "%28") - .replaceAll("\\)", "%29") - .replaceAll("\\*", "%2A") - .replaceAll("\\+", "%2B") - .replaceAll("\\,", "%2C") - .replaceAll("\\/", "%2F") - .replaceAll("\\:", "%3A") - .replaceAll("\\;", "%3B") - .replaceAll("\\=", "%3D") - .replaceAll("\\@", "%40") - .replaceAll("\\[", "%5B") - .replaceAll("\\]", "%5D"); + str = ESCAPER.escape(str); + StringBuilder encoded = new StringBuilder(str.length() + Math.max(str.length() >> 3, 4)); + + for (char ch : str.toCharArray()) { + switch (ch) { + case '!': encoded.append("%21"); break; + case '$': encoded.append("%24"); break; + case '&': encoded.append("%26"); break; + case '\'': encoded.append("%27"); break; + case '(': encoded.append("%28"); break; + case ')': encoded.append("%29"); break; + case '*': encoded.append("%2A"); break; + case '+': encoded.append("%2B"); break; + case ',': encoded.append("%2C"); break; + case '/': encoded.append("%2F"); break; + case ':': encoded.append("%3A"); break; + case ';': encoded.append("%3B"); break; + case '=': encoded.append("%3D"); break; + case '@': encoded.append("%40"); break; + case '[': encoded.append("%5B"); break; + case ']': encoded.append("%5D"); break; + default: encoded.append(ch); + } + } + + return encoded.toString(); } /** Returns S3 encoded string of given path where multiple '/' are trimmed. */ From ef5bdd8b4a080d512079a7af9170142114c53c8a Mon Sep 17 00:00:00 2001 From: Bala FA Date: Thu, 8 Feb 2024 07:37:45 +0530 Subject: [PATCH 2/3] Update api/src/main/java/io/minio/S3Escaper.java --- api/src/main/java/io/minio/S3Escaper.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/api/src/main/java/io/minio/S3Escaper.java b/api/src/main/java/io/minio/S3Escaper.java index 481fb4100..429799ac6 100644 --- a/api/src/main/java/io/minio/S3Escaper.java +++ b/api/src/main/java/io/minio/S3Escaper.java @@ -28,10 +28,8 @@ public static String encode(String str) { return ""; } - str = ESCAPER.escape(str); - StringBuilder encoded = new StringBuilder(str.length() + Math.max(str.length() >> 3, 4)); - - for (char ch : str.toCharArray()) { + StringBuilder encoded = new StringBuilder(); + for (char ch : ESCAPER.escape(str).toCharArray()) { switch (ch) { case '!': encoded.append("%21"); break; case '$': encoded.append("%24"); break; From 71758bd9fecb9939a8f6fad227900b2267c36551 Mon Sep 17 00:00:00 2001 From: Bala FA Date: Thu, 8 Feb 2024 07:44:47 +0530 Subject: [PATCH 3/3] Update api/src/main/java/io/minio/S3Escaper.java --- api/src/main/java/io/minio/S3Escaper.java | 72 ++++++++++++++++------- 1 file changed, 52 insertions(+), 20 deletions(-) diff --git a/api/src/main/java/io/minio/S3Escaper.java b/api/src/main/java/io/minio/S3Escaper.java index 429799ac6..028dd83b1 100644 --- a/api/src/main/java/io/minio/S3Escaper.java +++ b/api/src/main/java/io/minio/S3Escaper.java @@ -28,30 +28,62 @@ public static String encode(String str) { return ""; } - StringBuilder encoded = new StringBuilder(); + StringBuilder builder = new StringBuilder(); for (char ch : ESCAPER.escape(str).toCharArray()) { switch (ch) { - case '!': encoded.append("%21"); break; - case '$': encoded.append("%24"); break; - case '&': encoded.append("%26"); break; - case '\'': encoded.append("%27"); break; - case '(': encoded.append("%28"); break; - case ')': encoded.append("%29"); break; - case '*': encoded.append("%2A"); break; - case '+': encoded.append("%2B"); break; - case ',': encoded.append("%2C"); break; - case '/': encoded.append("%2F"); break; - case ':': encoded.append("%3A"); break; - case ';': encoded.append("%3B"); break; - case '=': encoded.append("%3D"); break; - case '@': encoded.append("%40"); break; - case '[': encoded.append("%5B"); break; - case ']': encoded.append("%5D"); break; - default: encoded.append(ch); + case '!': + builder.append("%21"); + break; + case '$': + builder.append("%24"); + break; + case '&': + builder.append("%26"); + break; + case '\'': + builder.append("%27"); + break; + case '(': + builder.append("%28"); + break; + case ')': + builder.append("%29"); + break; + case '*': + builder.append("%2A"); + break; + case '+': + builder.append("%2B"); + break; + case ',': + builder.append("%2C"); + break; + case '/': + builder.append("%2F"); + break; + case ':': + builder.append("%3A"); + break; + case ';': + builder.append("%3B"); + break; + case '=': + builder.append("%3D"); + break; + case '@': + builder.append("%40"); + break; + case '[': + builder.append("%5B"); + break; + case ']': + builder.append("%5D"); + break; + default: + builder.append(ch); } } - - return encoded.toString(); + return builder.toString(); } /** Returns S3 encoded string of given path where multiple '/' are trimmed. */