OCaml -safe-string string literal sharing across separate modules
0 reputation · 01 Feb 2020, 17:26 UTC
OCaml -safe-string string literal sharing across separate modules
With -safe-string enabled by default since OCaml 4.06, strings are immutable and mutation lives in Bytes. The compiler may physically share identical literals inside a single compilation unit, but the language definition does not state whether the same literal in different modules maps to one heap object.
The objective is to establish whether == on string literals is a dependable cross-module identity check. The missing constraint is that separate compilation, inlining, and link order can change literal layout, so behavior may differ between a single executable and a dynamically loaded library.
Does the OCaml separate compilation model guarantee physical identity for identical string literals across modules? Which compilation flags or linking strategies influence whether literals are shared? Is an explicit interning table the only portable way to implement string identity?