caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] is there a runtime cost for this
@ 2014-02-04  1:41 Francois Berenger
  2014-02-04 10:20 ` Goswin von Brederlow
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Francois Berenger @ 2014-02-04  1:41 UTC (permalink / raw)
  To: caml-list

Hello,

In a .ml file I'd like to write something like this
in order to factorize some code:

let f x y z = [...] some code [...]

module Something = struct
     let g = f
end

module Something_else = struct
     let h = f
end

Is calling Something.g or Something_else.h as efficient
as calling f directly?

-- 
Best regards,
Francois Berenger.

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

* Re: [Caml-list] is there a runtime cost for this
  2014-02-04  1:41 [Caml-list] is there a runtime cost for this Francois Berenger
@ 2014-02-04 10:20 ` Goswin von Brederlow
  2014-02-04 12:33 ` Gerd Stolpmann
  2014-02-04 16:53 ` rixed
  2 siblings, 0 replies; 4+ messages in thread
From: Goswin von Brederlow @ 2014-02-04 10:20 UTC (permalink / raw)
  To: caml-list

On Tue, Feb 04, 2014 at 10:41:50AM +0900, Francois Berenger wrote:
> Hello,
> 
> In a .ml file I'd like to write something like this
> in order to factorize some code:
> 
> let f x y z = [...] some code [...]
> 
> module Something = struct
>     let g = f
> end
> 
> module Something_else = struct
>     let h = f
> end
> 
> Is calling Something.g or Something_else.h as efficient
> as calling f directly?

I would expect this to result in identical code. But you know, you
could just compile both versions, objdump -d and compare the two
results.

MfG
	Goswin

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

* Re: [Caml-list] is there a runtime cost for this
  2014-02-04  1:41 [Caml-list] is there a runtime cost for this Francois Berenger
  2014-02-04 10:20 ` Goswin von Brederlow
@ 2014-02-04 12:33 ` Gerd Stolpmann
  2014-02-04 16:53 ` rixed
  2 siblings, 0 replies; 4+ messages in thread
From: Gerd Stolpmann @ 2014-02-04 12:33 UTC (permalink / raw)
  To: Francois Berenger; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 1437 bytes --]

Am Dienstag, den 04.02.2014, 10:41 +0900 schrieb Francois Berenger:
> Hello,
> 
> In a .ml file I'd like to write something like this
> in order to factorize some code:
> 
> let f x y z = [...] some code [...]
> 
> module Something = struct
>      let g = f
> end
> 
> module Something_else = struct
>      let h = f
> end
> 
> Is calling Something.g or Something_else.h as efficient
> as calling f directly?

This is the same: For the calls of g and h ocamlopt generates code that
actually calls f. Even the eta-expanded version is cheap:

module Something = struct
     let g2 x y z = f x y z
end

The code for g2 consists just of a jump to the body of f. (And normally
the inlining feature of ocamlopt even changes calls to g into calls to
f.)

Generally, ocamlopt is very good at function calls, no matter which
variant (direct or indirect call, with or without curried arguments,
inside an inner module or a functor). Don't put too much effort into
optimizing this by hand.

Gerd

> 
> -- 
> Best regards,
> Francois Berenger.
> 

-- 
------------------------------------------------------------
Gerd Stolpmann, Darmstadt, Germany    gerd@gerd-stolpmann.de
My OCaml site:          http://www.camlcity.org
Contact details:        http://www.camlcity.org/contact.html
Company homepage:       http://www.gerd-stolpmann.de
------------------------------------------------------------


[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 490 bytes --]

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

* Re: [Caml-list] is there a runtime cost for this
  2014-02-04  1:41 [Caml-list] is there a runtime cost for this Francois Berenger
  2014-02-04 10:20 ` Goswin von Brederlow
  2014-02-04 12:33 ` Gerd Stolpmann
@ 2014-02-04 16:53 ` rixed
  2 siblings, 0 replies; 4+ messages in thread
From: rixed @ 2014-02-04 16:53 UTC (permalink / raw)
  To: caml-list

As long as Something and Something_else are not functors, you
can even compile f separately from these two modules and still
your code calling Something.g should actually call f instead.

If there are some functors, that's another story, unfortunately.


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

end of thread, other threads:[~2014-02-04 16:53 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-02-04  1:41 [Caml-list] is there a runtime cost for this Francois Berenger
2014-02-04 10:20 ` Goswin von Brederlow
2014-02-04 12:33 ` Gerd Stolpmann
2014-02-04 16:53 ` rixed

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