From 8b9aa1fd27230e23bb2756d79e27de55fe536b59 Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Tue, 28 Feb 2012 22:24:26 +0200 Subject: [PATCH] lazily allocate the LZF buffer if its only needed --- .../common/io/stream/CachedStreamOutput.java | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/elasticsearch/common/io/stream/CachedStreamOutput.java b/src/main/java/org/elasticsearch/common/io/stream/CachedStreamOutput.java index 28dfaed4feb..65c5075110e 100644 --- a/src/main/java/org/elasticsearch/common/io/stream/CachedStreamOutput.java +++ b/src/main/java/org/elasticsearch/common/io/stream/CachedStreamOutput.java @@ -34,19 +34,26 @@ public class CachedStreamOutput { private static Entry newEntry() { BytesStreamOutput bytes = new BytesStreamOutput(); HandlesStreamOutput handles = new HandlesStreamOutput(bytes); - LZFStreamOutput lzf = new LZFStreamOutput(bytes, true); - return new Entry(bytes, handles, lzf); + return new Entry(bytes, handles); } public static class Entry { private final BytesStreamOutput bytes; private final HandlesStreamOutput handles; - private final LZFStreamOutput lzf; + private LZFStreamOutput lzf; - Entry(BytesStreamOutput bytes, HandlesStreamOutput handles, LZFStreamOutput lzf) { + Entry(BytesStreamOutput bytes, HandlesStreamOutput handles) { this.bytes = bytes; this.handles = handles; - this.lzf = lzf; + } + + // lazily initialize LZF, so we won't allocate it if we don't do + // any compression + private LZFStreamOutput lzf() { + if (lzf == null) { + lzf = new LZFStreamOutput(bytes, true); + } + return lzf; } /** @@ -65,11 +72,13 @@ public class CachedStreamOutput { } public LZFStreamOutput cachedLZFBytes() throws IOException { + LZFStreamOutput lzf = lzf(); lzf.reset(); return lzf; } public HandlesStreamOutput cachedHandlesLzfBytes() throws IOException { + LZFStreamOutput lzf = lzf(); handles.reset(lzf); return handles; }