Skip to content

Optimizations not applied when record has an optional field #5616

Open
@cknitt

Description

@cknitt

The following code (without an optional field)

type t = {
  a: string,
  b: int
}

let x = { a: "test", b: 1 }
let y = { ...x, b: 5 }

let f = x => { ...x, b: x.b + 1 }

is compiled to

var y = {
  a: "test",
  b: 5
};

function f(x) {
  return {
          a: x.a,
          b: x.b + 1 | 0
        };
}

var x = {
  a: "test",
  b: 1
};

However, as soon as I make a an optional field, the output changes to the more inefficient

var x = {
  a: "test",
  b: 1
};

var newrecord = Caml_obj.obj_dup(x);

newrecord.b = 5;

function f(x) {
  var newrecord = Caml_obj.obj_dup(x);
  newrecord.b = x.b + 1 | 0;
  return newrecord;
}

var y = newrecord;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions