Quantcast
Channel: ReScript Forum - Latest posts
Viewing all articles
Browse latest Browse all 2592

How does @inline actually work?

$
0
0
npm create rescript-app@latest
> select Command Line
> select rescript12-beta3

Add src/Foo.res:

@inline let a=(x, y)=>x+y
@inline let b=(x, y, z)=>a(a(x, y), z)

Add src/Foo.resi:

@inline let a:(int, int)=>int
@inline let b:(int, int, int)=>int

Modify src/Demo.res:

Console.log("Hello, world!")

let foo = Foo.b(1, 2, 3)

Inspect .res.mjs files:

File Foo.res.mjs:

// Generated by ReScript, PLEASE EDIT WITH CARE


function a(x, y) {
  return x + y | 0;
}

function b(x, y, z) {
  return (x + y | 0) + z | 0;
}

export {
  a,
  b,
}
/* No side effect */

Notice how a has be inlined inside b correctly

File Demo.res.mjs:

// Generated by ReScript, PLEASE EDIT WITH CARE

import * as Foo from "./Foo.res.mjs";

console.log("Hello, world!");

let foo = Foo.b(1, 2, 3);

export {
  foo,
}
/*  Not a pure module */

Notice how Foo.b has not be inlined

  1. Adding -bs-cross-module-opt does nothing
  2. Removing @inline from Foo.resi optimizes Demo.res.mjs to say let foo = 6 (so the @inline directive actually prevents optimization in this case, in my codebase it does nothing because i’m not using constants)
  3. Removing @inline from Foo.res apparently does nothing in this example, but in my codebase, it prevents a from being inlined into b

Viewing all articles
Browse latest Browse all 2592

Trending Articles