Skip to content

Specialize Any.{==, !=} when inlining #12157

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

Merged

Conversation

nicolasstucki
Copy link
Contributor

@nicolasstucki nicolasstucki commented Apr 20, 2021

Their specialized versions are known to have the same semantics as the generic == or !=.

Improvement related to #11998
Fiixes #12161

@nicolasstucki nicolasstucki linked an issue Apr 21, 2021 that may be closed by this pull request
@nicolasstucki nicolasstucki marked this pull request as ready for review April 21, 2021 07:50
case None => tree
}
case _ =>
tree
Copy link
Contributor

Choose a reason for hiding this comment

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

What are the criteria for deciding what methods should be specialized? Is it possible to generalize the specialization?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The criteria is that it is an == or != on Any

sel.symbol == defn.Any_== || sel.symbol == defn.Any_!=

and that the types of the compared values are the same value class

defn.ScalaValueClasses().find { cls =>
          arg1.tpe.derivesFrom(cls) && arg2.tpe.derivesFrom(cls)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We might generalize it for different classes. For example Short == Int.

The other potential generalization is on Numeric ops but that is much more complex.

Copy link
Contributor

Choose a reason for hiding this comment

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

I am thinking why even specialize == and !=. This seems to related to the discussion about whether inlining should re-resolve selections. Such specialization helps very little in that perspective, I'm thinking whether it's worthwhile to complicate the compiler.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, in this case it is about the partial evaluation of the operation. If we have (2: Any) == (3: Any) we know that this will end calling 2 == 3 and return that result.

All the testing frameworks will benefit from this as the variants of assertEquals will generate this code. That is already a large enough use case.

Their specilized versions are known to have the same semantics as the generic `==` or `!=`.

Improvement related to scala#11998
@nicolasstucki nicolasstucki force-pushed the specialize-any-eq-on-inlining branch from 6908d97 to 48f0e89 Compare April 21, 2021 09:00
Copy link
Contributor

@liufengyun liufengyun left a comment

Choose a reason for hiding this comment

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

LGTM

@japgolly
Copy link
Contributor

Thanks @nicolasstucki !

@nicolasstucki nicolasstucki merged commit 6f1846a into scala:master Apr 22, 2021
@nicolasstucki nicolasstucki deleted the specialize-any-eq-on-inlining branch April 22, 2021 09:06
@Kordyjan Kordyjan added this to the 3.0.1 milestone Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inefficient bytecode generated compared to Scala 2.13
4 participants