From: "Török Edwin" <edwin+ml-ocaml@etorok.net>
To: caml-list@inria.fr
Subject: Re: [Caml-list] The verdict on "%identity"
Date: Mon, 19 Nov 2012 20:02:15 +0200 [thread overview]
Message-ID: <50AA7427.5080104@etorok.net> (raw)
In-Reply-To: <1353347369.78785.YahooMailNeo@web111510.mail.gq1.yahoo.com>
On 11/19/2012 07:49 PM, Dario Teixeira wrote:
> Hi,
>
>
> I've found conflicting information regarding the use of "%identity",
> which I hope to see clarified.
>
> Let's consider a typical example where a module defines an abstract
> type t and provides (de)serialisation functions of_string/to_string.
> Moreover, the actual implementation of t uses a string, and the
> (de)serialisation functions are just identities:
>
> module Foo:
> sig
> type t
>
> val of_string: string -> t
> val to_string: t -> string
> end =
> struct
> type t = string
>
> let of_string x = x
> let to_string x = x
> end
>
>
> In practice, it's not unusual for such code to be implemented using
> the compiler's "%identity" builtin, all in the name of performance:
>
> module Foo:
> sig
> type t
Wouldn't 'type t = private string' help the compiler optimize this?
>
> external of_string: string -> t = "%identity"
> external to_string: t -> string = "%identity"
> end =
> struct
> type t = string
>
> external of_string: string -> t = "%identity"
> external to_string: t -> string = "%identity"
> end
>
>
> I realise that the use of "%identity" is dangerous. This is, after all,
> how Obj.magic is defined. Moreover, it uglifies interface definitions
> and makes a ridicule of the abstraction. However, on the assumption that
> ocamlopt won't otherwise optimise away the no-op across module boundaries,
> the use of "%identity" may well be justified for performance reasons.
>
> With all the above in mind, I have two questions:
>
> 1) Is the assumption correct that today's ocamlopt won't optimise no-ops
> across module boundaries? (I know that ocamlopt does not generally engage
> in MLton-style whole programme optimisation, but is this also true for
> low-hanging fruit such as the first example above?)
>
> 2) Consider the code below. For which modules can one expect of_string calls
> to be optimised across module boundaries?
>
> 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
>
> Thank you in advance for your time!
> Best regards,
> Dario Teixeira
>
>
next prev parent reply other threads:[~2012-11-19 18:03 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 [this message]
2012-11-19 18:18 ` Dario Teixeira
2012-11-19 18:28 ` David House
2012-11-20 9:53 ` Gabriel Scherer
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=50AA7427.5080104@etorok.net \
--to=edwin+ml-ocaml@etorok.net \
--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