4 ms·
This is interesting, but I feel like it only scratches the surface. What about all the types that _aren’t_ an int or a pointer?
by moomin 3y ago
This is interesting, but I feel like it only scratches the surface. What about all the types that _aren’t_ an int or a pointer?
- n2d4 3y agoThose are the only ways OCaml represents types in memory (besides float, as briefly mentioned in the article) If you have a record type, or anything else, that'll compile to a pointer and an object in memory, comparable to e.g. Python or Java objects. Types like booleans are converted to integers by the compiler, similarly to C(++) (though there's no implicit conversion on the language level).
- AgentOrange1234 3y agoYou can think of the bottom bit here as a one-bit tag. Various lisps will devote additional tag bits to allow for multiple “immediate” types like characters and floats. Every additional tag bit cuts into your primitive integer space, so it’s a tradeoff.
- thedufer 3y agoIt's a bit more complex than this, but roughly each block must contain either all pointers/ints or no pointers. There's a tag at the beginning of each block indicating what type of block it is (and some other stuff - the GC stores some state in the tag as well). If you have, for example, a record consisting of a pointer, an int, and a float, it'll be tagged as having all pointers/ints, and the float will actually be a pointer to a block tagged as data, that contains the float. This is the "float boxing" problem - floats cost an extra pointer chase in most cases. The 63-bit int is an optimization that avoids needing the extra indirection for ints. You can still have 64-bit ints, but they aren't the default, and they cost this extra pointer chase.