ARN

Kotlin adds dynamic method invocation for JVM

Experimental feature in Kotlin 1.4.20 allows string concatenations to be compiled into dynamic invocations for JDK 9 or later targets

JetBrains has released Kotlin 1.4.20, an upgrade to its language for the JVM, Android, and the browser, with an experimental capability for JVM-based string concatenation via the dynamic method invocation leading a list of new features.

String concatenation on the JVM since JDK 9 has been done via the dynamic method invocation, the invokedynamiccode instruction in the bytecode. This is faster and consumes less memory than the previous implementation and leaves room for future optimizations without needing bytecode changes.

This mechanism is now being implemented in Kotlin to improve performance. String concatenations now can be compiled into dynamic invocations for JDK 9 or later targets. The experimental feature can be invoked for the following use cases, via adding the -Xstring-concat option:

  • String.plus in the operator (a + bcode), explicit (a.plus(b)), and reference ( (a::plus)(b) ) forms.
  • toString on inline and data classes.
  • String templates, except for those with a single non-constant argument.

Developers must invoke one of the following rules to use invokedynamic:

  • indy-with-constants to perform invokedynamic concatenation on strings with <a href="https://docs.oracle.com/javase/9/docs/api/java/lang/invoke/StringConcatFactory.html#makeConcatWithConstants-java.lang.invoke.MethodHandles.Lookup-java.lang.String-java.lang.invoke.MethodType-java.lang.String-java.lang.Object...-" rel="nofollow">StringConcatFactory.makeConcatWithConstants()</a>.
  • indy to perform invokedynamic concatenation on strings with StringConcatFactory.makeConcat().
  • inline to switch back to the classic concatenation via StringBuilder.append().

JetBrains announced the release of Kotlin 1.4.20 on November 23. The command line compiler for Kotlin 1.4.20 can be downloaded from GitHub. IntelliJ Idea and Android Studio IDE users can find instructions for updating their Kotlin plug-in here.

Kotlin 1.4.20 also features the following:

  • For Kotlin/JS, new templates are offered for the project wizard. Control over project configuration has been enhanced via an improved DSL capability. The IR compiler has been fitted with a new way to compile projects, ignoring errors in code.
  • The Kotlin/JS Gradle DSL now offers simplified project setup and customization. Also, a single point is offered for webpack configuration.
  • The project wizard for Kotlin has added adjustable templates for Kotlin/JS applications. Templates are featured for browser, Node.js, and React applications.
  • Entries to the package.json file can be made from the Gradle build script.
  • An experimental selective yarn dependency resolutions capability is featured, configurable from the Gradle build script.
  • The JDK 15 target has been added.
  • Behavior inconsistencies between Kotlin/Native and Kotlin/JVM have been fixed in some corner cases, such as property initialization. Also for Kotlin/Native, Objective-C exceptions can be wrapped into Kotlin exceptions.
  • The standard library includes experimental extensions for java.nio.file.Path. Also, the performance of the String.Replace function has been improved.