Skip to content
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

Ant: fix GeneratedFilesHelper CRC cache #8190

Merged
merged 1 commit into from
Feb 7, 2025
Merged
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
2 changes: 1 addition & 1 deletion ide/project.ant/nbproject/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ antsrc.cp=\
${openide.filesystems.dir}/core/org-openide-filesystems.jar

javac.compilerargs=-Xlint -Xlint:-serial
javac.source=1.8
javac.release=17
javadoc.arch=${basedir}/arch.xml
javadoc.apichanges=${basedir}/apichanges.xml

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -658,16 +658,24 @@ static String computeCrc32(InputStream is) throws IOException {

// #50440 - cache CRC32's for various files to save time esp. during startup.

private static final Map<URL,String> crcCache = new HashMap<URL,String>();
private static final Map<URL,Long> crcCacheTimestampsXorSizes = new HashMap<URL,Long>();
private static final Map<URL, String> crcCache = new HashMap<>();
private static final Map<URL, TimeStampAndSize> fileFootprintCache = new HashMap<>();
private record TimeStampAndSize(long modified, long size) {
TimeStampAndSize(FileObject fo) {
this(fo.lastModified().getTime(), fo.getSize());
}
TimeStampAndSize(File f) {
this(f.lastModified(), f.length());
}
}

/** Try to find a CRC in the cache according to location of file and last mod time xor size. */
private static synchronized String findCachedCrc32(URL u, long footprint) {
private static synchronized String findCachedCrc32(URL u, TimeStampAndSize footprint) {
String crc = crcCache.get(u);
if (crc != null) {
Long l = crcCacheTimestampsXorSizes.get(u);
assert l != null;
if (l == footprint) {
TimeStampAndSize fp = fileFootprintCache.get(u);
assert fp != null;
if (fp.equals(footprint)) {
// Cache hit.
return crc;
}
Expand All @@ -677,16 +685,16 @@ private static synchronized String findCachedCrc32(URL u, long footprint) {
}

/** Cache a known CRC for a file, using current last mod time xor size. */
private static synchronized void cacheCrc32(String crc, URL u, long footprint) {
private static synchronized void cacheCrc32(String crc, URL u, TimeStampAndSize footprint) {
crcCache.put(u, crc);
crcCacheTimestampsXorSizes.put(u, footprint);
fileFootprintCache.put(u, footprint);
}

/** Find (maybe cached) CRC for a file, using a preexisting input stream (not closed by this method). */
private static String getCrc32(InputStream is, FileObject fo) throws IOException {
URL u = fo.toURL();
fo.refresh(); // in case was written on disk and we did not notice yet...
long footprint = fo.lastModified().getTime() ^ fo.getSize();
TimeStampAndSize footprint = new TimeStampAndSize(fo);
String crc = findCachedCrc32(u, footprint);
if (crc == null) {
crc = computeCrc32(is);
Expand All @@ -696,25 +704,21 @@ private static String getCrc32(InputStream is, FileObject fo) throws IOException
}

/** Find the time the file this URL represents was last modified xor its size, if possible. */
private static long checkFootprint(URL u) {
private static TimeStampAndSize checkFootprint(URL u) {
File f = FileUtil.archiveOrDirForURL(u);
if (f != null) {
return f.lastModified() ^ f.length();
} else {
return 0L;
}
return f != null ? new TimeStampAndSize(f) : null;
}

/** Find (maybe cached) CRC for a URL, using a preexisting input stream (not closed by this method). */
private static String getCrc32(InputStream is, URL u) throws IOException {
long footprint = checkFootprint(u);
TimeStampAndSize footprint = checkFootprint(u);
String crc = null;
if (footprint != 0L) {
if (footprint != null) {
crc = findCachedCrc32(u, footprint);
}
if (crc == null) {
crc = computeCrc32(is);
if (footprint != 0L) {
if (footprint != null) {
cacheCrc32(crc, u, footprint);
}
}
Expand All @@ -725,7 +729,7 @@ private static String getCrc32(InputStream is, URL u) throws IOException {
private static String getCrc32(FileObject fo) throws IOException {
URL u = fo.toURL();
fo.refresh();
long footprint = fo.lastModified().getTime() ^ fo.getSize();
TimeStampAndSize footprint = new TimeStampAndSize(fo);
String crc = findCachedCrc32(u, footprint);
if (crc == null) {
InputStream is = fo.getInputStream();
Expand All @@ -741,16 +745,16 @@ private static String getCrc32(FileObject fo) throws IOException {

/** Find (maybe cached) CRC for a URL. Will open its own input stream. */
private static String getCrc32(URL u) throws IOException {
long footprint = checkFootprint(u);
TimeStampAndSize footprint = checkFootprint(u);
String crc = null;
if (footprint != 0L) {
if (footprint != null) {
crc = findCachedCrc32(u, footprint);
}
if (crc == null) {
InputStream is = u.openStream();
try {
crc = computeCrc32(new BufferedInputStream(is));
if (footprint != 0L) {
if (footprint != null) {
cacheCrc32(crc, u, footprint);
}
} finally {
Expand Down
Loading