Jetpack Compose now supports K2! :confetti_ball: ...
# announcements
m
Jetpack Compose now supports K2! 🎊 Google has just released the Jetpack Compose compiler plugin, version 1.5.0, which is compatible with Kotlin 1.9.0 and includes experimental support for the K2 compiler. K2 is now in Beta for the JVM and your feedback is crucial at this stage of development. We would highly appreciate if you could enable the K2 compiler in your Kotlin-based Android projects and share your feedback in #k2-early-adopters. We are especially interested in performance benchmarks based on compilation time. If you come across any bugs, quirks, or discrepancies while testing, don’t hesitate to report them! Your feedback will help us fine-tune the compiler and ensure a smooth experience. Give it a try!
K 5
K 12
🤖 19
K 8
😮 15
❤️ 21
jetpack compose 29
❤️ 13
😍 1
💯 52
🎉 34
📌 14
K 123
y
Does K2 also work for Compose Multiplatform?
👀 11
5
m
If anyone is interested in that “performance benchmarks based on compilation time” bit, and you want to know how to do that: my talk from Droidcon Berlin covers this (jump to 21m 04s for the intro to gradle-profiler, or directly to 26m 56s for how to do benchmarks with K2). Here are the scenario files to do the benchmarks: • okhttp_k2.scenarios to compare using the
-Pkotlin.experimental.tryK2=true
flag • okhttp_different_branches.scenarios if you have K2 enabled on a different branch, and you want to benchmark 2 different Git branches Happy K2'ing! 🎉
thank you color 3
❤️ 2
🔥 5
🙌 2
s
@Youssef Shoaib [MOD] we're keeping track of K2 support in Compose Multiplatform here: https://youtrack.jetbrains.com/issue/KT-58953/K2-doesnt-work-with-Compose-Multiplatform
thank you color 11
d
@Sebastian Aigner Amazing! I really think Compose Multiplatform is going to be the way of the future.
6
m
Tried it with a spring boot based project with testcontainer tests. No improvement seen in build time. No problems seen either.
m
@Manuel Bogner how did you test it? Did you run a benchmark? Did you clean & disable the Gradle build cache in order to get clean data? I’m curious 🙂
m
@Marc Reichelt nothing sophisticated at all. I ran the build of my actual small spring based web app multiple times with and without the k2 setting in the properties and compared the numbers that gradle gave me as build time. All from command line. So no benchmark at all - just simple trial and error. I wanted to see if it helps to bring down the build time of non Android projects (as everybody is just talking about Android when it comes to Kotlin) but couldn’t see any relevant difference.
m
Thanks! Well, if you have not disabled the Gradle build cache and haven’t cleaned before each build, you’ll be measuring the ‘how fast can Gradle take the .class files out of the cache’, which won’t even invoke the Kotlin compiler. Try installing the https://github.com/gradle/gradle-profiler (e.g. via homebrew:
brew install gradle-profiler
), save this as your
k2.scenarios
file:
Copy code
default-scenarios = ["without_k2", "with_k2"]

without_k2 {
    title = "Without K2 compiler"
    cleanup-tasks = ["clean"]
    tasks = ["compileKotlin"]
    gradle-args = ["--no-build-cache"]
}

with_k2 {
    title = "With K2 compiler"
    cleanup-tasks = ["clean"]
    tasks = ["compileKotlin"]
    gradle-args = ["--no-build-cache", "-Pkotlin.experimental.tryK2=true"]
}
Then you can run the benchmark like this in your project directory:
gradle-profiler --benchmark --scenario-file k2.scenarios
Hope this helps!
❤️ 1
m
Screenshot 2023-08-03 at 14.31.19.png
Screenshot 2023-08-03 at 14.31.34.png
latest update after some tests with Marc:
m
nice 🎉
Around 20% faster!
m
yep 😃 that’s cool
thanks for the details @Marc Reichelt
🙌 1
m
Today I re-learned: the
build
task will of course also run all checks and tests, so in the first benchmark we actually compared how fast the build & running the tests is. And the Spring Boot tests of course take a lot of time 😅 Now we used
compileKotlin
to run the benchmark and we got some good results 👍
The OkHttp project has a performance improvement of around 30% with K2 using Kotlin 1.9.0-RC 🎉
🎉 2
c
@Manuel Bogner do you have the final
.scenarios
file you were using?
m
@Colton Idle it’s the one Marc posted above
Copy code
default-scenarios = ["without_k2", "with_k2"]

without_k2 {
    title = "Without K2 compiler"
    cleanup-tasks = ["clean"]
    tasks = ["compileKotlin"]
    gradle-args = ["--no-build-cache"]
}

with_k2 {
    title = "With K2 compiler"
    cleanup-tasks = ["clean"]
    tasks = ["compileKotlin"]
    gradle-args = ["--no-build-cache", "-Pkotlin.experimental.tryK2=true"]
}
c
oh alright. it sounded like you both dm'd each other and found something else. im gonna copy that into my project and do some tesitng too. cheers
❤️ 1
m
we did and looked into this in a meeting. outcome: the compilation IS faster but in my spring build it is just a very very small share
K 3
🚀 1
g
if I wanted to read about how K2 is different, where could I do that? I know that ANTLR was at one point part of kotlin, im guessing it isnt anymore. I know a great deal of design was inspired by PSI elements from IntelliJ, I'm guessing alot of this is to move INtelliJ's formalizisms out of the compiler. I also know you guys were flirting with LLVM's own representations (obviously important for kotlin native), So I'm guessing K2 is better front-end-ing for LLVM and back-end-ing for native-js-jvm. But I'm interested in more gory details. I want the moments where some JB kotlin developer was like "OMG this is so much easier now!"
m
It is explained in

this video

, there was some blog post too I think.
166 Views