Hi,
I was wondering about something, when it comes to cycle detection – if I naively make a search data structure (hash table, tree) with pointers as keys to detect cycles, then presumably that will break with the garbage collector moving values around, as the addresses are not constant.
I have confirmed my suspicion with
let a = ref 1;;
let addressof x = Printf.printf "0x%08x (%d)\n" (Obj.magic x) (Obj.magic x);;
addressof a;;
addressof a;;
Gc.compact();;
addressof a;;
addressof a;;
(* 0x3fad252353dc (70012884964316)
0x3fad252353dc (70012884964316)
0x3fad24debbcc (70012880468940)
0x3fad24debbcc (70012880468940) *)
But this is something that OCaml does, if you do
type 'a t = T of 'a * 'a t * 'a t;;
let rec x = T(1,T(2,x,x),x);;
(* val x : int t = T (1, T (2, <cycle>, <cycle>), <cycle>) *)
so this started me looking at the code, but I’m somewhat lost, I suspect the cycle detection is done in/around the code
which does use a hash table, from above
and here I get a little lost, does this not have a problem due to the GC moving objects around? I don’t see any GC locks or anything like that. Or have I misunderstood something?
Apologies about asking such an unusual question, I just wanted to clarify/clear this up, in case I fundamentally misunderstood something.