Repository navigation
perf: skip quadratic curies duplicate check for valid contexts - #85
Merged
Merged
Conversation
Context.as_converter() let curies run its strict duplicate check, which
compares every pair of records. For each merged context (4,273
records) that takes 15-25 seconds, so load_converter("merged") is slow
for every downstream user (see linkml#75 and monarch-app#819).
Scan the extended prefix map for shared prefixes or URI prefixes in
linear time, and build the converter with strict=False when there are
none. When duplicates do exist, keep strict=True so curies still raises
DuplicatePrefixes or DuplicateURIPrefixes with its usual message.
load_converter("merged") now takes about 0.3 seconds, and converters
for all bundled contexts build in about one second in total.
Add tests that every bundled context builds a converter, and that each
kind of shared prefix or URI prefix (canonical or synonym) is still
rejected.
Fixes linkml#75
Signed-off-by: jdsika <carlo.van-driesten@vdl.digital>
The integrity tests skipped merged, merged.monarch and merged.oak entirely. Their canonical expansions are bijective, so run the two canonical-uniqueness tests on every bundled context. The alias tests still skip the merged contexts, because those still contain aliases without a matching canonical record (linkml#26). Follow-up to the discussion in linkml#11. Signed-off-by: jdsika <carlo.van-driesten@vdl.digital>
The examples built converters with Converter.from_extended_prefix_map(context.as_extended_prefix_map()), which runs the slow strict duplicate check. Use as_converter() instead. This also fixes examples that assigned ctxt but read context, one line repeated three times, and the missing load_context import. Signed-off-by: jdsika <carlo.van-driesten@vdl.digital>
Contributor
Author
|
The red check is not caused by this change.
I'll rebase this branch once #84 is merged so CI goes fully green. |
kevinschaper
approved these changes
Nov 6, 2026
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.
Fixes #75. Also relevant to monarch-initiative/monarch-app#819 (slow CLI startup), since Monarch calls
load_converter("merged.monarch").Problem
Context.as_converter()relied on curies' defaultstrict=True. That check (_get_duplicate_prefixes/_get_duplicate_uri_prefixes) compares every pair of records, so its cost grows quadratically. For each merged context (4,273 records) it takes 15–25 s, so everyload_converter("merged")pays that on startup.Change
perf:Add_has_duplicates(), a linear scan for prefixes or URI prefixes (canonical or synonym) shared by two records.strict=False.strict=Trueis kept, so curies raises its usualDuplicatePrefixes/DuplicateURIPrefixeswith the same message as before. The public API is unchanged.test:Run the two canonical-uniqueness integrity tests onmerged,merged.monarchandmerged.oaktoo (they pass). The alias tests still skip the merged contexts (Add tests for integrity of merged contexts #26). This is the follow-up offered in Contexts don't guarantee bi-directional mapping #11.docs:docs/intro.mdbuilt converters withConverter.from_extended_prefix_map(context.as_extended_prefix_map()), which is the slow strict path. It now usesctxt.as_converter(). This also fixes examples that assignedctxtbut readcontext, and one line repeated three times.Results
load_converter("merged")Verification
An independent adversarial review checked equivalence with curies' strict check:
strictonly ever controls these two exact-match checks, andfrom_extended_prefix_mappassesstrict=through in every version.prefix_map,reverse_prefix_map,pattern_map, and 20k expand/compress probes onmerged.flake8with thetox -e flake8plugin set passes on Python 3.11.Notes
strict=True, they would be skipped for contexts that pass the linear scan. A linear duplicate check upstream in curies would be the long-term fix, and we're happy to propose one there.docs/intro.mdwere already stale onmainand are left unchanged here:FlyBase:FBgn123no longer expands in the first example.GEOGEO:1now expands in the last two examples.These are data changes, not caused by this PR, since the converters are identical.