-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
8346239: Improve memory efficiency of JimageDiffGenerator #22835
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,9 @@ | |
*/ | ||
package jdk.tools.jlink.internal.runtimelink; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.UncheckedIOException; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
|
@@ -49,6 +52,7 @@ public class JimageDiffGenerator { | |
public interface ImageResource extends AutoCloseable { | ||
public List<String> getEntries(); | ||
public byte[] getResourceBytes(String name); | ||
public InputStream getResource(String name); | ||
} | ||
|
||
/** | ||
|
@@ -71,7 +75,6 @@ public List<ResourceDiff> generateDiff(ImageResource base, ImageResource image) | |
resources.addAll(image.getEntries()); | ||
baseResources = base.getEntries(); | ||
for (String item: baseResources) { | ||
byte[] baseBytes = base.getResourceBytes(item); | ||
// First check that every item in the base image exist in | ||
// the optimized image as well. If it does not, it's a removed | ||
// item in the optimized image. | ||
|
@@ -82,19 +85,18 @@ public List<ResourceDiff> generateDiff(ImageResource base, ImageResource image) | |
ResourceDiff.Builder builder = new ResourceDiff.Builder(); | ||
ResourceDiff diff = builder.setKind(ResourceDiff.Kind.REMOVED) | ||
.setName(item) | ||
.setResourceBytes(baseBytes) | ||
.setResourceBytes(base.getResourceBytes(item)) | ||
.build(); | ||
diffs.add(diff); | ||
continue; | ||
} | ||
// Verify resource bytes are equal if present in both images | ||
boolean contentEquals = Arrays.equals(baseBytes, image.getResourceBytes(item)); | ||
if (!contentEquals) { | ||
if (!compareStreams(base.getResource(item), image.getResource(item))) { | ||
// keep track of original bytes (non-optimized) | ||
ResourceDiff.Builder builder = new ResourceDiff.Builder(); | ||
ResourceDiff diff = builder.setKind(ResourceDiff.Kind.MODIFIED) | ||
.setName(item) | ||
.setResourceBytes(baseBytes) | ||
.setResourceBytes(base.getResourceBytes(item)) | ||
.build(); | ||
diffs.add(diff); | ||
} | ||
|
@@ -112,4 +114,51 @@ public List<ResourceDiff> generateDiff(ImageResource base, ImageResource image) | |
return diffs; | ||
} | ||
|
||
/** | ||
* Compare the contents of the two input streams (byte-by-byte). | ||
* | ||
* @param is1 The first input stream | ||
* @param is2 The second input stream | ||
* @return {@code true} iff the two streams contain the same number of | ||
* bytes and each byte of the streams are equal. {@code false} | ||
* otherwise. | ||
*/ | ||
private boolean compareStreams(InputStream is1, InputStream is2) { | ||
byte[] buf1 = new byte[1024]; | ||
byte[] buf2 = new byte[1024]; | ||
int bytesRead1, bytesRead2 = 0; | ||
try { | ||
try (is1; is2) { | ||
while ((bytesRead1 = is1.read(buf1)) != -1 && | ||
(bytesRead2 = is2.read(buf2)) != -1) { | ||
if (bytesRead1 != bytesRead2) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test seems to assume there won't be any short reads. That might be true in practice, but for defensive programming reasons it would be safer to use |
||
return false; | ||
} | ||
if (bytesRead1 == buf1.length) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be simplified by using |
||
if (!Arrays.equals(buf1, buf2)) { | ||
return false; | ||
} | ||
} else { | ||
for (int i = 0; i < bytesRead1; i++) { | ||
if (buf1[i] != buf2[i]) { | ||
return false; | ||
} | ||
} | ||
} | ||
} | ||
// ensure we read both to the end | ||
if (bytesRead1 == -1) { | ||
bytesRead2 = is2.read(buf2); | ||
if (bytesRead2 != -1) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
} | ||
} catch (IOException e) { | ||
throw new UncheckedIOException("IO exception when comparing bytes", e); | ||
} | ||
return false; | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.