What is Kotlin 1.5.20 string concatenation via invokedynamic?

Kotlin, designed by JetBrains, is an open-source programming language. It is mainly used to develop Android mobile apps. Kotlin is fully compatible with Java.

Kotlin 1.5.20

To stay up to date, Kotlin updated and fixed the issues in Kotlin 1.5.0. Kotlin 1.5.20 is now released, and shows improvements in Java features and support.

The following are some of the updates and fixes in Kotlin 1.5.20.

  • Kotlin/JVM improvements
  • Kotlin/Native improvements
  • Kotlin/JS IR backend migration guide
  • Gradle improvements
  • Standard library improvements

Kotlin/JVM improvements

The improvements include:

  • string concatenation via invokedynamic.
  • experimental support to call Java’s Lombok-generated methods.
  • JSpecify nullness annotations.

Kotlin has extended the use of dynamic invocations, invokedynamic, in order to take advantage of the latest JVM features. Default support for compilation of SAM adapters was a feature of Kotlin 1.5.0. The feature was unbroken for string concatenation and lambdas experiments.

Additionally, compilation string concatenation to dynamic invocations is still set as a default in Kotlin 1.5.20.

invokedynamic

The main idea of invokedynamic is a more dynamic implementation. Without changing the bytecode, it makes it possible to change the concatenation approach. It offers optimization without recompilation.

Listed below are some other advantages of bytecode for invokedynamic.

  • Precision
  • It is more sophisticated
  • Less brittle
  • Speed
  • Consumes less memory as compared to the previous implementation

Before invokedynamic

String concatenation was implemented by StringBuilder before invokedynamic. However, string concatenation can now be performed through the use of invokedynamic.

For example, to concatenate a constant string with a variable before invokedynamic, we would write the following code.

"educative" + ThreadLocalRandom.current().nextInt();

After invokedynamic

Now, string concatenation can be performed via invokedynamic. The invokedynamic instruction clarifies and possibly improves implementations of compilers and runtime systems for dynamic languages on the JVM. To perform this, the invokedynamic instruction permits the language implementer to outline custom linkage behavior.

Dynamic call site

Each incidence of an invokedynamic command is called a dynamic call site.

A dynamic call site is formerly in an unlinked state, which means that there is no method specified for the call site to invoke.

Bootstrap method

A dynamic call site is joined to a technique via the bootstrap method.

The bootstrap methodology for a dynamic call site is specified by the compiler for the dynamically-typed language that is referred once by the JVM to link the location.

The call site’s behavior can be determined by the object returned from the bootstrap method.

Dynamically linked method with invokedynamic

The following are the steps to invoke a dynamically linked method with the invokedynamic.

  • Define the Bootstrap method.
  • Specify Constant Pool Entries.
  • Use the invokedynamic instruction.

Free Resources