From: Dario Teixeira <darioteixeira@yahoo.com>
To: OCaml mailing-list <caml-list@inria.fr>
Subject: [Caml-list] The verdict on "%identity"
Date: Mon, 19 Nov 2012 09:49:29 -0800 (PST) [thread overview]
Message-ID: <1353347369.78785.YahooMailNeo@web111510.mail.gq1.yahoo.com> (raw)
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
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 reply other threads:[~2012-11-19 17:49 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-11-19 17:49 Dario Teixeira [this message]
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
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=1353347369.78785.YahooMailNeo@web111510.mail.gq1.yahoo.com \
--to=darioteixeira@yahoo.com \
--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;
as well as URLs for NNTP newsgroup(s).