Skip to content
Closed
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
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 2024-05-15 - [Java Hex Conversion Optimization]
**Learning:** Using `String.format("%02x", b)` in a loop to convert byte arrays to hex strings is a significant performance anti-pattern. It creates excessive string allocations and slows down operations. This is a common performance bottleneck specific to this codebase's architecture when generating hashes like SHA-256 for documents.
**Action:** Replace `String.format` loops with Java 17+ `java.util.HexFormat.of().formatHex(bytes)`. This provides a more direct, allocation-efficient conversion. Keep this pattern in mind for any future byte manipulation or hashing implementations.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HexFormat;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
Expand Down Expand Up @@ -121,12 +122,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 HexFormat.of().formatHex(raw);
Comment thread
seonghobae marked this conversation as resolved.
Comment thread
seonghobae marked this conversation as resolved.
} catch (NoSuchAlgorithmException ex) {
throw new IllegalStateException("SHA-256 digest unavailable", ex);
} catch (IOException ex) {
Expand Down
Loading