BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Kotlin 1.6.20 Preview Features Context Receivers and Performance Improvements

Kotlin 1.6.20 Preview Features Context Receivers and Performance Improvements

This item in japanese

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

IntelliJ IDEA or Android Studio should be configured to use the Early Access Preview features via Tools - Kotlin - Configure Kotlin Plugin Updates and then install the latest preview version. The application’s build scripts should be updated to the latest Kotlin preview version: 1.6.20-M1.

Context receivers provide support for multiple receivers, instead of just one, that may be added to the declaration of functions, properties and classes to make them context-dependent. All context receivers should be declared as implicit receivers in the caller’s scope. For example, by declaring an interface that contains a reference to Logger and then using the logger property in a function by declaring the LoggingContext interface as an 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 compiler option may be used to create pre-release binaries in order to test the functionality. Since the feature isn’t complete yet, the binaries shouldn’t be used in production and there’s minimal support from the IDE.

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

When the build is already parallelized by the build tool, then adding another layer of parallelization might even increase the build time. The JVM IR backend mode doesn’t work with the no-longer-maintained kapt annotation processors as kapt disables the backend mode.

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

kotlin.incremental.js.ir=true

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

By default, Kotlin 1.6.20 enables hierarchical structure support for multiplatform projects to share source code across native targets to simplify the build setup and improve IDE support.

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

Support is available by registering in the Slack eap channel and issues may be reported in YouTrack.

About the Author

Rate this Article

Adoption
Style

BT