Why is an int in OCaml only 31 bits?

后端 未结 5 908
感情败类
感情败类 2020-12-12 12:53

Haven\'t seen this \"feature\" anywhere else. I know that the 32nd bit is used for garbage collection. But why is it that way only for ints and not for the other basic types

5条回答
  •  借酒劲吻你
    2020-12-12 13:08

    See the the "representation of integers, tag bits, heap-allocated values" section of https://ocaml.org/learn/tutorials/performance_and_profiling.html for a good description.

    The short answer is that it is for performance. When passing an argument to a function it is either passed as an integer or a pointer. At a machine level language level there is no way to tell if a register contains an integer or a pointer, it is just a 32 or 64 bit value. So the OCaml run time checks the tag bit to determine if what it received was an integer or a pointer. If the tag bit is set, then the value is an integer and it is passed to the correct overload. Otherwise it is a pointer and type is looked up.

    Why do only integers have this tag? Because everything else is passed as a pointer. What is passed is either an integer or a pointer to some other data type. With only one tag bit, there can be only two cases.

提交回复
热议问题