]> source.dussan.org Git - jgit.git/commitdiff
Lazily allocate Deflater in PackWriter 23/1023/1
authorShawn O. Pearce <spearce@spearce.org>
Thu, 1 Jul 2010 01:40:54 +0000 (18:40 -0700)
committerShawn O. Pearce <spearce@spearce.org>
Thu, 1 Jul 2010 01:40:54 +0000 (18:40 -0700)
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 <spearce@spearce.org>
org.eclipse.jgit/src/org/eclipse/jgit/storage/pack/PackWriter.java

index c851238c9c1ac1f59169aa3538c47cc9ae7e6d10..d39d5748148665a15befbc1eed425bc21824448c 100644 (file)
@@ -182,7 +182,9 @@ public class PackWriter {
        // edge objects for thin packs
        private final ObjectIdSubclassMap<ObjectId> edgeObjects = new ObjectIdSubclassMap<ObjectId>();
 
-       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);