[OCaml] Minor optimizations by avoiding double initialization
authorJosh Berdine <josh@berdine.net>
Sat, 27 Mar 2021 16:54:16 +0000 (16:54 +0000)
committerJosh Berdine <josh@berdine.net>
Mon, 5 Apr 2021 09:55:07 +0000 (10:55 +0100)
commit58bb9222dd298a2a38b76817df18323167f095f7
tree23c8c9d7de3f329ae4d7b0ce368910678fd99fae
parent5c25ff8739e013fec39bc8c6fc1df16e0e5041ca
[OCaml] Minor optimizations by avoiding double initialization

In several functions an OCaml block is allocated and no further OCaml
allocation functions (or other functions that might trigger allocation
or collection) are performed before the block is fully initialized. In
these cases, it is safe and slightly more efficient to allocate an
uninitialized block.

Also, the code does not become more complex after the non-initializing
allocation, since in the case that a non-small allocation is made, the
initial values stored are definitely not pointers to OCaml young
blocks, and so initializing via direct assignment is still safe. That
is, in general if `caml_alloc_small` is called, initializing it with
direct assignments is safe, but if `caml_alloc_shr` is
called (e.g. for a block larger than `Max_young_wosize`), then
`caml_initialize` should be called to inform the GC of a potential
major to minor pointer. But if the initial value is definitely not a
young OCaml block, direct assignment is safe.

Differential Revision: https://reviews.llvm.org/D99472
llvm/bindings/ocaml/llvm/llvm_ocaml.c