Skip to content

The backend never generates iinc #7452

Closed
scala/scala
#9713
@scabug

Description

@scabug

In the slightly shocking fact of the day dept. It is a decadent waste of bytecode to be generating four instructions including a load and a store when a single instruction can increment in-place.

A typical example - this is a single class:

git diff -w head^ head -- scala/collection/immutable/Range.class
 diff --git a/scala/collection/immutable/Range.class b/scala/collection/immutable/Range.class
 index 6fe5a67ab1..7bf47a582e 100644
 --- a/scala/collection/immutable/Range.class
 +++ b/scala/collection/immutable/Range.class
 @@ -371,10 +371,7 @@ public class scala.collection.immutable.Range extends scala.collection.AbstractS
         : invokestatic  #                 // Method scala/runtime/BoxesRunTime.boxToInteger:(I)Ljava/lang/Integer;
         : invokeinterface # ,             // InterfaceMethod scala/Function .apply:(Ljava/lang/Object;)Ljava/lang/Object;
         : pop
 -       : iload_2
 -       : iconst_1
 -       : iadd
 -       : istore_2
 +       : iinc           ,
         : iload_3
         : invokevirtual #                 // Method step:()I
         : iadd
 @@ -431,10 +428,7 @@ public class scala.collection.immutable.Range extends scala.collection.AbstractS
         : invokestatic  #                 // Method scala/runtime/BoxesRunTime.boxToInteger:(I)Ljava/lang/Integer;
         : invokeinterface # ,             // InterfaceMethod scala/Function .apply:(Ljava/lang/Object;)Ljava/lang/Object;
         : pop
 -       : iload
 -       : iconst_1
 -       : iadd
 -       : istore
 +       : iinc           ,
         : iload_3
         : iload
        : iadd
 @@ -529,10 +523,7 @@ public class scala.collection.immutable.Range extends scala.collection.AbstractS
         : iload_2
         : invokeinterface # ,             // InterfaceMethod scala/Function .apply$mcZI$sp:(I)Z
         : ifeq
 -       : iload_3
 -       : iconst_1
 -       : iadd
 -       : istore_3
 +       : iinc           ,
         : iload_2
         : invokevirtual #                 // Method step:()I
         : iadd
 @@ -814,10 +805,7 @@ public class scala.collection.immutable.Range extends scala.collection.AbstractS
         : aload_1
         : iload_3
         : invokeinterface # ,             // InterfaceMethod scala/Function .apply$mcVI$sp:(I)V
 -       : iload
 -       : iconst_1
 -       : iadd
 -       : istore
 +       : iinc           ,
         : iload_3
         : iload
         : iadd

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions