Hacker News new | past | comments | ask | show | jobs | submit login

> Additionally, what I miss in this story is which source code actually produces [...] (this instruction)

The post does not indeed make it clear, but the two instructions

    andb    $252, %ah
    movq    %rax, (%rbx)
come from the default branch of the switch in the C snippet

    Hd_hp (hp) = Whitehd_hd (hd);
This line is full of macros. hp (heap pointer I think ?) points to a value on the heap.

In Ocaml, each value has an additional word in front, and contains several packed parts. There is the size of the block, a tag which represents the value kind, and a few bits are used by the GC to store the "color".

This bit of code reset the color to "white". The code here is part of the GC sweep phase, responsible for deallocating block of unreachable memory. If the block is still reachable, the status is reset so that the next GC phases starts again from clean state.

Hence this instruction only updates the bits storing the color value and should not modify other part of the header like the size of the block and the tag.




Wouldn't the different layout of these packed bits result in the code which would never have to clear some bits in the second byte? Is that layout fully internal to the Ocaml gc?


I'm not really qualified to explain why such a layout was chosen, my understanding of ocaml internal is limited. The header layout is described here :

https://github.com/ocaml/ocaml/blob/4.04.2/byterun/caml/mlva...

I believe it's internal to the compiler/runtime/C interface (but hidden under abstraction), unless Marshal (an unsafe low level binary serialization format for ocaml values depends on it)

The lowest byte is filled with the tag. I guess it should be possible to swap the color and the tag, or put the color at the beginning before the size, but that probably would have a different tradeoff.

The tag is used for instance in pattern matching and in a lot of code. With this layout it can be accessed easily, which might be the reason the color is put in the second byte




Consider applying for YC's W25 batch! Applications are open till Nov 12.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: