Skip to content

Avoid clearing binary and library caches between KSP rounds - #3235

Merged
jaschdoc merged 1 commit into
google:mainfrom
cushon:optimize-ksp-cache-eviction
Oct 7, 2026
Merged

jaschdoc merged 1 commit into
google:mainfrom
cushon:optimize-ksp-cache-eviction

Conversation

@cushon

@cushon cushon commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

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().

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()`.
@cushon

cushon commented Sep 30, 2026

Copy link
Copy Markdown
Contributor Author

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 jaschdoc left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@hfmehmed

hfmehmed commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator

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.

@jaschdoc

jaschdoc commented Oct 7, 2026 •

Copy link
Copy Markdown
Collaborator

No, thank you for taking a look. Just wanted your perspective on possible KMP issues :)

@jaschdoc
jaschdoc added this pull request to the merge queue Oct 7, 2026
Merged via the queue into google:main with commit 958d32f Oct 7, 2026
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants