Skip to content

Delegate null-check-then-equals to Objects.equals #132

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
wants to merge 1 commit into
base: 2.13.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 2 additions & 17 deletions src/compiler/scala/tools/nsc/backend/jvm/BCodeBodyBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1584,25 +1584,10 @@ abstract class BCodeBodyBuilder extends BCodeSkelBuilder {
genCallMethod(Object_equals, InvokeStyle.Virtual, pos)
genCZJUMP(success, failure, TestOp.NE, BOOL, targetIfNoJump)
} else {
// l == r -> if (l eq null) r eq null else l.equals(r)
val eqEqTempLocal = locals.makeLocal(ObjectRef, nme.EQEQ_LOCAL_VAR.toString)
val lNull = new asm.Label
val lNonNull = new asm.Label

// l == r -> Objects.equals(l, r)
genLoad(l, ObjectRef)
genLoad(r, ObjectRef)
locals.store(eqEqTempLocal)
bc dup ObjectRef
genCZJUMP(lNull, lNonNull, TestOp.EQ, ObjectRef, targetIfNoJump = lNull)

markProgramPoint(lNull)
bc drop ObjectRef
locals.load(eqEqTempLocal)
genCZJUMP(success, failure, TestOp.EQ, ObjectRef, targetIfNoJump = lNonNull)

markProgramPoint(lNonNull)
locals.load(eqEqTempLocal)
genCallMethod(Object_equals, InvokeStyle.Virtual, pos)
genCallMethod(Objects_equals, InvokeStyle.Static, pos)
genCZJUMP(success, failure, TestOp.NE, BOOL, targetIfNoJump)
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/reflect/scala/reflect/internal/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,10 @@ trait Definitions extends api.StandardDefinitions {
}

// top types
lazy val AnyClass = enterNewClass(ScalaPackageClass, tpnme.Any, Nil, ABSTRACT).markAllCompleted()
lazy val AnyRefClass = newAlias(ScalaPackageClass, tpnme.AnyRef, ObjectTpe).markAllCompleted()
lazy val ObjectClass = getRequiredClass("java.lang.Object")
lazy val AnyClass = enterNewClass(ScalaPackageClass, tpnme.Any, Nil, ABSTRACT).markAllCompleted()
lazy val AnyRefClass = newAlias(ScalaPackageClass, tpnme.AnyRef, ObjectTpe).markAllCompleted()
lazy val ObjectClass = getRequiredClass("java.lang.Object")
lazy val ObjectsModule = getRequiredModule("java.util.Objects")

// Cached types for core monomorphic classes
lazy val AnyRefTpe = AnyRefClass.tpe
Expand Down Expand Up @@ -1285,6 +1286,7 @@ trait Definitions extends api.StandardDefinitions {
def Object_equals = getMemberMethod(ObjectClass, nme.equals_)
def Object_hashCode = getMemberMethod(ObjectClass, nme.hashCode_)
def Object_toString = getMemberMethod(ObjectClass, nme.toString_)
def Objects_equals = getMemberMethod(ObjectsModule, nme.equals_)

// boxed classes
lazy val ObjectRefClass = requiredClass[scala.runtime.ObjectRef[_]]
Expand Down
15 changes: 5 additions & 10 deletions test/junit/scala/tools/nsc/backend/jvm/BytecodeTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,11 @@ class BytecodeTest extends BytecodeTesting {
Label(7), Op(ICONST_2), Op(IRETURN)))

// t3: Array == is translated to reference equality, AnyRef == to null checks and equals
assertSameCode(getMethod(c, "t3"), List(
// Array ==
VarOp(ALOAD, 1), VarOp(ALOAD, 2), Jump(IF_ACMPEQ, Label(23)),
// AnyRef ==
VarOp(ALOAD, 2), VarOp(ALOAD, 1), VarOp(ASTORE, 3), Op(DUP), Jump(IFNONNULL, Label(14)),
Op(POP), VarOp(ALOAD, 3), Jump(IFNULL, Label(19)), Jump(GOTO, Label(23)),
Label(14), VarOp(ALOAD, 3), InvokeVirtual("java/lang/Object", "equals", "(Ljava/lang/Object;)Z"), Jump(IFEQ, Label(23)),
Label(19), Op(ICONST_1), Jump(GOTO, Label(26)),
Label(23), Op(ICONST_0),
Label(26), Op(IRETURN)))
assertSameCode(getMethod(c, "t3"), List(
VarOp(ALOAD, 1), VarOp(ALOAD, 2), Jump(IF_ACMPEQ, Label(11)),
VarOp(ALOAD, 2), VarOp(ALOAD, 1),
Invoke(INVOKESTATIC, "java/util/Objects", "equals", "(Ljava/lang/Object;Ljava/lang/Object;)Z", itf = false),
Jump(IFEQ, Label(11)), Op(ICONST_1), Jump(GOTO, Label(14)), Label(11), Op(ICONST_0), Label(14), Op(IRETURN)))

val t4t5 = List(
VarOp(ALOAD, 1), Jump(IFNULL, Label(6)),
Expand Down
Loading