mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
remove CCAllocCache
This commit is contained in:
parent
d849b51892
commit
6943771bdf
4 changed files with 1 additions and 112 deletions
2
_oasis
2
_oasis
|
|
@ -59,7 +59,7 @@ Library "containers_data"
|
||||||
CCPersistentHashtbl, CCDeque, CCFQueue, CCBV, CCMixtbl,
|
CCPersistentHashtbl, CCDeque, CCFQueue, CCBV, CCMixtbl,
|
||||||
CCMixmap, CCRingBuffer, CCIntMap, CCPersistentArray,
|
CCMixmap, CCRingBuffer, CCIntMap, CCPersistentArray,
|
||||||
CCMixset, CCHashconsedSet, CCGraph, CCHashSet, CCBitField,
|
CCMixset, CCHashconsedSet, CCGraph, CCHashSet, CCBitField,
|
||||||
CCHashTrie, CCWBTree, CCRAL, CCAllocCache,
|
CCHashTrie, CCWBTree, CCRAL,
|
||||||
CCImmutArray, CCHet, CCZipper
|
CCImmutArray, CCHet, CCZipper
|
||||||
BuildDepends: bytes
|
BuildDepends: bytes
|
||||||
# BuildDepends: bytes, bisect_ppx
|
# BuildDepends: bytes, bisect_ppx
|
||||||
|
|
|
||||||
|
|
@ -70,7 +70,6 @@ such as:
|
||||||
Various data structures.
|
Various data structures.
|
||||||
|
|
||||||
{!modules:
|
{!modules:
|
||||||
CCAllocCache
|
|
||||||
CCBitField
|
CCBitField
|
||||||
CCBV
|
CCBV
|
||||||
CCCache
|
CCCache
|
||||||
|
|
|
||||||
|
|
@ -1,75 +0,0 @@
|
||||||
|
|
||||||
(* This file is free software, part of Logtk. See file "license" for more details. *)
|
|
||||||
|
|
||||||
(** {1 Simple Cache for Allocations} *)
|
|
||||||
|
|
||||||
module Arr = struct
|
|
||||||
type 'a t = {
|
|
||||||
caches: 'a array array;
|
|
||||||
(* 2-dim array of cached arrays. The 2-dim array is flattened into
|
|
||||||
one dimension *)
|
|
||||||
max_buck_size: int;
|
|
||||||
(* number of cached arrays per length *)
|
|
||||||
sizes: int array;
|
|
||||||
(* number of cached arrays in each bucket *)
|
|
||||||
}
|
|
||||||
|
|
||||||
let create ?(buck_size=16) n =
|
|
||||||
if n<1 then invalid_arg "AllocCache.Arr.create";
|
|
||||||
{ max_buck_size=buck_size;
|
|
||||||
sizes=Array.make n 0;
|
|
||||||
caches=Array.make (n * buck_size) [||];
|
|
||||||
}
|
|
||||||
|
|
||||||
let make c i x =
|
|
||||||
if i=0 then [||]
|
|
||||||
else if i<Array.length c.sizes then (
|
|
||||||
let bs = c.sizes.(i) in
|
|
||||||
if bs = 0 then Array.make i x
|
|
||||||
else (
|
|
||||||
(* remove last array *)
|
|
||||||
let ret = c.caches.(i * c.max_buck_size + bs-1) in
|
|
||||||
c.sizes.(i) <- bs - 1;
|
|
||||||
ret
|
|
||||||
)
|
|
||||||
) else Array.make i x
|
|
||||||
|
|
||||||
let free c a =
|
|
||||||
let n = Array.length a in
|
|
||||||
if n > 0 && n < Array.length c.sizes then (
|
|
||||||
let bs = c.sizes.(n) in
|
|
||||||
if bs < c.max_buck_size then (
|
|
||||||
(* store [a] *)
|
|
||||||
c.caches.(n * c.max_buck_size + bs) <- a;
|
|
||||||
c.sizes.(n) <- bs + 1
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
let with_ c i x ~f =
|
|
||||||
let a = make c i x in
|
|
||||||
try
|
|
||||||
let ret = f a in
|
|
||||||
free c a;
|
|
||||||
ret
|
|
||||||
with e ->
|
|
||||||
free c a;
|
|
||||||
raise e
|
|
||||||
end
|
|
||||||
|
|
||||||
(*$inject
|
|
||||||
let c = Arr.create ~buck_size:2 20
|
|
||||||
|
|
||||||
*)
|
|
||||||
|
|
||||||
(*$Q
|
|
||||||
Q.small_int (fun n -> Array.length (Arr.make c n '_') = n)
|
|
||||||
*)
|
|
||||||
|
|
||||||
(*$T
|
|
||||||
let a = Arr.make c 1 '_' in Array.length a = 1
|
|
||||||
let a = Arr.make c 2 '_' in Array.length a = 2
|
|
||||||
let a = Arr.make c 3 '_' in Array.length a = 3
|
|
||||||
let a = Arr.make c 4 '_' in Array.length a = 4
|
|
||||||
*)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,35 +0,0 @@
|
||||||
|
|
||||||
(* This file is free software, part of Logtk. See file "license" for more details. *)
|
|
||||||
|
|
||||||
(** {1 Simple Cache for Allocations}
|
|
||||||
|
|
||||||
Be very careful not to use-after-free or double-free.
|
|
||||||
|
|
||||||
{b NOT THREAD SAFE}
|
|
||||||
{b status: experimental}
|
|
||||||
|
|
||||||
@since 0.15
|
|
||||||
|
|
||||||
*)
|
|
||||||
|
|
||||||
module Arr : sig
|
|
||||||
type 'a t
|
|
||||||
(** Cache for 'a arrays *)
|
|
||||||
|
|
||||||
val create: ?buck_size:int -> int -> 'a t
|
|
||||||
(** [create n] makes a new cache of arrays up to length [n]
|
|
||||||
@param buck_size number of arrays cached for each array length
|
|
||||||
@param n maximum size of arrays put in cache *)
|
|
||||||
|
|
||||||
val make : 'a t -> int -> 'a -> 'a array
|
|
||||||
(** [make cache i x] is like [Array.make i x],
|
|
||||||
but might return a cached array instead of allocating one.
|
|
||||||
{b NOTE}: if the array is already allocated then it
|
|
||||||
will NOT be filled with [x] *)
|
|
||||||
|
|
||||||
val free : 'a t -> 'a array -> unit
|
|
||||||
(** Return array to the cache. The array's elements will not be GC'd *)
|
|
||||||
|
|
||||||
val with_ : 'a t -> int -> 'a -> f:('a array -> 'b) -> 'b
|
|
||||||
(** Combines {!make} and {!free} *)
|
|
||||||
end
|
|
||||||
Loading…
Add table
Reference in a new issue