Skip to content

7.x canonicalization perf #3135

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

Draft
wants to merge 1 commit into
base: 7-maintenance
Choose a base branch
from
Draft
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
62 changes: 15 additions & 47 deletions rdflib/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,20 +424,11 @@ 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] = []
coloring_copy: list[Color] = []
color_copy = None
for c in coloring:
c_copy = c.copy()
Expand All @@ -447,42 +438,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:
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: Optional[Stats] = None):
if stats is not None:
Expand Down
Loading