File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 44
55import org .bson .*;
66import org .bson .io .*;
7+ import org .bson .util .*;
78
89import java .io .*;
910import 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}
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments