From ad0383734ebdbfb919ba14ca53f7e40501f65cee Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Wed, 30 Jun 2010 18:40:54 -0700 Subject: [PATCH] Lazily allocate Deflater in PackWriter Only allocate the Deflater if we can't reuse everything, but also make sure we release it when we release the PackWriter's resources. Change-Id: I16a32b94647af0778658eda87acbafc9a25b314a Signed-off-by: Shawn O. Pearce --- .../eclipse/jgit/storage/pack/PackWriter.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/storage/pack/PackWriter.java b/org.eclipse.jgit/src/org/eclipse/jgit/storage/pack/PackWriter.java index c851238c9c..d39d574814 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/storage/pack/PackWriter.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/storage/pack/PackWriter.java @@ -182,7 +182,9 @@ public class PackWriter { // edge objects for thin packs private final ObjectIdSubclassMap edgeObjects = new ObjectIdSubclassMap(); - private final Deflater deflater; + private int compressionLevel; + + private Deflater myDeflater; private final ObjectReader reader; @@ -252,7 +254,7 @@ public class PackWriter { reuseSupport = null; final CoreConfig coreConfig = configOf(repo).get(CoreConfig.KEY); - deflater = new Deflater(coreConfig.getCompression()); + compressionLevel = coreConfig.getCompression(); outputVersion = coreConfig.getPackIndexVersion(); } @@ -639,6 +641,10 @@ public class PackWriter { /** Release all resources used by this writer. */ public void release() { reader.release(); + if (myDeflater != null) { + myDeflater.end(); + myDeflater = null; + } } private void searchForReuse(ProgressMonitor compressMonitor) @@ -745,6 +751,7 @@ public class PackWriter { private void writeWholeObjectDeflate(PackOutputStream out, final ObjectToPack otp) throws IOException { + final Deflater deflater = deflater(); final ObjectLoader loader = reader.open(otp, otp.getType()); final byte[] data = loader.getCachedBytes(); out.writeHeader(otp, data.length); @@ -760,6 +767,12 @@ public class PackWriter { } while (!deflater.finished()); } + private Deflater deflater() { + if (myDeflater == null) + myDeflater = new Deflater(compressionLevel); + return myDeflater; + } + private void writeChecksum(PackOutputStream out) throws IOException { packcsum = out.getDigest(); out.write(packcsum); -- 2.39.5