To give slightly more information: an OCaml value may be either a number/scalar or an address/pointer (to an OCaml block). At runtime we need a way to distinguish those two sorts to avoid conflicts (confusing the GC for example). Standard OCaml runtimes (not js_of_ocaml) use two different conflict-avoidance schemes for scalar values:
- “tagging”, which is the use of the least significant bit to distinguish pointers (0) from constants (1). This adds a small bit of complexity to arithmetic operations, but this is relatively cheap. But it requires reserving a bit; this is why OCaml
int has only 31 or 63 bits on systems with machine words of 32 or 64 bits.
char, constant algebraic-datatype constructors use tagging. (We call “immediate” the values that are tagged scalars.)
- “boxing”, which is the use of a memory indirection, a memory block is allocated to hold the scalar data (so the OCaml value is the pointer to that block). This is typically more expensive, you need to allocate when creating values and to dereference when accessing values.
nativeint (which is either
int64) use boxing, with optimizations to avoid boxing/unboxing in intermediary computations.
These two approaches have coexisted for a long time, basically since the OCaml runtime exists, and most datatypes using one or the other. Int63 is a newer and weirder, its type is tagged on 64bit architectures and boxed on 32bit architectures. This improves efficiency (compared to Int64) in the common 64bit case. (Int32 could benefit from the same treatments, but 32-bit integers are too small anyway and this would be a compatibility-breaking change.)
@@immediate64 attribute mentioned above is related.
@@immediate is an attribute that the type-system tracks to know that certain types, despite being abstract (their definition is unknown), must contain only immediate values. (This is useful for some type-directed optimizations, typically specializing polymorphic operators on runtime, etc.)
@@immediate64 is a weirder variant for types that are only guaranteed to be
@@immediate on 64-bits machines.
@@immediate has been around for a while (4.03; released in April 2016),
@immediate64 is relatively recent (4.10, released in February 202). Both were contributed to the compiler upstream by Jane Street.