William Le Ferrand <[email protected]> writes:
> Dear list,
>
> We are building a cache in ocaml and we're wondering if it would make sense to
> store ocaml values outside the reach of the gc. (gc on a 20GB cache hangs the
> process for a second or so).
>
> To run some experiments, we wrote a small library (https://github.com/besport/
> ocaml-everlasting) that exposes two functions, get and set.
>
> When inserting a value, we copy recursively the blocs outside of the reach of
> the gc (and put the resulting value in some C array). When getting the value,
> we simply pass the pointer to the copied value to the ocaml code (the
> structure
> is still coherent and the value is directly usable). We also wrote an "update"
> function that compare a new value with the existing value in cache, to avoid
> unnecessary memory allocation/deallocation.
>
> It does not seems very stable though, but I don't know if it is a bug in the
> update function or simply because this approach is not reasonable. Do you have
> any thoughts? Is there any clever way to build a large cache in an ocaml app
> ?
>
> Thanks in advance for any tips!
>
> Best
>
> William
For a generic case you will have to inspect the values, tell the GC
about all the other ocaml values it points to and track any heap values
that have a reference to your external value so you don't delete it
before it is dead. In short you need to write a GC.
For special cases you can store completly self contained values, e.g. a
array of floats or a record of non pointer types. The simplest of types.
For a cache you often have millions of objects of the same type. And
often those contain pointers but do not share them. Then it becomes
feasable to copy everything they point to into the cache as well. Or
write a little C glue to store the data in an abstract or custom block
that does not contain pointers to outside the block. Often you can also
store the data much more compact than ocaml allows and thereby reduce
the memory footprint and increase cache efficiency.
MfG
Goswin
--
Caml-list mailing list. Subscription management and archives:
https://sympa-roc.inria.fr/wws/info/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs