Selective bounds-check re-enablement with -unsafe-code in OCaml
0 reputation · 13 Jun 2021, 05:23 UTC
-unsafe-code Flag Granularity in OCaml Production Builds
OCaml's -unsafe-code compiler option removes all bounds checks from generated bytecode and native code, trading memory safety for performance. This creates a configuration gap: local development environments using default safety checks may mask out-of-bounds errors that only manifest in production when -unsafe-code is enabled.
The OCaml documentation does not provide a mechanism to selectively re-enable specific bounds checks while maintaining -unsafe-code for other optimizations. This leaves developers without a way to protect critical data structures or modules even when the rest of the codebase could benefit from unsafe optimizations.
The core question is whether the OCaml compiler or runtime offers any configuration option—such as per-module flags, attributes, or environment variables—that allows granular control over bounds checking when -unsafe-code is active. Without such a feature, production deployments using -unsafe-code must either accept full unsafety or forego the performance benefits entirely.
Has any OCaml compiler version introduced selective bounds-check re-enablement, or is there an official roadmap for such functionality?