question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Handle Javac Optimisation of String Concatenations

See original GitHub issue

Description of the bug It appears that when I am trying to concatenate two Strings, when one is from the storage, then the compiler complains with the following message:

Inherited methods that are not specifically implemented are not supported. Implement the method 'toString' without a 'super' call to the class Object to use it.
io.neow3j.compiler.CompilerException: Inherited methods that are not specifically implemented are not supported. Implement the method 'toString' without a 'super' call to the class Object to use it.
        at io.neow3j.compiler.Compiler.throwIfObjectIsOwner(Compiler.java:603)
        at io.neow3j.compiler.Compiler.handleInsn(Compiler.java:584)
        at io.neow3j.compiler.NeoMethod.convert(NeoMethod.java:481)
        at io.neow3j.compiler.converters.MethodsConverter.handleUncachedMethodCall(MethodsConverter.java:224)

To Reproduce Steps to reproduce the behavior:

  1. create new smart contract
  2. try to concatenate two strings: map.getString(key) + “foo”;
  3. or: map.get(key).toString() + “foo”
  4. compile

Expected behavior It should be possible to concatenate two strings like this without compiling issues.

Platform and Software

  • neow3j 3.17

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:24 (14 by maintainers)

github_iconTop GitHub Comments

2reactions
ykoitzschcommented, Jul 28, 2022

I tested it, and it compiles just fine now @mialbu @gsmachado 😃

1reaction
mialbucommented, May 24, 2022

@ykoitzsch I could reproduce now. Javac is run with different compile options in VSCode than in Intellij. This includes an optimisation concerning the use of the StringBuilder class. It ultimately breaks the compilation to Neo byte code when an optimised concatenation was produced by the Java compiler.

I’ll add this issue to our backlog for now and we’ll address it in the future. As for now, until we’ve fixed this, please use the gradle plugin to compile when developing with VSCode.

If you’re interested in more details, check out this and this.

Additional Info

The following contract class fails to compile using VSCode:

class StringConcatenation {
        private static final StorageContext ctx = Storage.getStorageContext();
        public static String getSomething(String key) {
            return Storage.getString(ctx, key) + "foo";
        }
}

In the following I’ve listed the pattern of the compiled asm class when using either Intellij or VSCode.

Parts of the asmClass instructions based on compilation using Intellij IDE

TypeInsnNode StringBuilder 187
InsnNode 89
MethodInsnNode StringBuilder <init> 183
FieldInsnNode StorageContext ctx 178
VarInsnNode 25
MethodInsnNode Storage getString 184
MethodInsnNode StringBuilder append 182
LdcInsnNode 18
MethodInsnNode StringBuilder append 182
MethodInsnNode StringBuilder toString 182
InsnNode 176

Parts of the asmClass instructions based on compilation using VSCode IDE with RedHat Java extension

TypeInsnNode StringBuilder 187
InsnNode 89
FielInsnNode StorageContext ctx 178
VarInsnNode 25
MethodInsnNode Storage getString 184
MethodInsnNode String valueOf 184
MethodInsnNode String <init>
LdcInsnNode 18
MethodInsnNode StringBuilder append 182
MethodInsnNode StringBuilder toString 182
InsnNode 176
Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Compile time optimization of String concatenation
In Java, most optimization is performed by the runtime's just in time compiler, so generally javac optimizations don't matter much.
Read more >
Java Compiler Optimization for String Concatenation - Medium
StringConcatFactory offers different strategies to generate the CallSite divided in byte-code generator using ASM and MethodHandle-based one.
Read more >
JEP 280: Indify String Concatenation - OpenJDK
This will enable future optimizations of String concatenation without requiring further changes to the bytecode emitted by javac .
Read more >
We Don't Need StringBuilder for Simple Concatenation - DZone
Concatenating strings is useful, but expensive. Fortunately, you don't need to use StringBuilder anymore - the compiler can handle it for you.
Read more >
String concatenation in Java 9 (part 1) - Guardsquare
In this blog, we will focus on a more low-level change to the handling of Strings, namely the “indified String concatenation”. We will...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found