From 8c921e6019328bbaf5ffb360d5e17843c6e22796 Mon Sep 17 00:00:00 2001 From: Afloat Date: Tue, 17 Nov 2026 10:30:24 +0800 Subject: [PATCH 1/2] perf(tier2): store clustering similarities in packed doubles Signed-off-by: Afloat --- CHANGELOG.md | 2 ++ .../intra_skill/semantic_clustering.py | 9 ++++++--- .../intra_skill/test_semantic_clustering.py | 17 +++++++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f3a4fafc6..1343e5c44 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -438,6 +438,8 @@ All notable changes to SkillEvaluator are documented in this file. ### Fixed +- Tier 2 content clustering keeps pairwise similarities in packed doubles instead of + per-pair dictionary entries, reducing memory use for large skills without changing cluster scores. - Direct Tier 1 validates the target, selected checks and LLM configuration before preparing scanners, so configuration errors do not trigger downloads or stdout noise. - Documentation-only CI follows imports through collected test modules and diff --git a/src/skillevaluator/deduplication/intra_skill/semantic_clustering.py b/src/skillevaluator/deduplication/intra_skill/semantic_clustering.py index 184a3885d..d4c9d05e6 100644 --- a/src/skillevaluator/deduplication/intra_skill/semantic_clustering.py +++ b/src/skillevaluator/deduplication/intra_skill/semantic_clustering.py @@ -9,6 +9,7 @@ from __future__ import annotations +from array import array from dataclasses import dataclass, field, replace from itertools import combinations from typing import TYPE_CHECKING @@ -108,11 +109,13 @@ def siblings(i: int, j: int) -> bool: return group is not None and group == chunks[j].split_group uf = UnionFind(n) - pair_scores: dict[tuple[int, int], float] = {} + # Row i contains the upper-triangle scores (i, i + 1), ..., (i, n - 1). + # Packed doubles avoid a tuple key and boxed float for every pair. + pair_scores = [array("d") for _ in range(n - 1)] for i, j in combinations(range(n), 2): score = unit_vector_similarity(units[i], units[j]) - pair_scores[(i, j)] = score + pair_scores[i].append(score) if score >= threshold and not siblings(i, j): uf.union(i, j) @@ -122,7 +125,7 @@ def siblings(i: int, j: int) -> bool: continue members = _merge_split_pieces([chunks[i] for i in indices]) - scores = [pair_scores[(i, j)] for i, j in combinations(indices, 2) if not siblings(i, j)] + scores = array("d", (pair_scores[i][j - i - 1] for i, j in combinations(indices, 2) if not siblings(i, j))) clusters.append( ContentCluster( diff --git a/tests/deduplication/intra_skill/test_semantic_clustering.py b/tests/deduplication/intra_skill/test_semantic_clustering.py index d4851ecac..3db9289cc 100644 --- a/tests/deduplication/intra_skill/test_semantic_clustering.py +++ b/tests/deduplication/intra_skill/test_semantic_clustering.py @@ -55,6 +55,23 @@ def test_path_compression(self) -> None: class TestBuildClusters: + def test_separated_cluster_members_keep_their_pair_scores(self, make_chunk) -> None: + chunks = [ + make_chunk(source_file=f"{index}.md", embedding=vector) + for index, vector in enumerate( + [[1.0, 0.0, 0.0], [0.0, 0.0, 1.0], [0.0, -1.0, 0.0], [0.0, 0.6, 0.8], [0.96, 0.28, 0.0]] + ) + ] + + clusters = build_clusters(chunks, threshold=0.75) + + assert [[member.source_file for member in cluster.members] for cluster in clusters] == [ + ["0.md", "4.md"], + ["1.md", "3.md"], + ] + assert [cluster.max_similarity for cluster in clusters] == pytest.approx([0.96, 0.8]) + assert [cluster.avg_similarity for cluster in clusters] == pytest.approx([0.96, 0.8]) + def test_fewer_than_2_chunks_returns_empty(self, make_chunk) -> None: assert build_clusters([make_chunk(embedding=[1.0, 0.0])]) == [] assert build_clusters([]) == [] From e6a6e8e2bbf55cf8183009e8d88f1df8433e557a Mon Sep 17 00:00:00 2001 From: Afloat Date: Tue, 17 Nov 2026 11:42:45 +0800 Subject: [PATCH 2/2] docs: describe clustering memory savings for users Signed-off-by: Afloat --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b373a0e1..c5ff5499e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -443,8 +443,8 @@ All notable changes to SkillEvaluator are documented in this file. ### Fixed -- Tier 2 content clustering keeps pairwise similarities in packed doubles instead of - per-pair dictionary entries, reducing memory use for large skills without changing cluster scores. +- Tier 2 content clustering uses much less memory on skills with many chunks; + similarity scores and clusters are unchanged. - Direct Tier 1 validates the target, selected checks and LLM configuration before preparing scanners, so configuration errors do not trigger downloads or stdout noise. - Documentation-only CI follows imports through collected test modules and