https://kotlinlang.org logo
Channels
100daysofcode
100daysofkotlin
100daysofkotlin-2021
advent-of-code
aem
ai
alexa
algeria
algolialibraries
amsterdam
android
android-architecture
android-databinding
android-studio
androidgithubprojects
androidthings
androidx
androidx-xprocessing
anime
anko
announcements
apollo-kotlin
appintro
arabic
argentina
arkenv
arksemdevteam
armenia
arrow
arrow-contributors
arrow-meta
ass
atlanta
atm17
atrium
austin
australia
austria
awesome-kotlin
ballast
bangladesh
barcelona
bayarea
bazel
beepiz-libraries
belgium
benchmarks
berlin
big-data
books
boston
brazil
brikk
budapest
build
build-tools
bulgaria
bydgoszcz
cambodia
canada
carrat
carrat-dev
carrat-feed
chicago
chile
china
chucker
cincinnati-user-group
cli
clikt
cloudfoundry
cn
cobalt
code-coverage
codeforces
codemash-precompiler
codereview
codingame
codingconventions
coimbatore
collaborations
colombia
colorado
communities
competitive-programming
competitivecoding
compiler
compose
compose-android
compose-desktop
compose-hiring
compose-ios
compose-mp
compose-ui-showcase
compose-wear
compose-web
confetti
connect-audit-events
corda
cork
coroutines
couchbase
coursera
croatia
cryptography
cscenter-course-2016
cucumber-bdd
cyprus
czech
dagger
data2viz
databinding
datascience
dckotlin
debugging
decompose
decouple
denmark
deprecated
detekt
detekt-hint
dev-core
dfw
docs-revamped
dokka
domain-driven-design
doodle
dsl
dublin
dutch
eap
eclipse
ecuador
edinburgh
education
effective-kotlin
effectivekotlin
emacs
embedded-kotlin
estatik
event21-community-content
events
exposed
failgood
fb-internal-demo
feed
firebase
flow
fluid-libraries
forkhandles
forum
fosdem
fp-in-kotlin
framework-elide
freenode
french
fritz2
fuchsia
functional
funktionale
gamedev
ge-kotlin
general-advice
georgia
geospatial
german-lang
getting-started
github-workflows-kt
glance
godot-kotlin
google-io
gradle
graphic
graphkool
graphql
graphql-kotlin
graviton-browser
greece
grpc
gsoc
gui
hackathons
hacktoberfest
hamburg
hamkrest
helios
helsinki
hexagon
hibernate
hikari-cp
hire-me
hiring
hongkong
hoplite
http4k
hungary
hyderabad
image-processing
india
indonesia
inkremental
intellij
intellij-plugins
intellij-tricks
internships
introduce-yourself
io
ios
iran
israel
istanbulcoders
italian
jackson-kotlin
jadx
japanese
jasync-sql
java-to-kotlin-refactoring
javadevelopers
javafx
javalin
javascript
jdbi
jhipster-kotlin
jobsworldwide
jpa
jshdq
juul-libraries
jvm-ir-backend-feedback
jxadapter
k2-early-adopters
kaal
kafka
kakao
kalasim
kapt
karachi
karg
karlsruhe
kash_shell
kaskade
kbuild
kdbc
kgen-doc-tools
kgraphql
kinta
klaxon
klock
kloudformation
kmdc
kmm-español
kmongo
knbt
knote
koalaql
koans
kobalt
kobweb
kodein
kodex
kohesive
koin
koin-dev
komapper
kondor-json
kong
kontent
kontributors
korau
korean
korge
korim
korio
korlibs
korte
kotest
kotest-contributors
kotless
kotlick
kotlin-asia
kotlin-beam
kotlin-by-example
kotlin-csv
kotlin-data-storage
kotlin-foundation
kotlin-fuel
kotlin-in-action
kotlin-inject
kotlin-latam
kotlin-logging
kotlin-multiplatform-contest
kotlin-mumbai
kotlin-native
kotlin-pakistan
kotlin-plugin
kotlin-pune
kotlin-roadmap
kotlin-samples
kotlin-sap
kotlin-serbia
kotlin-spark
kotlin-szeged
kotlin-website
kotlinacademy
kotlinbot
kotlinconf
kotlindl
kotlinforbeginners
kotlingforbeginners
kotlinlondon
kotlinmad
kotlinprogrammers
kotlinsu
kotlintest
kotlintest-devs
kotlintlv
kotlinultimatechallenge
kotlinx-datetime
kotlinx-files
kotlinx-html
kotrix
kotson
kovenant
kprompt
kraph
krawler
kroto-plus
ksp
ktcc
ktfmt
ktlint
ktor
ktp
kubed
kug-leads
kug-torino
kvision
kweb
lambdaworld_cadiz
lanark
language-evolution
language-proposals
latvia
leakcanary
leedskotlinusergroup
lets-have-fun
libgdx
libkgd
library-development
lincheck
linkeddata
lithuania
london
losangeles
lottie
love
lychee
macedonia
machinelearningbawas
madrid
malaysia
mathematics
meetkotlin
memes
meta
metro-detroit
mexico
miami
micronaut
minnesota
minutest
mirror
mockk
moko
moldova
monsterpuzzle
montreal
moonbean
morocco
motionlayout
mpapt
mu
multiplatform
mumbai
munich
mvikotlin
mvrx
myndocs-oauth2-server
naming
navigation-architecture-component
nepal
new-mexico
new-zealand
newname
nigeria
nodejs
norway
npm-publish
nyc
oceania
ohio-kotlin-users
oldenburg
oolong
opensource
orbit-mvi
osgi
otpisani
package-search
pakistan
panamá
pattern-matching
pbandk
pdx
peru
philippines
phoenix
pinoy
pocketgitclient
polish
popkorn
portugal
practical-functional-programming
proguard
prozis-android-backup
pyhsikal
python
python-contributors
quasar
random
re
react
reaktive
realm
realworldkotlin
reductor
reduks
redux
redux-kotlin
refactoring-to-kotlin
reflect
refreshversions
reports
result
rethink
revolver
rhein-main
rocksdb
romania
room
rpi-pico
rsocket
russian
russian_feed
russian-kotlinasfirst
rx
rxjava
san-diego
science
scotland
scrcast
scrimage
script
scripting
seattle
serialization
server
sg-user-group
singapore
skia-wasm-interop-temp
skrape-it
slovak
snake
sofl-user-group
southafrica
spacemacs
spain
spanish
speaking
spek
spin
splitties
spotify-mobius
spring
spring-security
squarelibraries
stackoverflow
stacks
stayhungrystayfoolish
stdlib
stlouis
strife-discord-lib
strikt
students
stuttgart
sudan
swagger-gradle-codegen
swarm
sweden
swing
swiss-user-group
switzerland
talking-kotlin
tallinn
tampa
teamcity
tegal
tempe
tensorflow
terminal
test
testing
testtestest
texas
tgbotapi
thailand
tornadofx
touchlab-tools
training
tricity-kotlin-user-group
trójmiasto
truth
tunisia
turkey
turkiye
twitter-feed
uae
udacityindia
uk
ukrainian
uniflow
unkonf
uruguay
utah
uuid
vancouver
vankotlin
vertx
videos
vienna
vietnam
vim
vkug
vuejs
web-mpp
webassembly
webrtc
wimix_sentry
wwdc
zircon
Powered by
Title
e

