Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Bad bad things
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Pascal Brisset@sepia <brisset@r...>
Subject: [Caml-list] Bad bad things


I'm trying to play with Obj.magic ... and get a segmentation fault.
I know I deserve it but ... maybe someone may help:

I want to store efficiently in a data-structure an update in a array;
I need to store triples (array, index, value) to be able to execute

	array.(index) <- value

I need to store in the same data (i.e. a list) such update
informations for different types.

My first solution was to use closures:

	type t = (unit -> unit) list

and to store (fun () -> array.(index) <- value), a closure which will
be called when necessary

This solution works correctly but my program uses a lot of memory and
investigation about memory (and cpu) consumption of this closure solution
becomes essential.

My second solution is to hack the type-checker using Obj.magic to
store the triple (array, index, value). So I define

	type t = (string array * int * string) list
	let global = ref []

and my store function becomes

	let store array index value =
	  let array = Obj.magic array
	  and value = Obj.magic value in
	  global := (ref, index, value) :: !global

and to restore, I simply do something like

	List.iter (fun (a, i, v) -> a.(i) <- v) !global

This solution works for all my small tests (where GC is not required?)
but ends with a segmentation fault for larger examples.


Should I forget this nasty hack and keep my (expensive ?) closures ?

--Pascal
	

	
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr