List Info

Thread: "ocaml_beginners"::[] mutable records and efficiency




"ocaml_beginners"::[] mutable records and efficiency
user name
2006-11-29 17:52:21

I have the following type in my code:

type path = { prefix: string; mutable node: int };;

I left the node mutable so that I needn't copy the prefix
unnecessarily, and could instead define mutable versions of functions
that didn't change the prefix (this is in the context of a sparse
trie, where I often need to keep the prefix constant while I step
through the siblings of a node, and if I never need to backtrack,
changing the path in place doesn't affect anything.

For example:

(* mutates the record in place *)
let seek chr p =
p.node <- find chr p.node;
p;;

(* copies the record *)
let sib c p =
{ prefix = p.prefix; node = find c p.node };;

The recent thread on applicative data structures and the minor heap
made me wonder if this is really saving me anything, or whether I
should just make the node nonmutable and trust the language to
optimise for me. Any pointers?

martin

__._,_.___
.

__,_._,___
[1]

about | contact  Other archives ( Real Estate discussion Medical topics )