perf: cache representative_docs_ to avoid redundant recomputation [stacks on #2496]#2515
Draft
pidefrem wants to merge 1 commit into
Draft
perf: cache representative_docs_ to avoid redundant recomputation [stacks on #2496]#2515pidefrem wants to merge 1 commit into
representative_docs_ to avoid redundant recomputation [stacks on #2496]#2515pidefrem wants to merge 1 commit into
Conversation
representative_docs_ to avoid redundant recomputationrepresentative_docs_ to avoid redundant recomputation [stacks on #2496]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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., afterupdate_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):
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