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

Jsx ppx optimize object allocation #6376

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
- Untagged variants: Support `promise`, RegExes, Dates, File and Blob. https://github.com/rescript-lang/rescript-compiler/pull/6383
- Support aliased types as payloads to untagged variants. https://github.com/rescript-lang/rescript-compiler/pull/6394

#### :nail_care: Polish

- A little performance improvement for JSX V4 runtime helper by removing one object allocation for components with key prop. https://github.com/rescript-lang/rescript-compiler/pull/6376

# 11.0.0-rc.3

#### :bug: Bug Fix
Expand Down
4 changes: 2 additions & 2 deletions jscomp/others/jsxPPXReactSupportC.res
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ module Jsx = JsxC

%%private(
@val
external propsWithKey: (@as(json`{}`) _, 'props, {"key": string}) => 'props = "Object.assign"
external propsWithKey: ({"key": string}, 'props) => 'props = "Object.assign"
Copy link
Member

Choose a reason for hiding this comment

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

This was intentional to avoid mutating the object.

Copy link
Member

Choose a reason for hiding this comment

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

Am I remembering correctly? @cknitt

Copy link
Member

Choose a reason for hiding this comment

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

Oh, I missed that switching arguments position between key and props. It makes sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It doesn't mutate the props object. It'll create the new object with key field and then mutate it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Before:

  1. Create empty obj
  2. Create obj with key
  3. Mixing props and obj with key to the empty object

After:

  1. Create obj with key
  2. Mixing props to the obj with key

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't understand what you want to say 😅

Copy link
Member

Choose a reason for hiding this comment

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

Sorry for the lack of explanation.
I meant I think that the existing implementation is more appropriate.

What do you think? @cknitt

Copy link
Member

Choose a reason for hiding this comment

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

This should work like the previous implementation (no mutation), but with one less object allocation. Looks good to me.

Copy link
Member

Choose a reason for hiding this comment

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

(Provided that the caller passes a different {"key": key} object every time, but that seems to be the case and this is an internal function anyway.)

Copy link
Member

Choose a reason for hiding this comment

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

Okay. It seems unlikely that Object.assign({"key": key}, props) would change the value of key at runtime, since props would not be able to contain key when the JSX ppx transforms the component application.

LGTM.


@inline
let addKeyProp = (~key: option<string>=?, p: 'props): 'props =>
switch key {
| Some(key) => propsWithKey(p, {"key": key})
| Some(key) => propsWithKey({"key": key}, p)
| None => p
}
)
Expand Down
4 changes: 2 additions & 2 deletions jscomp/others/jsxPPXReactSupportU.res
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ module Jsx = JsxU

%%private(
@val
external propsWithKey: (@as(json`{}`) _, 'props, {"key": string}) => 'props = "Object.assign"
external propsWithKey: ({"key": string}, 'props) => 'props = "Object.assign"

@inline
let addKeyProp = (~key: option<string>=?, p: 'props): 'props =>
switch key {
| Some(key) => propsWithKey(p, {"key": key})
| Some(key) => propsWithKey({"key": key}, p)
| None => p
}
)
Expand Down
8 changes: 4 additions & 4 deletions lib/es6/jsxPPXReactSupportC.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import * as React from "react";
import * as Caml_splice_call from "./caml_splice_call.js";

function createElementWithKey(key, component, props) {
return React.createElement(component, key !== undefined ? Object.assign({}, props, {
return React.createElement(component, key !== undefined ? Object.assign({
key: key
}) : props);
}, props) : props);
}

function createElementVariadicWithKey(key, component, props, elements) {
return Caml_splice_call.spliceApply(React.createElement, [
component,
key !== undefined ? Object.assign({}, props, {
key !== undefined ? Object.assign({
key: key
}) : props,
}, props) : props,
elements
]);
}
Expand Down
8 changes: 4 additions & 4 deletions lib/es6/jsxPPXReactSupportU.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import * as React from "react";
import * as Caml_splice_call from "./caml_splice_call.js";

function createElementWithKey(key, component, props) {
return React.createElement(component, key !== undefined ? Object.assign({}, props, {
return React.createElement(component, key !== undefined ? Object.assign({
key: key
}) : props);
}, props) : props);
}

function createElementVariadicWithKey(key, component, props, elements) {
return Caml_splice_call.spliceApply(React.createElement, [
component,
key !== undefined ? Object.assign({}, props, {
key !== undefined ? Object.assign({
key: key
}) : props,
}, props) : props,
elements
]);
}
Expand Down
8 changes: 4 additions & 4 deletions lib/js/jsxPPXReactSupportC.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ var React = require("react");
var Caml_splice_call = require("./caml_splice_call.js");

function createElementWithKey(key, component, props) {
return React.createElement(component, key !== undefined ? Object.assign({}, props, {
return React.createElement(component, key !== undefined ? Object.assign({
key: key
}) : props);
}, props) : props);
}

function createElementVariadicWithKey(key, component, props, elements) {
return Caml_splice_call.spliceApply(React.createElement, [
component,
key !== undefined ? Object.assign({}, props, {
key !== undefined ? Object.assign({
key: key
}) : props,
}, props) : props,
elements
]);
}
Expand Down
8 changes: 4 additions & 4 deletions lib/js/jsxPPXReactSupportU.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ var React = require("react");
var Caml_splice_call = require("./caml_splice_call.js");

function createElementWithKey(key, component, props) {
return React.createElement(component, key !== undefined ? Object.assign({}, props, {
return React.createElement(component, key !== undefined ? Object.assign({
key: key
}) : props);
}, props) : props);
}

function createElementVariadicWithKey(key, component, props, elements) {
return Caml_splice_call.spliceApply(React.createElement, [
component,
key !== undefined ? Object.assign({}, props, {
key !== undefined ? Object.assign({
key: key
}) : props,
}, props) : props,
elements
]);
}
Expand Down