Skip to content

Suboptimal equals$extension for value classes. #3140

Closed
@nicolasstucki

Description

@nicolasstucki

The class class Foo(val bits: Long) extends AnyVal produces the following bytecode:

In `scalac`
 public final boolean equals$extension(long, java.lang.Object);
    Code:
       0: aload_3
       1: astore        4
       3: aload         4
       5: instanceof    #31                 // class Foo
       8: ifeq          17
      11: iconst_1
      12: istore        5
      14: goto          20
      17: iconst_0
      18: istore        5
      20: iload         5
      22: ifeq          53
      25: aload_3
      26: checkcast     #31                 // class Foo
      29: invokevirtual #35                 // Method Foo.bits:()J
      32: lstore        6
      34: lload_1
      35: lload         6
      37: lcmp
      38: ifne          45
      41: iconst_1
      42: goto          46
      45: iconst_0
      46: ifeq          53
      49: iconst_1
      50: goto          54
      53: iconst_0
      54: ireturn
 
In `dotty`
public final boolean equals$extension(long, java.lang.Object);
    Code:
       0: aload_3
       1: astore        4
       3: aload         4
       5: instanceof    #34                 // class Foo
       8: ifeq          77
      11: aload         4
      13: ifnonnull     23
      16: aconst_null
      17: invokestatic  #38                 // Method scala/runtime/BoxesRunTime.unboxToLong:(Ljava/lang/Object;)J
      20: goto          31
      23: aload         4
      25: checkcast     #34                 // class Foo
      28: invokevirtual #42                 // Method Foo.bits:()J
      31: lstore        5
      33: lload         5
      35: lstore        7
      37: lload_1
      38: invokestatic  #24                 // Method scala/runtime/BoxesRunTime.boxToLong:(J)Ljava/lang/Long;
      41: lload         7
      43: invokestatic  #24                 // Method scala/runtime/BoxesRunTime.boxToLong:(J)Ljava/lang/Long;
      46: astore        9
      48: dup
      49: ifnonnull     61
      52: pop
      53: aload         9
      55: ifnull        69
      58: goto          73
      61: aload         9
      63: invokevirtual #46                 // Method java/lang/Object.equals:(Ljava/lang/Object;)Z
      66: ifeq          73
      69: iconst_1
      70: goto          74
      73: iconst_0
      74: goto          106
      77: aload         4
      79: instanceof    #4                  // class java/lang/Object
      82: ifeq          96
      85: aload         4
      87: checkcast     #4                  // class java/lang/Object
      90: astore        10
      92: iconst_0
      93: goto          106
      96: new           #48                 // class scala/MatchError
      99: dup
     100: aload         4
     102: invokespecial #51                 // Method scala/MatchError."":(Ljava/lang/Object;)V
     105: athrow
     106: ireturn
 

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions