caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Pascal Brisset@sepia" <brisset@recherche.enac.fr>
To: caml-list@inria.fr
Subject: [Caml-list] Bad bad things
Date: Wed, 13 Feb 2002 15:22:57 +0100	[thread overview]
Message-ID: <E16b0JG-0006jL-00@mauve.recherche.enac.fr> (raw)



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


                 reply	other threads:[~2002-02-13 14:23 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=E16b0JG-0006jL-00@mauve.recherche.enac.fr \
    --to=brisset@recherche.enac.fr \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).