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

Share a few more equal switch branches #709

Merged
merged 2 commits into from
Jul 28, 2016

Conversation

chambart
Copy link
Contributor

I was surprised to find that in the example:

type t =
  | Immutable of { cell : int }
  | Mutable of { mutable cell : int }

let cell = function
  | Immutable { cell } -> cell
  | Mutable { cell } -> cell

The match branches where not shared. This comes from a small difference between the generated code in both branches:

(let (cell/1209 =a (field 0 param/1215)) cell/1209)
(let (cell/1210 =o (field 0 param/1215)) cell/1210)

The first one being substituted by Lambda.make_key to

(field 0 param/1215)

while the other remain unchanged.

This small change simplify (let (x =o exp) x) into x to enable sharing in this kind of patterns.

CC @maranget

@gasche
Copy link
Member

gasche commented Jul 23, 2016

One justification for this optimization is that it is already implemented in simplif.ml.

@gasche gasche mentioned this pull request Jul 23, 2016
@chambart chambart merged commit d84cd91 into ocaml:trunk Jul 28, 2016
@damiendoligez
Copy link
Member

IIUC the two branches of your example will have to be different for the parallel runtime.
/cc @kayceesrk @stedolan

@stedolan
Copy link
Contributor

Sharing these two branches is possible in the parallel runtime, but would require some effort. With the current sharing logic, @damiendoligez is correct that the two branches would generate different code and not be shared. The optimisation of turning (let (x =o exp) x) into x seems like a good one, and should cause no problems with the parallel runtime, but it won't apply in this example without more complicated sharing logic.

The load operations are different for mutable and immutable fields (a read barrier is applied to mutable loads), but it is always safe to use the mutable version even when not required. Perhaps a better way to think about the operations is that one is the general case of loading a field, and the other is the special optimised case for fields known immutable.

So, in theory, these could be shared, by allowing the compiler to combine an immutable load and a mutable load into a mutable load. It would be a complex tradeoff to share them: lowering code size at the expense of an extra barrier in the immutable case.

camlspotter pushed a commit to camlspotter/ocaml that referenced this pull request Oct 17, 2017
Share a few more equal switch branches
EmileTrotignon pushed a commit to EmileTrotignon/ocaml that referenced this pull request Jan 12, 2024
* OCaml 5.0 release: announce and release page

* Update landing page banner text and color

* Set text to refer to OCaml 5
* Set color to red-800

* OCaml 5.0.0 announce: review comments

* OCaml 5 release file: fix links

* OCaml 5 announce: don't mention beta repository

* OCaml 5.0 announce: more fixes

Co-authored-by: Cuihtlauac ALVARADO <cuihtmlauac@tarides.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants