From 44b8257cfd6e1ab0521e58a3702af3c3e57a856e Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Tue, 27 Oct 2015 19:18:34 +0100 Subject: [PATCH] Cleanup (formatting; code reuse) --- .../src/com/intellij/util/lang/JarLoader.java | 80 ++++++------------- 1 file changed, 25 insertions(+), 55 deletions(-) diff --git a/platform/util/src/com/intellij/util/lang/JarLoader.java b/platform/util/src/com/intellij/util/lang/JarLoader.java index 9920fb52bb18..d95963035c3b 100644 --- a/platform/util/src/com/intellij/util/lang/JarLoader.java +++ b/platform/util/src/com/intellij/util/lang/JarLoader.java @@ -18,17 +18,15 @@ package com.intellij.util.lang; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.util.Pair; import com.intellij.openapi.util.io.FileUtil; +import com.intellij.reference.SoftReference; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.lang.ref.SoftReference; import java.net.URL; import java.util.*; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.concurrent.atomic.AtomicLong; import java.util.jar.Attributes; import java.util.jar.JarFile; import java.util.jar.Manifest; @@ -47,7 +45,7 @@ class JarLoader extends Loader { pair(Resource.Attribute.IMPL_VENDOR, Attributes.Name.IMPLEMENTATION_VENDOR)); private final File myCanonicalFile; - private final boolean myCanLockJar; // true implies that the zipfile will not be modified in the lifetime of the JarLoader + private final boolean myCanLockJar; // true implies that the .jar file will not be modified in the lifetime of the JarLoader private SoftReference myMemoryLoader; private volatile SoftReference myZipFileSoftReference; // Used only when myCanLockJar==true private final Map myAttributes; @@ -119,10 +117,6 @@ class JarLoader extends Loader { } } - private static final AtomicInteger myGetResourceRequests = new AtomicInteger(); - private static final AtomicLong myOpenTime = new AtomicLong(); - private static final AtomicLong myCloseTime = new AtomicLong(); - @Override @Nullable Resource getResource(String name, boolean flag) { @@ -152,60 +146,38 @@ class JarLoader extends Loader { } protected void error(String message, Throwable t) { - //Logger.getLogger(JarLoader.class.getName()).log(Level.SEVERE, message, t); Logger.getInstance(JarLoader.class).error(message, t); } - private void releaseZipFile(ZipFile zipFile) throws IOException { - long started = System.nanoTime(); - try { - // Closing of zip file when myCanLockJar=true happens in ZipFile.finalize - if (!myCanLockJar) { - zipFile.close(); - } - } finally { - myCloseTime.addAndGet(System.nanoTime() - started); - } - } + private static final Object ourLock = new Object(); @NotNull private ZipFile getZipFile() throws IOException { - @SuppressWarnings("unused") int requests = myGetResourceRequests.incrementAndGet(); + // This code is executed at least 100K times (O(number of classes needed to load)) and it takes considerable time to open ZipFile's + // such number of times so we store reference to ZipFile if we allowed to lock the file (assume it isn't changed) + if (myCanLockJar) { + ZipFile zipFile = SoftReference.dereference(myZipFileSoftReference); + if (zipFile != null) return zipFile; - long started = System.nanoTime(); - try { + synchronized (ourLock) { + zipFile = SoftReference.dereference(myZipFileSoftReference); + if (zipFile != null) return zipFile; - // This code is executed at least 100K times (O(number of classes needed to load)) and it takes considerable time to open ZipFile's - // such number of times so we store reference to ZipFile if we allowed to lock the file (assume it isn't changed) - if (myCanLockJar) { - SoftReference zipFileSoftReference = myZipFileSoftReference; - if (zipFileSoftReference != null) { - ZipFile existingZipFile = zipFileSoftReference.get(); - if (existingZipFile != null) return existingZipFile; - } - synchronized (ourLock) { - zipFileSoftReference = myZipFileSoftReference; - if (zipFileSoftReference != null) { - ZipFile existingZipFile = zipFileSoftReference.get(); - if (existingZipFile != null) return existingZipFile; - } - // ZipFile's native implementation (ZipFile.c, zip_util.c) has path -> file descriptor cache - ZipFile zipFile = new ZipFile(myCanonicalFile); - myZipFileSoftReference = new SoftReference(zipFile); - return zipFile; - } + // ZipFile's native implementation (ZipFile.c, zip_util.c) has path -> file descriptor cache + zipFile = new ZipFile(myCanonicalFile); + myZipFileSoftReference = new SoftReference(zipFile); + return zipFile; } - else { - return new ZipFile(myCanonicalFile); - } - } finally { - myOpenTime.addAndGet(System.nanoTime() - started); - //if (requests % 1000 == 0) { - // int factor = 1000000; - // System.out.println( - // "Jar loading :" + getClass().getClassLoader() + "," + requests + ", ot:" + (myOpenTime.get() / factor) + ", ct:" + - // (myCloseTime.get() / factor)); - //} + } + else { + return new ZipFile(myCanonicalFile); + } + } + + private void releaseZipFile(ZipFile zipFile) throws IOException { + // Closing of zip file when myCanLockJar=true happens in ZipFile.finalize + if (!myCanLockJar) { + zipFile.close(); } } @@ -213,6 +185,4 @@ class JarLoader extends Loader { public String toString() { return "JarLoader [" + myCanonicalFile + "]"; } - - private static final Object ourLock = new Object(); }