Skip to content

Specialize uses of new dict literal to direct JS object creation#6538

Merged
zth merged 20 commits into
masterfrom
specialize-js-dict-fromArray
Sep 4, 2024
Merged

Specialize uses of new dict literal to direct JS object creation#6538
zth merged 20 commits into
masterfrom
specialize-js-dict-fromArray

Conversation

@zth

@zth zth commented Dec 21, 2023

Copy link
Copy Markdown
Member

A Dict is obviously a JS object at runtime, but there has been no way to create a dict in a way that compiles to a regular JS object creation. This can be a bit confusing when looking at the generated code, as well as hurt performance (albeit slightly in most cases) as creating dict objects are done dynamically via an array.

This changes so that any usage of the new dict{} literal gets compiled to an actual JS object creation.

Related issues:

@cknitt

cknitt commented Jan 4, 2024

Copy link
Copy Markdown
Member

What about RescriptCore.Dict.fromArray?

@zth

zth commented Jan 4, 2024

Copy link
Copy Markdown
Member Author

Yeah that's a good point, we should make sure this PR handles both.

@zth

zth commented Jan 25, 2024

Copy link
Copy Markdown
Member Author

Dict should be promoted to a built in type and specialized that way instead.

@zth zth added this to the v12 milestone Jan 25, 2024
This was referenced Jan 29, 2024
@zth zth mentioned this pull request Mar 5, 2024
2 tasks
@zth zth force-pushed the specialize-js-dict-fromArray branch from 45b4d6a to f67c121 Compare May 26, 2024 14:12
@zth zth marked this pull request as ready for review May 26, 2024 19:09
@zth

zth commented May 26, 2024

Copy link
Copy Markdown
Member Author

@cristianoc please have a look at whether this approach is good and if the optimization is in the correct place. Maybe it could be put somewhere else?

