I'm trying to derive a Graphviz file describing a structured value. This is for diagnostic purposes so I want my graph to mirror the actual structure in memory as closely as possible. I'm using the below to map values to Graphviz vertices so that I can reuse a vertex when a value has two or more inbound references:
let same = (==)
module StateIdentity : Hashtbl.HashedType = struct
type t = R.meta_t state
let hash = Hashtbl.hash
let equal = same
end
module StateHashtbl = Hashtbl.Make (StateIdentity)
The documentation for Hashtbl.hash
suggests that it is suitable for use both when StateIdentity.equal = (=)
and when StateIdentity.equal = (==)
but I'd like to ensure that hash table access is as close to O(1) as possible so would rather not have Hashtbl.hash
walking a (potentially large in this case) object graph on every lookup.
I know Ocaml moves references around, but is there an O(1) proxy for reference identity available in Ocaml?
The answer to Hashtable of mutable variable in Ocaml suggests not.
I'm loathe to attach serial numbers to states, since this is diagnostic code so any errors I make doing that have the potential to mask other bugs.