Skip to content

Commit 8aeb408

Browse files
committed
pooling for extra buffers
1 parent 1a4750e commit 8aeb408

2 files changed

Lines changed: 53 additions & 3 deletions

File tree

src/main/com/mongodb/io/DBOutputBuffer.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import org.bson.*;
66
import org.bson.io.*;
7+
import org.bson.util.*;
78

89
import java.io.*;
910
import java.util.*;
@@ -20,7 +21,8 @@ public void reset(){
2021
_cur.reset();
2122
_end.reset();
2223

23-
// TODO: pool
24+
for ( int i=0; i<_fromPool.size(); i++ )
25+
_extra.done( _fromPool.get(i) );
2426
_fromPool.clear();
2527
}
2628

@@ -90,7 +92,7 @@ void _afterWrite(){
9092
if ( _end.y < BUF_SIZE )
9193
return;
9294

93-
_fromPool.add( new byte[BUF_SIZE] ); // TODO: pool
95+
_fromPool.add( _extra.get() );
9496
_end.nextBuffer();
9597
_cur.reset( _end );
9698
}
@@ -180,5 +182,10 @@ public String toString(){
180182

181183
private final Position _cur = new Position();
182184
private final Position _end = new Position();
183-
185+
186+
private static SimplePool<byte[]> _extra = new SimplePool<byte[]>( ( 1024 * 1024 * 10 ) / BUF_SIZE ){
187+
protected byte[] createNew(){
188+
return new byte[BUF_SIZE];
189+
}
190+
};
184191
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// SimplePool.java
2+
3+
package org.bson.util;
4+
5+
import java.util.*;
6+
7+
public abstract class SimplePool<T> {
8+
9+
public SimplePool( int max ){
10+
_max = max;
11+
}
12+
13+
public SimplePool(){
14+
_max = 1000;
15+
}
16+
17+
protected abstract T createNew();
18+
19+
protected boolean ok( T t ){
20+
return true;
21+
}
22+
23+
public T get(){
24+
synchronized ( _stored ){
25+
if ( _stored.size() > 0 )
26+
return _stored.removeFirst();
27+
}
28+
return createNew();
29+
}
30+
31+
public void done( T t ){
32+
if ( ! ok( t ) )
33+
return;
34+
synchronized ( _stored ){
35+
if ( _stored.size() > _max )
36+
return;
37+
_stored.addFirst( t );
38+
}
39+
}
40+
41+
final int _max;
42+
private LinkedList<T> _stored = new LinkedList<T>();
43+
}

0 commit comments

Comments
 (0)