Runtime representation of numbers in OCaml

Опубликовано: 17 Май 2026
на канале: Artem Pianykh
613
29

Why ints are 63 bits rather than 64? Why floats take 2 machine words rather than 1. Why a full 64-bit Int takes 192 bits of space? How come '+ 1' is translated as '+ 2' in the assembly? In this video I do a deeper dive into how different values are represented in OCaml looking both at the runtime's source code and the generated assembly to support the findings. I also run a number of benchmarks to get a better feel for the overhead that comes from using a universal representation of values in OCaml.

This video is complementary to the "Memory Representation of Values" chapter of "Real World OCaml": https://dev.realworldocaml.org/runtim...
Source code and scripts used in the video: https://pianykh.com/blog/posts/2023-0...
Post about Unboxed Types for OCaml: https://www.janestreet.com/tech-talks...

Contents:
00:00 Intro
00:49 Opening Thoughts
01:07 Reference to Real World OCaml
01:24 Plan for the video
02:02 Dissecting ints
03:34 mlvalues.h and the runtime
04:16 ints vs blocks
06:36 int64 runtime representation
07:31 Custom blocks
08:19 int addition in assembly
10:37 int addition perf
11:05 A word of caution around benchmarks
12:04 int mul perf
14:20 Dissecting floats
15:26 float perf
17:17 float perf, no inlining
19:32 Int64.t perf or do you really need this extra bit?
20:24 Closing Thoughts