Browse thread
Efficency of varient types
-
Michael D. Adams
- Stefan Monnier
- Nicolas Cannasse
- Jon Harrop
- Lukasz Stafiniak
- David Baelde
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Nicolas Cannasse <ncannasse@m...> |
| Subject: | Re: [Caml-list] Efficency of varient types |
Michael D. Adams wrote: > I have recently learned about OCaml and have been impressed by how > fast it is in the benchmarks. However I have discovered that variant > types can slow down a program quite a bit. For example, consider the > Ackermann function implemented for int (see program 1) and the same > Ackermann function implemented for a "type value = Int of int | String > of int" (program 2). The second one is ten times slower! (Using > ocamlopt.) In order to understand what there is such difference, it's useful to learn the ocaml memory model at runtime : - int are 31 bits unboxed value with last bit set to 1 in order to differenciate them with GC allocated pointers. - tagged variants are GC allocated blocks with a discriminating "tag" in the header. - chars and booleans are integers at runtime The second bit is used to mark an exception but it's only internal and temporary when dealing with callbacks. If you have a tagged variant where all constructors have a parameter, you can use Obj module to unbox the Int variant but the code is a lot less readable. Nicolas