caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Bad bad things
@ 2002-02-13 14:22 Pascal Brisset@sepia
  0 siblings, 0 replies; only message in thread
From: Pascal Brisset@sepia @ 2002-02-13 14:22 UTC (permalink / raw)
  To: caml-list



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


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2002-02-13 14:23 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-02-13 14:22 [Caml-list] Bad bad things Pascal Brisset@sepia

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).