Skip to content

Prevent more unnecessary float boxing, especially in if and match#107

Closed
vbrankov wants to merge 5 commits intoocaml:trunkfrom
vbrankov:unnecessary-boxing
Closed

Prevent more unnecessary float boxing, especially in if and match#107
vbrankov wants to merge 5 commits intoocaml:trunkfrom
vbrankov:unnecessary-boxing

Conversation

@vbrankov
Copy link

This patch prevents unnecessary boxing in more language constructs. Especially important are if and match blocks, for example:

let reduce dir price ~by =
  match dir with
  | Buy -> price -. by
  | Sell -> price +. by

All OCaml tests passed. The test code that shows that the extra allocations are gone is here:

https://github.com/vbrankov/ocaml-demos/blob/master/unnecessary-boxing/test.ml

@chambart
Copy link
Contributor

This seams correct to me

@gasche
Copy link
Member

gasche commented Oct 24, 2014

I'm a bit worried by the compilation of pattern-matching issued from GADTs, that may return distinct types in each branch. The code never decides to unbox when two branches return an integer and a float, but it seems to me that it may unbox when one or several branches return a float, and the other are staticraises. I'm not sure which input source patterns lead to static raises in these positions, but I don't see a clear reason why those couldn't be another GADT clause returning a non-float type.

@chambart
Copy link
Contributor

It is not a problem to unbox if a branch of a match returns something unboxable and another one never returns.
There don't seems to be any problem with for instance:

catch
  let a =
    if x
    then 3.14
    else exit lbl
  in a +. 1.2
with lbl -> 123

The whole expression is not unboxable, because 2 branch returns different kind of values, but the if could be unboxed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any non returning primitive could return Undetermined. Praise for instance. (I am not certain there are no other one)

@vbrankov
Copy link
Author

I've added handling of int32, int64 and nativeint constants.

@vbrankov vbrankov mentioned this pull request May 5, 2015
@alainfrisch
Copy link
Contributor

I'm currently integrating the patch and will merge it shortly.

I've renamed Undetermined to No_result, to make it clear that it really means that the sub-expression can never return a value (and so it is fine to apply any kind of unboing to it). And I've also changed the code in the Ulet case to avoid emitting code for the let-body when one knows that the let-bound expression cannot return.

@alainfrisch
Copy link
Contributor

Applied to trunk, commit 16271.

@alainfrisch
Copy link
Contributor

This ticket can be closed.

@gasche gasche closed this Jul 27, 2015
stedolan pushed a commit to stedolan/ocaml that referenced this pull request Mar 14, 2017
lthls pushed a commit to lthls/ocaml that referenced this pull request Apr 15, 2020
lthls pushed a commit to lthls/ocaml that referenced this pull request Sep 23, 2020
lthls pushed a commit to lthls/ocaml that referenced this pull request Sep 23, 2020
lthls pushed a commit to lthls/ocaml that referenced this pull request Sep 24, 2020
chambart pushed a commit to chambart/ocaml-1 that referenced this pull request Aug 4, 2021
stedolan pushed a commit to stedolan/ocaml that referenced this pull request Oct 5, 2021
stedolan pushed a commit to stedolan/ocaml that referenced this pull request Mar 21, 2023
c703f5f Incorporate upstream comments into type-variable refactor (ocaml#121)
362ba23 Constrain curry modes to increase along applications (ocaml#108)
b1f0cf9 Simplify the extension handling (ocaml#114)
4fd53a1 Remove pat_mode from typedtree (ocaml#105)
cf6fcbc Handle attributes on lambdas with locally abstract types (ocaml#120)
5fa80fe Don't track attributes inside attributes for warning 53 (ocaml#115)
8a69777 Handle unclosed `[: ... :]` patterns (via `Generic_array` machinery) (ocaml#117)
b0737f4 Add promote-one Makefile target (ocaml#118)
c6ad684 Refactoring and fixes around module lookup (ocaml#107)
b0a6495 Add documentation for global constructor arguments (ocaml#69)
dd79aec Print `nlocal` in the `-d(raw)lambda` output (ocaml#112)
8035026 Fix `nlocal` in the generated Lambda for list comprehensions (ocaml#113)
afbcdf0 Immutable arrays (ocaml#47)
bfe1490 fix several issues when removing exp_mode (ocaml#110)
8f46060 Better error message for under-applied functions (ocaml#74)
27331d8 Consistently use Lmutvar or Lvar in comprehensions (ocaml#111)
01e965b Skip failing test for now
0131357 Fix test case to use comprehensions_experimental
22a7368 Temporarily disable list comprehensions tests due to locals bug
e08377d Make `comprehensions` into `comprehensions_experimental` for now (ocaml#109)
947cf89 List and array comprehensions (ocaml#46)
bd9e051 remove exp_mode from typedtree (ocaml#100)
a9268d2 Fix misplaced attribute warning when using external parser (and some cleanup) (ocaml#101)
2b33f24 Refactor toplevel local escape check (ocaml#104)
ed2aec6 Comment functions exported from TyVarEnv.
87838ba Move new variable creation into TyVarEnv.
a3f60ab Encapsulate functions that work with tyvars
43d83a6 Prevent possibility of forgetting to re-widen
2f3dd34 Encapsulate context when narrowing type env't
d78ff6d Make immediate64 things mode cross (ocaml#97)
aa25ab9 Fix version number (ocaml#94)
d01ffa0 Fix .depend file (ocaml#93)
942f2ab Bootstrap (ocaml#92)
05f7e38 Check Menhir version (ocaml#91)
1569b58 Move the CI jobs from 4.12 to 4.14. (ocaml#90)

git-subtree-dir: ocaml
git-subtree-split: c703f5f
EmileTrotignon pushed a commit to EmileTrotignon/ocaml that referenced this pull request Jan 12, 2024
EmileTrotignon pushed a commit to EmileTrotignon/ocaml that referenced this pull request Jan 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants