Kotlin 1.6.20 Preview introduces context sinks and performance improvements

0

JetBrains has released Kotlin 1.6.20-M1, a preview of the upcoming GA release that introduces context sinks, several performance enhancement options to reduce compilation time, and concurrent garbage collection.

IntelliJ IDEA or Android Studio must be configured to use the Early Access Preview features via Tools – Kotlin – Configure Kotlin plugin updates then install the latest preview version. The app build scripts should be updated to the latest Kotlin preview version: 1.6.20-M1.

Context receivers support multiple receivers, instead of just one, that can be added to the declaration of functions, properties, and classes to make them context-dependent. All context receivers must be declared as implicit receivers in the caller’s scope. For example, by declaring an interface that contains a reference to Logger then using the logger property in a function by declaring the LoggingContext interface as implicit receiver:

interface LoggingContext {
    val logger: Logger 
}

context(LoggingContext)
fun saveUser(student: Student) {
    // Logic to save the user to the database
    logger.info("User has been saved in the database")
}

Currently, the -Xcontext-receivers The compile option can be used to build pre-release binaries to test functionality. Since the feature is not yet complete, the binaries should not be used in production, and IDE support is minimal.

The experimental JVM IR backend mode was introduced in order to compile all files of a module in parallel to reduce compilation time. The compiler option -Xbackend-threads enables this mode and the number of threads can be configured either to 0, which uses one thread for each CPU core in the machine, or to a value less than the number of CPU cores in the machine. The tradeoff for a decrease in compile time is an increase in heap memory that is proportional to the number of threads used.

When the build is already parallelized by the build tool, adding another layer of parallelization can even increase the build time. JVM IR backend mode does not work with kapt annotation processors that are no longer maintained because kapt disables backend mode.

Incremental compilation for development binaries with Kotlin/JS IR compiler is now available after setup gradle.properties:

kotlin.incremental.js.ir=true

After enabling incremental compilation, the compiler caches previous module compilations when using compileDevelopmentExecutableKotlinJs Gradle task. Especially with small modifications, module caches can be used instead of compiling modules again.

By default, Kotlin 1.6.20 enables hierarchical structure support for cross-platform projects to share source code between native targets to simplify build configuration and improve IDE support .

Kotlin/Native compilation performance has been improved by reducing execution time, bit size, and compilation time by statically initializing some compiler-generated synthetic objects and optimizing compiler caches. The new memory manager reduces execution time and now contains a concurrent implementation for the scan phase of garbage collection which can be enabled with -Xgc=cms.

Support is available by subscribing to the Slack eap channel and issues can be reported in YouTrack.

Share.

About Author

Comments are closed.