File tree 2 files changed +25
-8
lines changed
2 files changed +25
-8
lines changed Original file line number Diff line number Diff line change @@ -11,22 +11,33 @@ type 'a t = {
11
11
items : 'a list_ A .t ;
12
12
}
13
13
14
- let create ?(clear = ignore) ~mk_item ?(max_size = 512 ) () : _ t =
15
- { mk_item; clear; max_size; items = A. make Nil }
14
+ let [@ inline] size_ = function
15
+ | Cons (sz , _ , _ ) -> sz
16
+ | Nil -> 0
17
+
18
+ let create ?(clear = ignore) ~mk_item ?(init_size = 0 ) ?(max_size = 512 ) () :
19
+ _ t =
20
+ let @ _sp = Trace. with_span ~__FILE__ ~__LINE__ " apool.create" in
21
+ if max_size < init_size then invalid_arg " apool: max_size < init_size" ;
22
+ let items = ref Nil in
23
+ for _i = 0 to init_size - 1 do
24
+ let @ _sp = Trace. with_span ~__FILE__ ~__LINE__ " apool.create.mk-item" in
25
+ let item = mk_item () in
26
+ items := Cons (size_ ! items + 1 , item, ! items)
27
+ done ;
28
+ { mk_item; clear; max_size; items = A. make ! items }
16
29
17
30
let rec acquire_ self =
18
31
match A. get self.items with
19
- | Nil -> self.mk_item ()
32
+ | Nil ->
33
+ let @ _sp = Trace. with_span ~__FILE__ ~__LINE__ " apool.acquire.mk-item" in
34
+ self.mk_item ()
20
35
| Cons (_ , x , tl ) as l ->
21
36
if A. compare_and_set self.items l tl then
22
37
x
23
38
else
24
39
acquire_ self
25
40
26
- let [@ inline] size_ = function
27
- | Cons (sz , _ , _ ) -> sz
28
- | Nil -> 0
29
-
30
41
let release_ self x : unit =
31
42
self.clear x;
32
43
while
Original file line number Diff line number Diff line change @@ -9,11 +9,17 @@ type 'a t
9
9
(* * Pool of values of type ['a] *)
10
10
11
11
val create :
12
- ?clear : ('a -> unit ) -> mk_item :(unit -> 'a ) -> ?max_size : int -> unit -> 'a t
12
+ ?clear : ('a -> unit ) ->
13
+ mk_item :(unit -> 'a ) ->
14
+ ?init_size : int ->
15
+ ?max_size : int ->
16
+ unit ->
17
+ 'a t
13
18
(* * Create a new pool.
14
19
@param mk_item produce a new item in case the pool is empty
15
20
@param max_size maximum number of item in the pool before we start
16
21
dropping resources on the floor. This controls resource consumption.
22
+ @param init_size initial number of items to create (default 0). This must be <= max_size.
17
23
@param clear a function called on items before recycling them.
18
24
*)
19
25
You can’t perform that action at this time.
0 commit comments