Closed elfring closed 2 years ago
Besides being only syntatical sugar what would be the immediate benefit?
The documentation for the programming language “Java” is providing the following information.
A compound assignment expression of the form E1 op= E2 is equivalent to …, except that E1 is evaluated only once.
Do in-place data modifications achieve more desirable software run time characteristics at the shown source code places? :thinking: (Other information sources offer more hints about such implementation details.)
When there will be a need to change the specific code it may get updated according to the likes of the coder but currently I don't see an urgent need as it still defines as syntatic sugar.
:thought_balloon: Can the shown source code adjustments become desirable for the run time characteristics of this software?
Does syntactical sugar add performance benefits to ANY language? No. --> No.
:thought_balloon: I got other impressions according to the mentioned implementation details.
Then maybe you shouldn´t only read the Java Specification but test the results for yourself... The following code contains both old style and syntatical sugar style functions which perform exactly the same:
package com.company;
public class Main {
public static void test_old(String var) {
String execStr = "";
execStr = execStr + var;
}
public static void test_new(String var) {
String execStr = "";
execStr += var;
}
public static void main(String[] args) {
String var = "hello";
test_old(var);
test_new(var);
}
}
Look at the decompiled statements for both functions...They are exactly the same:
// class version 61.0 (61)
// access flags 0x21
public class com/company/Main {
// compiled from: Main.java
// access flags 0x19
public final static INNERCLASS java/lang/invoke/MethodHandles$Lookup java/lang/invoke/MethodHandles Lookup
// access flags 0x1
public <init>()V
L0
LINENUMBER 3 L0
ALOAD 0
INVOKESPECIAL java/lang/Object.<init> ()V
RETURN
L1
LOCALVARIABLE this Lcom/company/Main; L0 L1 0
MAXSTACK = 1
MAXLOCALS = 1
// access flags 0x9
public static test_old(Ljava/lang/String;)V
L0
LINENUMBER 6 L0
LDC ""
ASTORE 1
L1
LINENUMBER 7 L1
ALOAD 1
ALOAD 0
INVOKEDYNAMIC makeConcatWithConstants(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String; [
// handle kind 0x6 : INVOKESTATIC
java/lang/invoke/StringConcatFactory.makeConcatWithConstants(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite;
// arguments:
"\u0001\u0001"
]
ASTORE 1
L2
LINENUMBER 8 L2
RETURN
L3
LOCALVARIABLE var Ljava/lang/String; L0 L3 0
LOCALVARIABLE execStr Ljava/lang/String; L1 L3 1
MAXSTACK = 2
MAXLOCALS = 2
// access flags 0x9
public static test_new(Ljava/lang/String;)V
L0
LINENUMBER 11 L0
LDC ""
ASTORE 1
L1
LINENUMBER 12 L1
ALOAD 1
ALOAD 0
INVOKEDYNAMIC makeConcatWithConstants(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String; [
// handle kind 0x6 : INVOKESTATIC
java/lang/invoke/StringConcatFactory.makeConcatWithConstants(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite;
// arguments:
"\u0001\u0001"
]
ASTORE 1
L2
LINENUMBER 13 L2
RETURN
L3
LOCALVARIABLE var Ljava/lang/String; L0 L3 0
LOCALVARIABLE execStr Ljava/lang/String; L1 L3 1
MAXSTACK = 2
MAXLOCALS = 2
// access flags 0x9
public static main([Ljava/lang/String;)V
L0
LINENUMBER 16 L0
LDC "hello"
ASTORE 1
L1
LINENUMBER 17 L1
ALOAD 1
INVOKESTATIC com/company/Main.test_old (Ljava/lang/String;)V
L2
LINENUMBER 18 L2
ALOAD 1
INVOKESTATIC com/company/Main.test_new (Ljava/lang/String;)V
L3
LINENUMBER 19 L3
RETURN
L4
LOCALVARIABLE args [Ljava/lang/String; L0 L4 0
LOCALVARIABLE var Ljava/lang/String; L1 L4 1
MAXSTACK = 1
MAXLOCALS = 2
}
… but test the results for yourself...
:thought_balloon: Where is it documented that the suggested source code transformation would be performed already as a possible optimisation by a selected Java compiler version?
You may want to seek answer for this maybe on quora or stackoverflow. But again: syntactic sugar is not an optimization. But here is the wrong place for further discussions.
:eyes: Some source code analysis tools can help to find opportunities for improving software components. :thought_balloon: I propose to increase the usage of compound operators accordingly.