I’d like to understand Ephemerons better. They are designed to implement caches whose values can be collected by the GC when no longer used and have been added to the OCaml standard library.
So far, I would use the Weak module for this as follows: When a program deals with a lot of identical strings it makes sense to store only one unique copy as it safes space and it speeds up comparisons. Below is module
Atom that stores strings in a weak hash table and ensures that each string only exists once. A string that is not referenced from outside the hash table can still be GC’ed.
module Atom : sig val string: string -> string end = struct module S = struct include String let hash = Hashtbl.hash end module H = Weak.Make(S) let atoms = H.create 29 let string str = try H.find atoms str with Not_found -> H.add atoms str; str end
Would there be an advantage to use Ephemerons or when and how would I use them instead?