Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimization of types #3978

Closed
vicuna opened this issue Feb 18, 2006 · 4 comments
Closed

Optimization of types #3978

vicuna opened this issue Feb 18, 2006 · 4 comments
Assignees

Comments

@vicuna
Copy link

vicuna commented Feb 18, 2006

Original bug ID: 3978
Reporter: Christophe
Assigned to: @mshinwell
Status: resolved (set by @mshinwell on 2016-12-06T21:26:51Z)
Resolution: fixed
Priority: low
Severity: feature
Version: 3.09.1
Category: ~DO NOT USE (was: OCaml general)
Monitored by: n8gray nogin

Bug description

When I defined a mono type like

type color = Color of int ;;

all values of type color are integers. But it seems that, even if I compile with ocamlopt, such value are represented within a block. It seems completly useless to do such a representation as there is only one constructor in the color type, as core values are integers, and as typage is a static operation....

So is it a complex optimization, or is there some difficulty that I do not see that prevent such an optimization ?

Thank you !

@vicuna
Copy link
Author

vicuna commented Mar 24, 2006

Comment author: @xavierleroy

This representation optimization has been considered before. Unclear to me
that a special case is warranted. Similar efficiency can be achieved
by "type color = int", possibly with a signature constraint to make "color"
abstract.

@vicuna
Copy link
Author

vicuna commented Mar 24, 2006

Comment author: Christophe

type color = Color of int
let dark = function Color i -> Color (i+1)

but...

module Color : sig
type color
val of_int : int -> color
val to_int : color -> int
end = struct
type color = int
let of_int x = x
let to_int x = x end

let dark c = Color.of_int (Color.to_int c + 1)

this is a more complex code just for efficiency ! This makes difficult to use primitive types without merging them (just say type color = int).
This is specially true when I want several different ints or strings or something primitive... The use of a constructor as a flag is very useful in those cases.

@vicuna
Copy link
Author

vicuna commented Mar 25, 2006

Comment author: n8gray

I think Christophe makes the case for this pretty well, but I just wanted to add my support. I keep wanting to use this sort of construct for units:

type cm = Cm of float
type inch = Inch of float
let in_of_cm (Cm f) = Inch (0.393700787 *. f)
let cm_of_in (Inch f) = Cm (2.54 *. f)
...

I would argue that guaranteeing an efficient representation for this would lead to more reliable software, since programmers wouldn't have to worry about killing performance with needless boxing ops, even in low-level math code.

Update: Fixed my math! (blush)

@vicuna
Copy link
Author

vicuna commented Dec 6, 2016

Comment author: @mshinwell

Fixed by #606

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants