Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Improve option optimization for constants#7913

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
cknitt merged 2 commits intomasterfromoption-optimization
Sep 21, 2025
Merged

Conversation

@cknitt
Copy link
Member

Optimizes the option alias pass so we skip the redundant is-not-none guard whenever the compiler already knows the value isSome, which trims the JS we emit for simple constant cases.


functionconstant(){
console.log(42);
}
Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Before this change:

functionconstant(){letopt=42;if(opt!==undefined){console.log(opt);return;}}

@pkg-pr-new
Copy link

pkg-pr-newbot commentedSep 20, 2025
edited
Loading

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript-lang/rescript@7913

@rescript/darwin-arm64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-arm64@7913

@rescript/darwin-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-x64@7913

@rescript/linux-arm64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/linux-arm64@7913

@rescript/linux-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/linux-x64@7913

@rescript/runtime

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/runtime@7913

@rescript/win32-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/win32-x64@7913

commit:64b3b83

Copy link
Collaborator

@cristianoccristianoc left a comment

Choose a reason for hiding this comment

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

This looks great.
I have a process going trying to find adversarial attacks, as an experiment.
This change looks so simple and clearly right, I don't expect surprises.

@cristianoc
Copy link
Collaborator

CODEX review:

The new clause incompiler/core/lam_pass_remove_alias.ml:87 skips thePis_not_none guard whenever the metadata says we have a constantSome. That’s fine for well-behaved values, but it removes the last runtime check that protects the non-nested option encoding from strayundefineds. Before this change, constants showed up inLam_stats asConstant, so we kept emittingfoo !== undefined ? … : …; with the new shortcut we collapse directly to theSome arm.

You can see the behavioural change with a tiny repro that smuggles anundefined payload throughObj.magic (something we allow today for interop):

letfoo:option<int>=Obj.magic(Some(Obj.magic(None:option<int>)))letres=switchfoo {  |Some(_)=>"some"  |None=>"none"  }Js.log(res)
  • Building the compiler before this patch still outputs code that performsfoo !== undefined at runtime, so it prints "none" when the payload is actuallyundefined.
  • After this patch lands, the generated JS becomeslet res = "some";, so the same program now prints "some".

I verified this by locally reverting the newConstant c branch, rebuilding (make), and comparing the generatedtmp_magic_weird.js/cjs. The regression only reproduces once the new shortcut is present.

Could we gate the optimisation onLam_constant.is_some_none_aux (i.e. ensure we’re not in the non-nested encoding with anundefined payload), or fall back to the old behaviour when the payload can beundefined?

@cristianoc
Copy link
Collaborator

Screenshot 2025-09-21 at 08 01 05

@cristianoc
Copy link
Collaborator

CODEX follow-up:

You’re absolutely right—the simplified JS (let res = "some";) was already produced before this patch. I rebuilt the parent commit to double-check and confirmed that I’d misread the before/after comparison. Sorry for the noise!

@cristianoc
Copy link
Collaborator

However, the report is legit in a wider sense.
It opens a question of what we want to do with incorrect types coming from obj magic or incorrect externals.
I think we need a clear position on that.

@cknittcknitt merged commit645e1a9 intomasterSep 21, 2025
25 checks passed
@cknitt
Copy link
MemberAuthor

However, the report is legit in a wider sense. It opens a question of what we want to do with incorrect types coming from obj magic or incorrect externals. I think we need a clear position on that.

I think with incorrect externals, all bets are off.

@cknittcknitt deleted the option-optimization branchSeptember 21, 2025 06:57
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@cristianoccristianoccristianoc approved these changes

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@cknitt@cristianoc

[8]ページ先頭

©2009-2025 Movatter.jp