When Core moves into the compiler (#6761) then we'll change this to account for Dict.fromArray as well.

@zth zth force-pushed the specialize-js-dict-fromArray branch from f261c58 to 41af695 Compare August 30, 2024 12:48
Comment thread jscomp/ml/translcore.ml Outdated
@zth

zth commented Aug 30, 2024

Copy link
Copy Markdown
Member Author

Got a ton of unrelated changes here rebasing, including to test JS files. Not sure why. Also changes to build.ninja, not sure why either.

@cknitt

cknitt commented Aug 30, 2024

Copy link
Copy Markdown
Member

Got a ton of unrelated changes here rebasing, including to test JS files. Not sure why. Also changes to build.ninja, not sure why either.

build.ninja has changed because DictTest.res was added.

The line number changes in the JS output must be from a previous PR, maybe I forgot to check those in when reformatting the tests.

@zth

zth commented Aug 30, 2024

Copy link
Copy Markdown
Member Author

@cknitt could you check if that's the case, with the reformatting in master?

@cknitt

cknitt commented Aug 30, 2024

Copy link
Copy Markdown
Member

Yes, I currently get the same JS output files changed on master.

@cknitt

cknitt commented Aug 30, 2024

Copy link
Copy Markdown
Member

Shall I create a separate PR for committing those JS output changes?

@cknitt

cknitt commented Aug 30, 2024

Copy link
Copy Markdown
Member

Shall I create a separate PR for committing those JS output changes?

@zth #6992

@zth zth force-pushed the specialize-js-dict-fromArray branch from 520d0e6 to 58f541e Compare August 30, 2024 15:14

@cristianoc cristianoc left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good.
Left comment on robustness of path detection.

Comment thread jscomp/ml/translcore.ml Outdated
@zth zth force-pushed the specialize-js-dict-fromArray branch from 58f541e to ad43ed0 Compare August 30, 2024 19:11
@zth zth changed the title Specialize Js.Dict.fromArray to compile to direct JS object creation Specialize uses of new dict literal to direct JS object creation Sep 3, 2024
@zth zth requested a review from cristianoc September 3, 2024 20:24
@zth

zth commented Sep 3, 2024

Copy link
Copy Markdown
Member Author

@cristianoc check again please. I changed the approach to use a builtin primitive instead, and it now only works on the new dict{} literal, which I think makes sense.

@cristianoc cristianoc left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks great

Comment thread jscomp/syntax/src/res_comments_table.ml Outdated

@cristianoc cristianoc left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The only minor comment is the syntax does not let us remove Js.Dict in future.
Just adding a reminder issue should be enough I think. Or in the issue about moving core into the compiler.

@zth

zth commented Sep 4, 2024

Copy link
Copy Markdown
Member Author

The only minor comment is the syntax does not let us remove Js.Dict in future. Just adding a reminder issue should be enough I think. Or in the issue about moving core into the compiler.

Why is that? This is in the syntax so we should be able to just change the path the dict literal produces, no?

Regardless, maybe there's a better module to put this and other internal-only functions like for await? Several benefits would come from that:

  • Never accidentally trigger autocomplete for this function if you pipe a dict
  • No issue like the one above stated where we can't move modules around. They can all just remain in the internal one

What do you think?

Another observation on this change - previously there was a wip PR that added spreads to dicts, which is quite nice. That's probably a bit harder to achieve with this structure. And it's a nice feature, so it'd be cool to have it at some point.

@zth

zth commented Sep 4, 2024

Copy link
Copy Markdown
Member Author

Trying to figure out why the syntax roundtrip tests still are broken.

@cristianoc

Copy link
Copy Markdown
Collaborator

The only minor comment is the syntax does not let us remove Js.Dict in future. Just adding a reminder issue should be enough I think. Or in the issue about moving core into the compiler.

Why is that? This is in the syntax so we should be able to just change the path the dict literal produces, no?

Regardless, maybe there's a better module to put this and other internal-only functions like for await? Several benefits would come from that:

  • Never accidentally trigger autocomplete for this function if you pipe a dict
  • No issue like the one above stated where we can't move modules around. They can all just remain in the internal one

What do you think?

Another observation on this change - previously there was a wip PR that added spreads to dicts, which is quite nice. That's probably a bit harder to achieve with this structure. And it's a nice feature, so it'd be cool to have it at some point.

Sounds like a great idea.
My only comment was trivial: if we remove the path, the trick does not work anymore. So we'll need to remember to change the path when that happens. And check that the trick works today when one opens core by default.

@cristianoc

Copy link
Copy Markdown
Collaborator

Trying to figure out why the syntax roundtrip tests still are broken.

you did not check in the changes -- les's see if CI works now

Comment thread jscomp/syntax/tests/printer/expr/expected/dict.res.txt Outdated
@zth zth requested review from cknitt and cristianoc September 4, 2024 08:56
@zth

zth commented Sep 4, 2024

Copy link
Copy Markdown
Member Author

@cristianoc @cknitt look again, the creator fn is now in its own runtime module.

Comment thread packages/artifacts.txt Outdated
@cristianoc

Copy link
Copy Markdown
Collaborator

can you check that the dict syntax is not formatted away?
that's what I interpret the print tests to be saying at the moment

Comment thread jscomp/syntax/tests/printer/expr/expected/dict.res.txt Outdated
Comment thread jscomp/test/DictTests.res
@@ -0,0 +1,8 @@
let someString = "hello"

let createdDict = dict{

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe also add a Dict<int> for testing?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Sure, adding.

@cristianoc

Copy link
Copy Markdown
Collaborator

ship it

@zth zth enabled auto-merge (squash) September 4, 2024 12:20
@zth zth merged commit 85eb864 into master Sep 4, 2024
@nojaf

nojaf commented Sep 4, 2024

Copy link
Copy Markdown
Member

ship it

Any timeline on the next alpha?

@zth zth deleted the specialize-js-dict-fromArray branch September 4, 2024 12:41
@zth

zth commented Sep 4, 2024

Copy link
Copy Markdown
Member Author

@nojaf not sure, but probably not too far away. We can release the alphas pretty much whenever we want to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

4 participants