From: Gabriel Scherer <gabriel.scherer@gmail.com>
To: David House <dhouse@janestreet.com>
Cc: "Dario Teixeira" <darioteixeira@yahoo.com>,
"Török Edwin" <edwin+ml-ocaml@etorok.net>,
"caml-list@inria.fr" <caml-list@inria.fr>
Subject: Re: [Caml-list] The verdict on "%identity"
Date: Tue, 20 Nov 2012 10:53:42 +0100 [thread overview]
Message-ID: <CAPFanBHcEKxJB+feaaiSQfEPUiQowzuq13gs2P2Dkn-NqqG=Xg@mail.gmail.com> (raw)
In-Reply-To: <CAK=fH+hLkJXqe_A-ux5T4Yu4c2DR3QdY9NK7xLxDJmz7K+6Y+w@mail.gmail.com>
On Mon, Nov 19, 2012 at 7:28 PM, David House <dhouse@janestreet.com> wrote:
> If you wanted to investigate this yourself, you could compile with -S
> and look at the generated assembly. For such short functions, this is
> generally not very hard.
Indeed:
cat test.ml
module type SIG1 = sig type t val of_string: string -> t end
module type SIG2 = sig type t external of_string: string -> t =
"%identity" end
module Impl1 = struct type t = string let of_string x = x end
module Impl2 = struct type t = string external of_string: string ->
t = "%identity" end
module A: SIG1 = Impl1
module B: SIG1 = Impl2
(* module C: SIG2 = Impl1 *)
module D: SIG2 = Impl2
let testA = A.of_string "foo"
let testB = B.of_string "bar"
let testD = D.of_string "baz"
(I commented C out because it makes no sense to me, semantically, and
it's rejected by the compiler.)
ocamlopt -c -S test.ml
less test.s
The (relevant part of the) result on my machine, that correspond to
compilation of testA, testB, testD:
camlTest__entry:
[...]
movl $camlTest__3, %eax
movl %eax, camlTest + 20
movl $camlTest__2, %eax
movl %eax, camlTest + 24
movl $camlTest__1, %eax
movl %eax, camlTest + 28
[...]
All compiled in the same way.
There may be a difference for calls across compilation units: in
absence of the .cmx, no inlining would be performed. My guess would be
that in presence of the .cmx we should get the same final result, but
I must say I don't really care for performances on this front.
Note that there is however an important difference with private
definitions: with private, the cast from t to string is not only
erased by the compiler, it is a *coercion* that can be lifted to casts
to larger datatypes. You can coerce a (list t) into a (list string)
and this is also a no-op in the dynamic semantics. That's much
stronger than what you get from %identity.
(This suggest that, with the explicit subtyping we have in OCaml,
there would be a case for inter-coercible types: a way to define a t
such that for example (t :> string) and (string :> t), but not (t =
string). This doesn't increase the type safety of arbitrary programs
but allow programmers to force abstraction-breaking to be explicit,
with no performance cost in both directions.)
>
> On Mon, Nov 19, 2012 at 6:18 PM, Dario Teixeira <darioteixeira@yahoo.com> wrote:
>> Hi,
>>
>>> Wouldn't 'type t = private string' help the compiler optimize this?
>>
>>
>> Possibly, though the semantics would change: what before was
>> an abstract type is now translucent (ie, not quite transparent).
>>
>> Regards,
>> Dario
>>
>> --
>> Caml-list mailing list. Subscription management and archives:
>> https://sympa.inria.fr/sympa/arc/caml-list
>> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
> --
> Caml-list mailing list. Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
next prev parent reply other threads:[~2012-11-20 9:54 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-11-19 17:49 Dario Teixeira
2012-11-19 18:02 ` Török Edwin
2012-11-19 18:18 ` Dario Teixeira
2012-11-19 18:28 ` David House
2012-11-20 9:53 ` Gabriel Scherer [this message]
2012-11-20 10:25 ` Pierre Chambart
2012-11-20 16:19 ` Gabriel Scherer
2012-11-20 19:03 ` Vincent HUGOT
2012-11-20 20:43 ` Dario Teixeira
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='CAPFanBHcEKxJB+feaaiSQfEPUiQowzuq13gs2P2Dkn-NqqG=Xg@mail.gmail.com' \
--to=gabriel.scherer@gmail.com \
--cc=caml-list@inria.fr \
--cc=darioteixeira@yahoo.com \
--cc=dhouse@janestreet.com \
--cc=edwin+ml-ocaml@etorok.net \
/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