Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inefficient code generated for module function arguments #6874

vicuna opened this issue May 20, 2015 · 1 comment

Inefficient code generated for module function arguments #6874

vicuna opened this issue May 20, 2015 · 1 comment


Copy link

@vicuna vicuna commented May 20, 2015

Original bug ID: 6874
Reporter: @mmottl
Assigned to: @garrigue
Status: closed (set by @xavierleroy on 2016-12-07T10:49:25Z)
Resolution: fixed
Priority: normal
Severity: minor
Platform: Mac
OS: Mac OS X
OS Version: Yosemite
Version: 4.02.1
Target version: 4.03.0+dev / +beta1
Fixed in version: 4.03.0+dev / +beta1
Category: middle end (typedtree to clambda)
Monitored by: @yakobowski @mmottl

Bug description

Consider the following three implementations of essentially the same function:

module type ARG = sig val f : unit -> unit end

let foo_bad (module Arg : ARG) b =
  if b then Arg.f ()

let foo_good1 (arg : (module ARG)) b =
  let module Arg = (val arg) in
  if b then Arg.f ()

let foo_good2 b (module Arg : ARG) =
  if b then Arg.f ()

"foo_bad" generates a lot of unnecessary and expensive machine code whereas "foo_good1" doesn't. "foo_good2" just changes the argument order compared to "foo_bad" and generates equivalently efficient code as "foo_good1".

This seems to indicate that the compiler doesn't check whether subsequent function arguments (initializers for default arguments) actually refer to the unpacked module. The compiler then unnecessarily allocates closures to deal with that situation.

One could improve the compiler by introducing a check whether the module argument is being referred to by others. Maybe one can even solve this issue without any closure allocations in general.

Copy link

@vicuna vicuna commented May 21, 2015

Comment author: @garrigue

Fixed in trunk at revision 16135.
Push bindings of module patterns together with defaults.
Also added merging of Lfunctions in Simplif.simplify_lets, while this is not strictly needed for this.

@vicuna vicuna closed this as completed Dec 7, 2016
@vicuna vicuna added this to the 4.03.0 milestone Mar 14, 2019
@vicuna vicuna added the bug label Mar 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet

No branches or pull requests

2 participants