Consider for a motivating example a copy-on-write array, which implements a persistent (i.e. immutable) array data type. As an optimisation at runtime, a reference counter can be used to avoid the copy in the common case that the old array would become garbage after the modification occurs. By eliding copies (where allowed), the "immutable" copy-on-write data structure is effectively mutable "under the hood".
The above optimisation occurs at runtime. What analogous optimisations can be done at compile-time? By "analogous" I mean that code using some persistent data structure gets optimised into code which uses a mutable data structure instead. I'm particularly interested if there is a class of persistent data structures which can leverage existing compiler optimisations (e.g. in LLVM) to achieve this in common cases.