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

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

Merged
merged 20 commits into from
Sep 4, 2024

Conversation

zth
Copy link
Collaborator

@zth zth commented Dec 21, 2023

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
Copy link
Member

cknitt commented Jan 4, 2024

What about RescriptCore.Dict.fromArray?

@zth
Copy link
Collaborator Author

zth commented Jan 4, 2024

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

@zth
Copy link
Collaborator Author

zth commented Jan 25, 2024

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
Copy link
Collaborator Author

zth commented May 26, 2024

@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
jscomp/ml/translcore.ml Outdated Show resolved Hide resolved
@zth
Copy link
Collaborator Author

zth commented Aug 30, 2024

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
Copy link
Member

cknitt commented Aug 30, 2024

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
Copy link
Collaborator Author

zth commented Aug 30, 2024

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

@cknitt
Copy link
Member

cknitt commented Aug 30, 2024

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

@cknitt
Copy link
Member

cknitt commented Aug 30, 2024

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

@cknitt
Copy link
Member

cknitt commented Aug 30, 2024

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
Copy link
Collaborator

@cristianoc cristianoc left a comment

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.

jscomp/ml/translcore.ml Outdated Show resolved Hide resolved
@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
Copy link
Collaborator Author

zth commented Sep 3, 2024

@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.

Copy link
Collaborator

@cristianoc cristianoc left a comment

Choose a reason for hiding this comment

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

Looks great

jscomp/syntax/src/res_comments_table.ml Outdated Show resolved Hide resolved
Copy link
Collaborator

@cristianoc cristianoc left a comment

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
Copy link
Collaborator Author

zth commented Sep 4, 2024

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
Copy link
Collaborator Author

zth commented Sep 4, 2024

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

@cristianoc
Copy link
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
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

@zth
Copy link
Collaborator Author

zth commented Sep 4, 2024

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

packages/artifacts.txt Outdated Show resolved Hide resolved
@cristianoc
Copy link
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

@@ -0,0 +1,8 @@
let someString = "hello"

let createdDict = dict{
Copy link
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
Collaborator 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
Collaborator

ship it

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

nojaf commented Sep 4, 2024

ship it

Any timeline on the next alpha?

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

zth commented Sep 4, 2024

@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
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants