From d978ed6d8104e83cf37da73eb76ed92317cf521d Mon Sep 17 00:00:00 2001 From: "Maxim.Mossienko" Date: Wed, 5 Jul 2017 19:58:24 +0200 Subject: [PATCH] [performance] Speed-up resource loading via classloader: Avoid eager loading jar resource bytes. --- .../src/com/intellij/util/lang/JarLoader.java | 50 +++++++++++++++++-- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/platform/util/src/com/intellij/util/lang/JarLoader.java b/platform/util/src/com/intellij/util/lang/JarLoader.java index d95963035c3b..9c54f0ba0a1f 100644 --- a/platform/util/src/com/intellij/util/lang/JarLoader.java +++ b/platform/util/src/com/intellij/util/lang/JarLoader.java @@ -19,12 +19,11 @@ 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 com.intellij.util.ArrayUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; +import java.io.*; import java.net.URL; import java.util.*; import java.util.jar.Attributes; @@ -131,7 +130,7 @@ class JarLoader extends Loader { try { ZipEntry entry = zipFile.getEntry(name); if (entry != null) { - return MemoryResource.load(getBaseURL(), zipFile, entry, myAttributes); + return new MyResource(getBaseURL(), entry); } } finally { @@ -144,6 +143,49 @@ class JarLoader extends Loader { return null; } + + private class MyResource extends Resource { + private final URL myUrl; + private final ZipEntry myEntry; + + public MyResource(URL url, ZipEntry entry) throws IOException { + myUrl = new URL(url, entry.getName()); + myEntry = entry; + } + + @Override + public URL getURL() { + return myUrl; + } + + @Override + public InputStream getInputStream() throws IOException { + return new ByteArrayInputStream(getBytes()); + } + + @Override + public byte[] getBytes() throws IOException { + byte[] result = ArrayUtil.EMPTY_BYTE_ARRAY; + try { + ZipFile file = getZipFile(); + try { + result = FileUtil.loadBytes(file.getInputStream(myEntry), (int)myEntry.getSize()); + } finally { + releaseZipFile(file); + } + } + catch (Exception e) { + error("file: " + myCanonicalFile, e); + } + + return result; + } + + @Override + public String getValue(Attribute key) { + return myAttributes != null ? myAttributes.get(key) : null; + } + } protected void error(String message, Throwable t) { Logger.getInstance(JarLoader.class).error(message, t);