Skip to content

Improve canonicalization performance #3119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 14 additions & 46 deletions rdflib/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,19 +415,10 @@ def _traces(
stats["prunings"] = 0
depth[0] += 1
candidates = self._get_candidates(coloring)
best: list[list[Color]] = []
best_coloring = None
best_score = None
best_experimental_score = None
last_coloring = None
generator: dict[Node, set[Node]] = defaultdict(set)
visited: set[Node] = set()

for candidate, color in candidates:
if candidate in generator:
v = generator[candidate] & visited
if len(v) > 0:
visited.add(candidate)
continue
visited.add(candidate)
coloring_copy: list[Color] = []
color_copy = None
for c in coloring:
Expand All @@ -438,42 +429,19 @@ def _traces(
new_color = self._individuate(color_copy, candidate)
coloring_copy.append(new_color)
refined_coloring = self._refine(coloring_copy, [new_color])
color_score = tuple([c.key() for c in refined_coloring])
experimental = self._experimental_path(coloring_copy)
experimental_score = set([c.key() for c in experimental])
if last_coloring:
generator = self._create_generator( # type: ignore[unreachable]
[last_coloring, experimental], generator
)
last_coloring = experimental
if best_score is None or best_score < color_score: # type: ignore[unreachable]
best = [refined_coloring]
color_score = tuple(c.key() for c in refined_coloring)

if best_score is None or best_score < color_score:
Copy link
Contributor

@edmondchuc edmondchuc May 7, 2025

Choose a reason for hiding this comment

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

The right-hand side of the or is never evaluated as best_score is None is always true here. Can you please review this.

best_coloring = refined_coloring
best_score = color_score
best_experimental_score = experimental_score
elif best_score > color_score: # type: ignore[unreachable]
# prune this branch.
if stats is not None:
stats["prunings"] += 1
elif experimental_score != best_experimental_score:
best.append(refined_coloring)
else:
# prune this branch.
if stats is not None:
stats["prunings"] += 1
discrete: list[list[Color]] = [x for x in best if self._discrete(x)]
if len(discrete) == 0:
best_score = None
best_depth = None
for coloring in best:
d = [depth[0]]
new_color = self._traces(coloring, stats=stats, depth=d)
color_score = tuple([c.key() for c in refined_coloring])
if best_score is None or color_score > best_score: # type: ignore[unreachable]
discrete = [new_color]
best_score = color_score
best_depth = d[0]
depth[0] = best_depth # type: ignore[assignment]
return discrete[0]

if best_coloring is None:
return coloring

if not self._discrete(best_coloring):
return self._traces(best_coloring, stats=stats, depth=depth)

return best_coloring

def canonical_triples(self, stats: Stats | None = None):
if stats is not None:
Expand Down