Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
no_scan_tag and int array
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2010-03-06 (09:26)
From: ygrek <ygrekheretix@g...>
Subject: no_scan_tag and int array

Consider this code:

open Printf

let measure f =
  let t = Unix.gettimeofday () in
  let () = f () in
  printf "%.4f sec" (Unix.gettimeofday () -. t)

let () =
  let gc () = for i = 1 to 10 do Gc.full_major () done in
  let a = Array.make 4_000_000 0 in
  measure gc;
  printf " normal %u (%u)\n%!" (Array.length a) (Gc.stat ()).Gc.live_words;

  Obj.set_tag (Obj.repr a) (Obj.no_scan_tag);
  measure gc;
  printf " no_scan_tag %u (%u)\n%!" (Array.length a) (Gc.stat ()).Gc.live_words;

  measure gc;
  printf " no array (%u)\n%!" (Gc.stat ()).Gc.live_words;

Output looks like :

0.2281 sec normal 4000000 (4000165)
0.0002 sec no_scan_tag 4000000 (4000165)
0.0002 sec no array (164)

So, as expected, setting No_scan_tag on the array of integers prevents GC from uselessly 
scanning the huge chunk of memory. Looks like polymorphic array functions still work fine and
GC correctly reclaims array memory when it is not referenced anymore.
Apparantly this trick is not allowed for float array as they have a special tag set.
The question is - how safe is this? And even more, could the compiler itself set this tag?