Page MenuHomePhabricator

[OCaml] Minor optimizations by avoiding double initialization
ClosedPublic

Authored by jberdine on Mar 28 2021, 3:30 PM.

Details

Summary

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.

Diff Detail

Event Timeline

jberdine created this revision.Mar 28 2021, 3:30 PM
jberdine requested review of this revision.Mar 28 2021, 3:30 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 28 2021, 3:30 PM
vaivaswatha accepted this revision.Mar 29 2021, 10:01 AM

As far as I can see, this looks fine, but I'm far from an expert on OCaml internals. It would be greatly useful if someone else who understands OCaml internals can take a look, but I don't know if it's easy finding such a reviewer here. So if you're confident and the code has been tested well on your end, I suppose it's okay to commit this.

This revision is now accepted and ready to land.Mar 29 2021, 10:01 AM
This revision was landed with ongoing or failed builds.Apr 5 2021, 2:59 AM
This revision was automatically updated to reflect the committed changes.