Vtables calculated at runtime

I was investigating the poor startup performance of a large OCaml binary that I work with. Once I took care of the egregious cases of expensive code in the module initialization path, I noticed something curious.

Through stack sampling, I noticed that my binary’s startup was spending a significant amount of time in the CamlInternalOO module. Specifically, the inherits function.

I did some digging, and what I found was quite surprising to me. The vtables for every class is not generated during compile time, but rather at runtime. For most projects, this cost is probably negligible, but due to the size of my project and the number of classes, this code is contributing 10s of milliseconds to startup.

It seems like the vtables could be constructed during compile time instead, at least for classes. I did a bit of searching but I couldn’t find any existing discussion about this.

Has anyone else observed this in their projects? To those more familiar with the compiler/RTS, are there any blockers to constructing the vtables at compile time? I might be interested in contributing some changes here.

4 Likes

AFAIK there is no optimization work on the object system, nor is anyone planning on adding it. We could optimize a lot if we could determine which objects come purely from classes, as you noticed. But since the object system is rarely used, it’s not a priority.