caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] The verdict on "%identity"
@ 2012-11-19 17:49 Dario Teixeira
  2012-11-19 18:02 ` Török Edwin
  0 siblings, 1 reply; 9+ messages in thread
From: Dario Teixeira @ 2012-11-19 17:49 UTC (permalink / raw)
  To: OCaml mailing-list

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


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2012-11-20 20:43 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-11-19 17:49 [Caml-list] The verdict on "%identity" 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
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

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).