Skip to content

perf: cache representative_docs_ to avoid redundant recomputation [stacks on #2496]#2515

Draft
pidefrem wants to merge 1 commit into
MaartenGr:masterfrom
pidefrem:pr05-cache-repr-docs
Draft

perf: cache representative_docs_ to avoid redundant recomputation [stacks on #2496]#2515
pidefrem wants to merge 1 commit into
MaartenGr:masterfrom
pidefrem:pr05-cache-repr-docs

Conversation

@pidefrem

@pidefrem pidefrem commented Jul 8, 2026

Copy link
Copy Markdown

What does this PR do?

perf: cache representative_docs_ to avoid redundant recomputation

_extract_representative_docs() runs the full computation pipeline (sampling → c-TF-IDF → cosine similarity → MMR selection) every time it is invoked. In interactive workflows, multiple visualization calls each trigger this computation even though topic assignments haven't changed.

This is the root cause behind #2367 — KeyBERTInspired recomputes representative docs embeddings even when precomputed embeddings are available.

Changes:

Cache representative_docs_ after first computation. Invalidate the cache when topic assignments change (i.e., after update_topics(), reduce_outliers(), merge_topics()). Subsequent calls return the cached result without recomputation.

This follows the same pattern BERTopic already uses for topic_embeddings_ — compute once, store, invalidate on change.

Benchmark (to be filled in before submission):

Workflow (N repeated accesses) Time before (no cache) Time after (cached) Speedup
20× representative-docs access, ~3k docs (TODO) (TODO) (TODO)

First call costs the same; every subsequent access in an unchanged model is served from cache. The win is in interactive/dashboard workflows that re-trigger the computation repeatedly.

Fixes #2499

Recommended merge order: Best reviewed after #2496 (dedup fix) and #2496 (indexing fix) for _extract_representative_docs, so the cache stores correct results. The caching mechanism itself is independent.

Before submitting

  • This PR fixes a typo or improves the docs (if yes, ignore all other checks!).
  • Did you read the contributor guideline?
  • Was this discussed/approved via a Github issue? Please add a link to it if that's the case.
  • Did you make sure to update the documentation with your changes (if applicable)?
  • Did you write any new necessary tests?

⚠️ Draft — stacks on #2496. This branch includes the _extract_representative_docs fix commits from #2496 as a prerequisite (marked [prereq]) so the cache stores correct results; the diff here shows that work too. Once #2496 merges, I'll rebase onto master and the prereq commits drop out, leaving only the caching change. Marking ready for review after #2496 lands.

@pidefrem pidefrem changed the title perf: cache representative_docs_ to avoid redundant recomputation perf: cache representative_docs_ to avoid redundant recomputation [stacks on #2496] Jul 8, 2026
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.

_extract_representative_docs recomputes from scratch on every call — no caching

2 participants