mmottl/postgresql-ocaml

Improve use of caml_alloc_final on results

Closed this issue · 1 comments

Two suggested improvements on:

value v_res = caml_alloc_final(3, free_result, 1, 500);

  1. The implementation of caml_alloc_final in the OCaml runtime system needs to wrap the finalizer into a full "custom operations" struct. This is cached, but the lookup in the cache is a linear scan. So a program that uses a lot of caml_alloc_final with different finalization functions can get some overhead. It is better to use directly caml_alloc_custom in user code.

  2. The ratio 1/500 seems a bit high. It implies a major cycle every 500 allocations of a result. I don't know how much of an overhead a "result" represents in the postgresql client library, but if it's not too high, it might be good to reduce the ratio. One could also expose an explicit function to release a result value explicitly (before the GC collects the OCaml wrapper).

Thanks for the hint, I have improved the situation. Since most result values are likely only a few hundred bytes and given the available memory of modern machines, I have considerably increased the number of allocations before each major cycle to 100_000. My other bindings will also soon be changed to avoid caml_alloc_final.