From 4adde48324cc4a2f5f5110c6cdff2a42a76cb6e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?TATSUNO=20=E2=80=9CTaz=E2=80=9D=20Yasuhiro?= Date: Thu, 5 Nov 2026 11:26:14 +0900 Subject: [PATCH] perf(sqs): hash message MD5s with the optimized Digests helper (#7563) --- .../floci/services/sqs/SqsService.java | 18 ++---------- .../floci/services/sqs/model/Message.java | 28 ++----------------- 2 files changed, 5 insertions(+), 41 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/sqs/SqsService.java b/src/main/java/io/github/hectorvent/floci/services/sqs/SqsService.java index 0034fbfdbf..3fb30ebe27 100644 --- a/src/main/java/io/github/hectorvent/floci/services/sqs/SqsService.java +++ b/src/main/java/io/github/hectorvent/floci/services/sqs/SqsService.java @@ -14,6 +14,7 @@ import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsEndpoints; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.AwsRegions; +import io.github.hectorvent.floci.core.common.Digests; import io.github.hectorvent.floci.core.common.PaginatedResult; import io.github.hectorvent.floci.core.common.Pagination; import io.github.hectorvent.floci.core.common.RegionResolver; @@ -37,8 +38,6 @@ import jakarta.inject.Inject; import org.jboss.logging.Logger; import java.nio.charset.StandardCharsets; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; import java.time.Clock; import java.time.Duration; import java.time.Instant; @@ -47,7 +46,6 @@ import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; -import java.util.HexFormat; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -112,7 +110,6 @@ public class SqsService implements Resettable, ResourceProvider { return t; }); private final AtomicLong sequenceCounter = new AtomicLong(0); - private static final HexFormat HEX = HexFormat.of(); private record RedrivePolicy(int maxReceiveCount, String deadLetterTargetArn) { } @@ -1071,7 +1068,7 @@ public class SqsService implements Resettable, ResourceProvider { String dedupId = messageDeduplicationId; if (dedupId == null || dedupId.isEmpty()) { if ("true".equalsIgnoreCase(queue.getAttributes().get("ContentBasedDeduplication"))) { - dedupId = computeMd5(body); + dedupId = Digests.md5Hex(body.getBytes(StandardCharsets.UTF_8)); } else { throw new AwsException("InvalidParameterValue", "The queue should either have ContentBasedDeduplication enabled or " + @@ -1446,17 +1443,6 @@ public class SqsService implements Resettable, ResourceProvider { } } - private static String computeMd5(String input) { - try { - MessageDigest md = MessageDigest.getInstance("MD5"); - byte[] digest = md.digest(input.getBytes(StandardCharsets.UTF_8)); - return HEX.formatHex(digest); - } catch (NoSuchAlgorithmException ignored) { - // MD5 is guaranteed to be available in the standard JDK runtime - return ""; - } - } - public List receiveMessage(String queueUrl, int maxMessages, int visibilityTimeout, Integer waitTimeSeconds, String region) { String storageKey = regionKey(region, queueUrl); diff --git a/src/main/java/io/github/hectorvent/floci/services/sqs/model/Message.java b/src/main/java/io/github/hectorvent/floci/services/sqs/model/Message.java index f6a1911741..0e6c42c97f 100644 --- a/src/main/java/io/github/hectorvent/floci/services/sqs/model/Message.java +++ b/src/main/java/io/github/hectorvent/floci/services/sqs/model/Message.java @@ -2,13 +2,12 @@ package io.github.hectorvent.floci.services.sqs.model; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import io.github.hectorvent.floci.core.common.Digests; import io.quarkus.runtime.annotations.RegisterForReflection; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.nio.charset.StandardCharsets; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; import java.time.Instant; import java.util.ArrayList; import java.util.Collections; @@ -66,7 +65,7 @@ public class Message { this.messageAttributes = new HashMap<>(); this.sentTimestamp = Instant.now(); this.receiveCount = 0; - this.md5OfBody = computeMd5(body); + this.md5OfBody = Digests.md5Hex(body.getBytes(StandardCharsets.UTF_8)); } /** A detached snapshot that retains this message's identity and delivery state. */ @@ -196,7 +195,6 @@ public class Message { return null; } try { - MessageDigest md = MessageDigest.getInstance("MD5"); ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bos); @@ -225,30 +223,10 @@ public class Message { } } - byte[] digest = md.digest(bos.toByteArray()); - StringBuilder sb = new StringBuilder(); - for (byte b : digest) { - sb.append(String.format("%02x", b)); - } - return sb.toString(); + return Digests.md5Hex(bos.toByteArray()); } catch (Exception ignored) { // Attribute serialization failure falls back to null MD5 return null; } } - - private static String computeMd5(String input) { - try { - MessageDigest md = MessageDigest.getInstance("MD5"); - byte[] digest = md.digest(input.getBytes(StandardCharsets.UTF_8)); - StringBuilder sb = new StringBuilder(); - for (byte b : digest) { - sb.append(String.format("%02x", b)); - } - return sb.toString(); - } catch (NoSuchAlgorithmException ignored) { - // MD5 is always available in the standard JDK runtime - return ""; - } - } }