Skip to content

Commit d5717b7

Browse files
academeywilkinsona
authored andcommitted
Remove unused SHA-1 hash from UNPACK markers
In BootZipCopyAction and AbstractJarWriter, SHA-1 hash is calculated for stored entries requiring unpack and set as entry comment. However, the hash isn't used anywhere, just the marker prefix 'UNPACK:' is checked. This commit removes the unnecessary SHA-1 hash calculation which reads the file completely in memory, potentially three times in extreme cases. Now the comment is simply set to 'UNPACK:' without any hash, improving performance. See gh-46520 Signed-off-by: Hyunjoon Choi <hyunjoon@example.com> Signed-off-by: academey <academey@gmail.com>
1 parent 79ad5e6 commit d5717b7

File tree

3 files changed

+9
-40
lines changed

3 files changed

+9
-40
lines changed

build-plugin/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootZipCopyAction.java

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,10 @@
2323
import java.io.OutputStream;
2424
import java.io.OutputStreamWriter;
2525
import java.nio.charset.StandardCharsets;
26-
import java.security.MessageDigest;
27-
import java.security.NoSuchAlgorithmException;
2826
import java.time.OffsetDateTime;
2927
import java.time.ZoneOffset;
3028
import java.util.Collection;
3129
import java.util.HashMap;
32-
import java.util.HexFormat;
3330
import java.util.LinkedHashMap;
3431
import java.util.LinkedHashSet;
3532
import java.util.List;
@@ -585,36 +582,24 @@ private static class StoredEntryPreparator {
585582

586583
private static final int BUFFER_SIZE = 32 * 1024;
587584

588-
private final @Nullable MessageDigest messageDigest;
585+
private final boolean unpack;
589586

590587
private final CRC32 crc = new CRC32();
591588

592589
private long size;
593590

594591
StoredEntryPreparator(InputStream inputStream, boolean unpack) throws IOException {
595-
this.messageDigest = (unpack) ? sha1Digest() : null;
592+
this.unpack = unpack;
596593
try (inputStream) {
597594
load(inputStream);
598595
}
599596
}
600597

601-
private static MessageDigest sha1Digest() {
602-
try {
603-
return MessageDigest.getInstance("SHA-1");
604-
}
605-
catch (NoSuchAlgorithmException ex) {
606-
throw new IllegalStateException(ex);
607-
}
608-
}
609-
610598
private void load(InputStream inputStream) throws IOException {
611599
byte[] buffer = new byte[BUFFER_SIZE];
612600
int bytesRead;
613601
while ((bytesRead = inputStream.read(buffer)) != -1) {
614602
this.crc.update(buffer, 0, bytesRead);
615-
if (this.messageDigest != null) {
616-
this.messageDigest.update(buffer, 0, bytesRead);
617-
}
618603
this.size += bytesRead;
619604
}
620605
}
@@ -624,8 +609,8 @@ void prepareStoredEntry(ZipArchiveEntry entry) {
624609
entry.setCompressedSize(this.size);
625610
entry.setCrc(this.crc.getValue());
626611
entry.setMethod(ZipEntry.STORED);
627-
if (this.messageDigest != null) {
628-
entry.setComment("UNPACK:" + HexFormat.of().formatHex(this.messageDigest.digest()));
612+
if (this.unpack) {
613+
entry.setComment("UNPACK:");
629614
}
630615
}
631616

loader/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/AbstractJarWriter.java

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,9 @@
2424
import java.io.OutputStreamWriter;
2525
import java.net.URL;
2626
import java.nio.charset.StandardCharsets;
27-
import java.security.MessageDigest;
28-
import java.security.NoSuchAlgorithmException;
2927
import java.util.Collection;
3028
import java.util.Enumeration;
3129
import java.util.HashSet;
32-
import java.util.HexFormat;
3330
import java.util.Set;
3431
import java.util.function.Function;
3532
import java.util.jar.JarEntry;
@@ -320,36 +317,24 @@ private static class StoredEntryPreparator {
320317

321318
private static final int BUFFER_SIZE = 32 * 1024;
322319

323-
private final @Nullable MessageDigest messageDigest;
320+
private final boolean unpack;
324321

325322
private final CRC32 crc = new CRC32();
326323

327324
private long size;
328325

329326
StoredEntryPreparator(InputStream inputStream, boolean unpack) throws IOException {
330-
this.messageDigest = (unpack) ? sha1Digest() : null;
327+
this.unpack = unpack;
331328
try (inputStream) {
332329
load(inputStream);
333330
}
334331
}
335332

336-
private static MessageDigest sha1Digest() {
337-
try {
338-
return MessageDigest.getInstance("SHA-1");
339-
}
340-
catch (NoSuchAlgorithmException ex) {
341-
throw new IllegalStateException(ex);
342-
}
343-
}
344-
345333
private void load(InputStream inputStream) throws IOException {
346334
byte[] buffer = new byte[BUFFER_SIZE];
347335
int bytesRead;
348336
while ((bytesRead = inputStream.read(buffer)) != -1) {
349337
this.crc.update(buffer, 0, bytesRead);
350-
if (this.messageDigest != null) {
351-
this.messageDigest.update(buffer, 0, bytesRead);
352-
}
353338
this.size += bytesRead;
354339
}
355340
}
@@ -359,8 +344,8 @@ void prepareStoredEntry(ZipArchiveEntry entry) {
359344
entry.setCompressedSize(this.size);
360345
entry.setCrc(this.crc.getValue());
361346
entry.setMethod(ZipEntry.STORED);
362-
if (this.messageDigest != null) {
363-
entry.setComment("UNPACK:" + HexFormat.of().formatHex(this.messageDigest.digest()));
347+
if (this.unpack) {
348+
entry.setComment("UNPACK:");
364349
}
365350
}
366351

loader/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/AbstractPackagerTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,7 @@ void libraries() throws Exception {
208208
ZipEntry entry = getPackagedEntry("BOOT-INF/lib/" + libJarFile.getName());
209209
assertThat(entry.getTime()).isEqualTo(JAN_1_1985);
210210
entry = getPackagedEntry("BOOT-INF/lib/" + libJarFileToUnpack.getName());
211-
assertThat(entry.getComment()).startsWith("UNPACK:");
212-
assertThat(entry.getComment()).hasSize(47);
211+
assertThat(entry.getComment()).isEqualTo("UNPACK:");
213212
}
214213

215214
@Test

0 commit comments

Comments
 (0)