elihart

11/01/2021, 9:29 PM
I am seeing pretty bad performance in KSP compared to KAPT that is fairly concerning (version 1.5.31-1.0.0) I am just about done migrating Epoxy to support KSP via XProcessing (https://github.com/airbnb/epoxy/pull/1244) and took some time to benchmark ksp vs kapt via both gradle build scans and manual timing print statements in the processor. All of our processors now support both ksp and kapt, and I can switch between modes to compare them, but numbers are much worse on KSP for clean builds. Here are processors timings we manually logged for a clean build of one of our larger modules: Paris - 1100ms kapt , 3600ms ksp : 3.27x slower Epoxy - 675ms kapt , 4800ms ksp : 7.1x slower another in house processor - 375ms kapt , 1800ms ksp : 4.8x slower Even with taking kapt stub generation into account KSP does worse, when looking at gradle tasks. ksp mode: - :kspDebugKotlin 18.992s kapt mode: - :kaptDebugKotlin 5.652s - :kaptGenerateStubsDebugKotlin 3.814s I haven’t done any in depth profiling yet to know where this comes from - not sure if it would mainly be xprocessing or KSP itself. Room uses xprocessing though and claimed 2x speed increase so I’m curious if that is still correct or if maybe there was a regression? I believe we used to actually see better performance with KSP a few months ago
not relevant to your case, but one mistake i made was relying on gradle profiler's ABI change which actually didn't change the ABI in a way KAPT would be invalidated.
also, you should definitely count KAPT stub time in your comparisons since that is what makes KAPT clean compilation slow
e

elihart

11/01/2021, 9:40 PM
thanks for the scripts, I’d love to get a flamegraph to dig into where the time is going. I’m doing clean builds with a command on loop like this
./gradlew module:clean module:assembleDebug --no-build-cache
even with stub generation taken into account ksp is twice as slow. but I believe KSP claimed that even without stub generation KSP should be faster because they can do more granular type resolution on demand
y

yigit

11/01/2021, 9:42 PM
Not sure about that because kotlin type system is more complex than Java
The first script i linked will give you a flame graph but to invalidate abi for KAPT, you will need my custom gradle profiler build. I might dig that up
The interesting case i found there was, gradle profiler invalidates abi by addinga public method to a file, which did invalidate KSP but didn't invalidate KAPT as it turns them into Java files hence the original annotated file stays unmodified
e

elihart

11/01/2021, 9:46 PM
shouldn’t matter for clean build profiling though?
y

yigit

11/01/2021, 10:06 PM
nope
let me see if i can find the results
just ran that ksp-kapt comparison script.
totals:
kapt : 53759 ms
ksp : 33144 ms
taskTotals:
kaptWithKaptDebugAndroidTestKotlin : 22754 ms
kaptClasspath_kaptWithKaptDebugKotlin : 0 ms
kaptClasspath_kaptDebugKotlin : 1 ms
kspWithKspDebugAndroidTestKotlin : 32804 ms
kaptAndroidTestWithKapt : 329 ms
kaptGenerateStubsWithKaptDebugAndroidTestKotlin : 30438 ms
kaptAndroidTestDebug : 0 ms
kapt : 1 ms
kaptAndroidTest : 0 ms
kspAndroidTestWithKsp : 319 ms
kaptGenerateStubsWithKaptDebugKotlin : 80 ms
kaptWithKaptDebugKotlin : 41 ms
kaptGenerateStubsDebugKotlin : 50 ms
kspWithKspDebugAndroidTestKotlinProcessorClasspath : 21 ms
kaptDebugKotlin : 37 ms
kaptWithKapt : 0 ms
kaptDebug : 0 ms
kaptWithKaptDebug : 0 ms
kaptAndroidTestWithKaptDebug : 0 ms
kaptClasspath_kaptWithKaptDebugAndroidTestKotlin : 28 ms
`
e

elihart

11/01/2021, 10:25 PM
thanks for checking, good to see Room is still faster. I’m trying to get a flame graph with the gradle profiler it’s also interesting to see that without the stubs task, direct comparison of kapt vs ksp is roughly 50% different so that helps me set expectations for my processors
I got a jprofiler flamegraph working (happy to share it if anyone is interested in the data). xprocessing’s
getDeclaredMethods
takes a significant amount of time, in large part I think because of forces type resolution for this
// if it receives or returns inline, drop it.
                // we can re-enable these once room generates kotlin code
                it.parameters.any {
                    it.type.resolve().isInline()
                } || it.returnType?.resolve()?.isInline() == true
similarly
syntheticGetterSetterMethods
checks type for inline functions. I suppose anywhere that xprocessing currently resolves type should try to be deferred as much as possible. that’s really wasteful for my specific use case. I’m not sure how flexible you want to make the xprocessing api’s - it might be easier to have an escape hatch to expose the ksp resolver so we can do efficient things when we know we’re in ksp (right now I access the resolver reflectively, but it would be nice to have a real api) similarly maybe access to modifiers should be deferred more. for example, getting all methods forces creation of modifiers, when we could probably defer knowing whether its a suspend function
return if (declaration.modifiers.contains(Modifier.SUSPEND)) {
                KspSuspendMethodElement(env, containing, declaration)
            } else {
                KspNormalMethodElement(env, containing, declaration)
            }
Also, accessing rawType seems to not defer creation of type name
constructor(original: KspType) : this(
        ksType = original.ksType.starProjection().makeNotNullable(),
        typeName = original.typeName.rawTypeName()
    )
and that is a big chunk of my flame graph I think addressing those two things should help me a lot actually
In KSP itself there is a lot of time spent in
getSymbolsWithAnnotation -> resolveToUnderlying
which I suppose is to be expected, but would be great if some optimizations could be done eventually
j

Jiaxiang

11/02/2021, 3:22 AM
Thanks for doing performance evaluation, we will address performance issues as part of our plan. Speaking of
getSymbolsWithAnnotation
, the overhead comes from resolving annotation type, we do have some optimizations in place before calling
resolveToUnderlying
, we will look into if we can put more optimization, but my guess is it is most likely marginal since a type resolution is unavoidable, unless we can improve performance for type resolution.
e

elihart

11/02/2021, 5:15 AM
thanks, yes it seems type resolution is mostly needed. I think the largest room for optimization is in xprocessing
t

Ting-Yuan Huang

11/02/2021, 8:17 AM
re: `getSymbolsWithAnnotation -> resolveToUnderlying`: Looks like the optimization (check simple name before resolution) for Java sources is failing. This is a huge performance hit because it basically resolves all annotations in Java sources. We'll fix it in KSP 1.0.1. https://github.com/google/ksp/issues/707
🎉 1
y

yigit

11/02/2021, 2:56 PM
@elihart can you file them as bugs? also, does your code never need those resolutions? Flame graphs might sometimes be misleeading if you eventually read those values.
e

elihart

11/02/2021, 5:54 PM
does your code never need those resolutions?
yes, in many cases we either only need names, or are filtering the methods/fields so it is wasteful to resolve all of them
I’ll file a bug
y

yigit

11/02/2021, 6:45 PM
i guess that lazy resolution wasn't a problem for room because most things we read (pojos, daos) we need them all. that being said, it will probably help room as well as for entities, sometimes they are not value objects so we might be reading more than we need (possibly insignificant though). might be significant for dagger as well
👍 1
@elihart, do you have any of your profiles merged into any of those projects using xprocessing? (wondering if there is an easy way for me to take a look)
profiler scripts*
e

elihart

11/03/2021, 4:56 PM
I ran this on a large module in our airbnb android project. But I did it with the standard gradle profiler with jprofiler -
gradle-profiler --profile jprofiler --jprofiler-config sampling-all --scenario-file profiling.scenarios
With a
profiling.scenarios
file like
default-scenarios = ["profiling"]

profiling {
    title = "Profile ksp"
    tasks = ["kspDebugKotlin"]
    cleanup-tasks = ["clean"]
    gradle-args = ["--no-build-cache", "-Dkotlin.compiler.execution.strategy=in-process"]
    daemon = none
}
I can send you a jprofiler file if you like though
y

yigit

11/03/2021, 5:26 PM
👍 i wanted to run them myself.
actually, if you can share the profiler result with yboyar@google that would be nice. i made some small changes but not resolving them does not seem to help much. maybe it is a different path
also greatly struggling w/ picking a benchmarking library for this so suggestions are welcome there. I need the API similar to androidx.benchmarking that will allow me to stop/start measurements. JMH does not seem to provide that (not very faimilar though)
ok so i did some simple tight look of getting raw types and making it lazy does seem to help based on an async profiler output. don't have proper benchmarks yet so it does feel a bit uncomfortable yet 🙂
but when i try to measure actual time spent, it does not seem to be getting better which does not make much sense, need to dig deeper or better figure out how to setup jmh to run some of these properly
e

elihart

11/03/2021, 6:52 PM
thanks for starting to look at this so quickly. I don’t have a benchmark library to recommend, but I will email you my jprofiler file
y

yigit

11/03/2021, 10:39 PM
https://android-review.googlesource.com/c/platform/frameworks/support/+/1880590/1 this is how i'm measuring it, to be honest relying on profile is not great but that is the best i have for now. https://android-review.googlesource.com/c/platform/frameworks/support/+/1880595/2
👀 1
🎉 1
e

elihart

11/04/2021, 12:03 AM
great start, thanks!