Quantcast
Channel: Active questions tagged ocaml - Stack Overflow
Viewing all articles
Browse latest Browse all 527

Is there a benefit/penalty in record modification?

$
0
0

In a functional program I have an API that provides functions on complex state implemented as a record:

let remove_number nr {counter ; numbers ; foo } = {counter ; numbers = IntSet.remove nr numbers ; foo}let add_fresh {counter ; numbers ; foo } = { counter = counter + 1 ; numbers = IntSet.add counter numbers ; foo }

I know, I can use the simplified record modification syntax like this:

let remove_number nr state = { state with numbers = IntSet.remove nr numbers }

When the record type grows, the latter style is actually more readable. Hence, I will probably use it anyway. But out of curiosity, I wonder, whether it also allows the compiler to detect possible memory reusage more easily (my application is written in a monadic style, so there will usually only be one record that is passed along, hence an optimizing compiler could remove all allocations but one and do in-place-mutation instead). In my limited view, the with-syntax gives a good heuristic for places to apply such optimization, but is that true?

  • Does OCaml even optimize (unneeded) record allocations?
  • Is the record modification syntax lowered before any optimizations apply?
  • And finally, is there any pattern recognition implemented in the ocamlcompiler, that tells it that there is a "cheap" way to create onerecord expression by modifying a "dead" value in place (and how isthat optimization usually called)?

Viewing all articles
Browse latest Browse all 527

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>