From bd3291a8a669e25a61c99a4cd91fc7a87dc87f9b Mon Sep 17 00:00:00 2001 From: seonghobae <8172694+seonghobae@users.noreply.github.com> Date: Tue, 30 Jun 2026 06:28:29 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20[=EC=84=B1=EB=8A=A5=20?= =?UTF-8?q?=EA=B0=9C=EC=84=A0]=20String.format=20=EA=B8=B0=EB=B0=98=20hex?= =?UTF-8?q?=20=EB=B3=80=ED=99=98=EC=9D=84=20HexFormat.of().formatHex()?= =?UTF-8?q?=EB=A1=9C=20=EB=8C=80=EC=B2=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit DefaultDocumentConversionService.java에서 byte array를 hex string으로 변환하는 과정의 성능 병목을 해결합니다. 루프 내에서 String.format("%02x", b)를 반복 호출하는 방식은 많은 메모리 할당과 오버헤드를 발생시킵니다. 이를 Java 내장 API인 java.util.HexFormat.of().formatHex(raw)를 사용하도록 변경하여 처리 속도를 높이고 메모리 사용량을 줄입니다. 관련 학습 내용은 .jules/bolt.md 에 기록하였습니다. --- .jules/bolt.md | 3 +++ .../viewer/service/DefaultDocumentConversionService.java | 7 +------ 2 files changed, 4 insertions(+), 6 deletions(-) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..b9c5f72 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-06-30 - Replace String.format with HexFormat for byte-to-hex conversion +**Learning:** Using a loop with `String.format("%02x", b)` to convert byte arrays to hex strings causes unnecessary memory allocations and is a performance bottleneck in this application. +**Action:** Always use `java.util.HexFormat.of().formatHex(bytes)` instead of manual string formatting loops for converting bytes to hex strings to optimize performance and memory usage. diff --git a/src/main/java/com/clearfolio/viewer/service/DefaultDocumentConversionService.java b/src/main/java/com/clearfolio/viewer/service/DefaultDocumentConversionService.java index 9c59593..3cd4597 100644 --- a/src/main/java/com/clearfolio/viewer/service/DefaultDocumentConversionService.java +++ b/src/main/java/com/clearfolio/viewer/service/DefaultDocumentConversionService.java @@ -121,12 +121,7 @@ private String contentHash(MultipartFile file) { } byte[] raw = digest.digest(); - StringBuilder hex = new StringBuilder(raw.length * 2); - for (byte b : raw) { - hex.append(String.format("%02x", b)); - } - - return hex.toString(); + return java.util.HexFormat.of().formatHex(raw); } catch (NoSuchAlgorithmException ex) { throw new IllegalStateException("SHA-256 digest unavailable", ex); } catch (IOException ex) {