Repository navigation
Avoid clearing binary and library caches between KSP rounds - #3235
Conversation
In `dropCaches()`, KSP previously called `publishGlobalModuleStateModificationEvent()`, `KaSessionProvider.getInstance(project).clearCaches()`, `psiManager.dropResolveCaches()`, `psiManager.dropPsiCaches()`, and `KSObjectCacheManager.clear()` on each round. KSP rounds only add newly generated source files to `KaSourceModule` and do not mutate binary libraries or dependencies (`KaLibraryModule`). Calling `publishGlobalModuleStateModificationEvent()` forces Analysis API and FIR to discard all binary/library symbol provider and Java class caches, which then have to be recomputed from scratch in every subsequent round. Switching to `publishGlobalSourceModuleStateModificationEvent()` preserves library caches across rounds while properly invalidating source module caches. Additionally: - `KaSessionProvider.getInstance(project).clearCaches()` is redundant because `KaSessionProvider` already subscribes to `KotlinModificationEvent` and clears caches when modification events are published. - `psiManager.dropResolveCaches()` is redundant because `psiManager.dropPsiCaches()` already calls `dropResolveCaches()`.
|
There's some more context including benchmark results in internal bug b/567808367, I am seeing a ~9% wall time improvement for one large KSP action from this. |
jaschdoc
left a comment
There was a problem hiding this comment.
Thanks for the PR. Reducing the number of manual calls is a good idea. Before I approve, I just want to ask @hfmehmed, do you see any problems with not clearing library caches in KMP setups? If we have commonMain and a terminal compilation task, how does KSP run now? Is processing run on commonMain first and its sources and generated sources available to the terminal compilation task as a library?
|
I dont see a kmp specific issue here @jaschdoc - did you have some edge case in mind that you were wondering about? I think the PR is looking good in general. |
|
No, thank you for taking a look. Just wanted your perspective on possible KMP issues :) |
In
dropCaches(), KSP previously calledpublishGlobalModuleStateModificationEvent(),KaSessionProvider.getInstance(project).clearCaches(),psiManager.dropResolveCaches(),psiManager.dropPsiCaches(), andKSObjectCacheManager.clear()on each round.KSP rounds only add newly generated source files to
KaSourceModuleand donot mutate binary libraries or dependencies (
KaLibraryModule). CallingpublishGlobalModuleStateModificationEvent()forces Analysis API and FIRto discard all binary/library symbol provider and Java class caches, which then
have to be recomputed from scratch in every subsequent round.
Switching to
publishGlobalSourceModuleStateModificationEvent()preserveslibrary caches across rounds while properly invalidating source module caches.
Additionally:
KaSessionProvider.getInstance(project).clearCaches()is redundant becauseKaSessionProvideralready subscribes toKotlinModificationEventandclears caches when modification events are published.
psiManager.dropResolveCaches()is redundant becausepsiManager.dropPsiCaches()already callsdropResolveCaches().