-
-
Notifications
You must be signed in to change notification settings - Fork 873
Optimise comparison with singleton custom types on JavaScript #4903. … #4951
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
Open
re-masashi
wants to merge
13
commits into
gleam-lang:main
Choose a base branch
from
re-masashi:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+855
−28
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1a5ce62
Optimise comparison with singleton custom types on JavaScript #4903. …
re-masashi c0bc2f3
implemented optimizations for guard clauses as well. added proper tes…
re-masashi 1a7329b
PLUH
re-masashi bcd37c2
finalized optimization for all cases. added more tests. cleaned the c…
re-masashi e93197e
refactor
re-masashi 0f67076
refactor
re-masashi 3f65f99
refactor
re-masashi 4f0046e
refactor
re-masashi ba10e88
refactor. addded helper methods for singleton equality codegen
re-masashi eb37f27
Merge branch 'gleam-lang:main' into main
re-masashi 95c73b4
refactor
re-masashi 3d76e01
added tests to ensure there are no false positives (optmizations dont…
re-masashi a05544e
refactor
re-masashi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1536,14 +1536,75 @@ impl<'module, 'a> Generator<'module, 'a> { | |
return docvec![left_doc, operator, right_doc]; | ||
} | ||
|
||
// For comparison with singleton custom types, ie, one with no fields. | ||
// If you have some code like this | ||
// ```gleam | ||
// pub type Wibble { | ||
// Wibble | ||
// Wobble | ||
// } | ||
|
||
// pub fn is_wibble(w: Wibble) -> Bool { | ||
// w == Wibble | ||
// } | ||
// ``` | ||
// Instead of `isEqual(w, new Wibble())`, generate `w instanceof Wibble` | ||
// because the first approach needs to construct a new Wibble, and then call the isEqual function, | ||
// which supports any shape of data, and so does a lot of extra logic which isn't necessary. | ||
|
||
if let Some(doc) = self.singleton_variant_equality(left, right, should_be_equal) { | ||
return doc; | ||
} | ||
|
||
if let Some(doc) = self.singleton_variant_equality(right, left, should_be_equal) { | ||
return doc; | ||
} | ||
|
||
// Other types must be compared using structural equality | ||
let left = | ||
self.not_in_tail_position(Some(Ordering::Strict), |this| this.wrap_expression(left)); | ||
let right = | ||
self.not_in_tail_position(Some(Ordering::Strict), |this| this.wrap_expression(right)); | ||
|
||
self.prelude_equal_call(should_be_equal, left, right) | ||
} | ||
|
||
fn singleton_variant_equality( | ||
&mut self, | ||
left: &'a TypedExpr, | ||
right: &'a TypedExpr, | ||
should_be_equal: bool, | ||
) -> Option<Document<'a>> { | ||
if let TypedExpr::Var { | ||
constructor: | ||
ValueConstructor { | ||
variant: ValueConstructorVariant::Record { arity: 0, name, .. }, | ||
.. | ||
}, | ||
.. | ||
} = right | ||
{ | ||
let left_doc = self | ||
.not_in_tail_position(Some(Ordering::Strict), |this| this.wrap_expression(left)); | ||
Some(self.singleton_equal(left_doc, name, should_be_equal)) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
fn singleton_equal( | ||
&self, | ||
value: Document<'a>, | ||
tag: &EcoString, | ||
should_be_equal: bool, | ||
) -> Document<'a> { | ||
if should_be_equal { | ||
docvec![value, " instanceof ", tag.to_doc()] | ||
} else { | ||
docvec!["!(", value, " instanceof ", tag.to_doc(), ")"] | ||
} | ||
} | ||
|
||
fn equal_with_doc_operands( | ||
&mut self, | ||
left: Document<'a>, | ||
|
@@ -1966,16 +2027,26 @@ impl<'module, 'a> Generator<'module, 'a> { | |
docvec![left, " !== ", right] | ||
} | ||
|
||
ClauseGuard::Equals { left, right, .. } => { | ||
let left = self.guard(left); | ||
let right = self.guard(right); | ||
self.prelude_equal_call(true, left, right) | ||
} | ||
ClauseGuard::Equals { left, right, .. } | ||
| ClauseGuard::NotEquals { left, right, .. } => { | ||
let should_be_equal = matches!(guard, ClauseGuard::Equals { .. }); | ||
|
||
// Handle singleton equality optimization for guards | ||
if let Some(doc) = | ||
self.singleton_variant_guard_equality(left, right, should_be_equal) | ||
{ | ||
return doc; | ||
} | ||
|
||
ClauseGuard::NotEquals { left, right, .. } => { | ||
let left = self.guard(left); | ||
let right = self.guard(right); | ||
self.prelude_equal_call(false, left, right) | ||
if let Some(doc) = | ||
self.singleton_variant_guard_equality(right, left, should_be_equal) | ||
{ | ||
return doc; | ||
} | ||
|
||
let left_doc = self.guard(left); | ||
let right_doc = self.guard(right); | ||
self.prelude_equal_call(should_be_equal, left_doc, right_doc) | ||
} | ||
|
||
ClauseGuard::GtFloat { left, right, .. } | ClauseGuard::GtInt { left, right, .. } => { | ||
|
@@ -2078,6 +2149,26 @@ impl<'module, 'a> Generator<'module, 'a> { | |
} | ||
} | ||
|
||
fn singleton_variant_guard_equality( | ||
&mut self, | ||
left: &'a TypedClauseGuard, | ||
right: &'a TypedClauseGuard, | ||
should_be_equal: bool, | ||
) -> Option<Document<'a>> { | ||
if let ClauseGuard::Constant(Constant::Record { | ||
arguments, name, .. | ||
}) = right | ||
&& arguments.is_empty() | ||
&& right.type_().is_named() | ||
&& let ClauseGuard::Var { type_, .. } = left | ||
&& !matches!(&**type_, Type::Fn { .. }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use the same logic as the expression equality here, where we check the |
||
{ | ||
let left_doc = self.guard(left); | ||
return Some(self.singleton_equal(left_doc, name, should_be_equal)); | ||
} | ||
None | ||
} | ||
|
||
fn wrapped_guard(&mut self, guard: &'a TypedClauseGuard) -> Document<'a> { | ||
match guard { | ||
ClauseGuard::Var { .. } | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we extract the duplicated bits to helper methods please 🙏
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
surely
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This hasn't been done yet! The patterns are still duplicated. Could we write something like this?:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is still outstanding 🙏
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This still hasn't been done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once again, this still needs to be done