improve thread cached bytes allocation

This commit is contained in:
kimchy 2010-08-14 02:26:12 +03:00
parent 7c5f0fe405
commit e1226626bc
1 changed files with 16 additions and 10 deletions

View File

@ -34,15 +34,21 @@ public class BytesStreamOutput extends StreamOutput {
*/ */
public static class Cached { public static class Cached {
private static final ThreadLocal<ThreadLocals.CleanableValue<BytesStreamOutput>> cache = new ThreadLocal<ThreadLocals.CleanableValue<BytesStreamOutput>>() { static class Entry {
@Override protected ThreadLocals.CleanableValue<BytesStreamOutput> initialValue() { final BytesStreamOutput bytes;
return new ThreadLocals.CleanableValue<BytesStreamOutput>(new BytesStreamOutput()); final HandlesStreamOutput handles;
}
};
private static final ThreadLocal<ThreadLocals.CleanableValue<HandlesStreamOutput>> cacheHandles = new ThreadLocal<ThreadLocals.CleanableValue<HandlesStreamOutput>>() { Entry(BytesStreamOutput bytes, HandlesStreamOutput handles) {
@Override protected ThreadLocals.CleanableValue<HandlesStreamOutput> initialValue() { this.bytes = bytes;
return new ThreadLocals.CleanableValue<HandlesStreamOutput>(new HandlesStreamOutput(new BytesStreamOutput())); this.handles = handles;
}
}
private static final ThreadLocal<ThreadLocals.CleanableValue<Entry>> cache = new ThreadLocal<ThreadLocals.CleanableValue<Entry>>() {
@Override protected ThreadLocals.CleanableValue<Entry> initialValue() {
BytesStreamOutput bytes = new BytesStreamOutput();
HandlesStreamOutput handles = new HandlesStreamOutput(bytes);
return new ThreadLocals.CleanableValue<Entry>(new Entry(bytes, handles));
} }
}; };
@ -50,13 +56,13 @@ public class BytesStreamOutput extends StreamOutput {
* Returns the cached thread local byte stream, with its internal stream cleared. * Returns the cached thread local byte stream, with its internal stream cleared.
*/ */
public static BytesStreamOutput cached() { public static BytesStreamOutput cached() {
BytesStreamOutput os = cache.get().get(); BytesStreamOutput os = cache.get().get().bytes;
os.reset(); os.reset();
return os; return os;
} }
public static HandlesStreamOutput cachedHandles() throws IOException { public static HandlesStreamOutput cachedHandles() throws IOException {
HandlesStreamOutput os = cacheHandles.get().get(); HandlesStreamOutput os = cache.get().get().handles;
os.reset(); os.reset();
return os; return os;
} }