Skip to content

Commit e0ac1e0

Browse files
committed
Short-circuit when combining a registry with itself.
This is useful when combining some base registry with further resources -- e.g. in jsonschema we do JSONSCHEMA_SPECS.combine(...) and if no additional resources are provided that's meant to be a no-op. Doing this seems to be a huge speed-up, taking the test run from ~25s to 9s (though even that is very slow compared to the non-compliant implementation, so some performance optimization will come regardless). But as a micro-optimization, this seems definitely useful.
1 parent 09b273f commit e0ac1e0

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

referencing/_core.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,8 @@ def combine(self, *registries: Registry[D]) -> Registry[D]:
339339
"""
340340
Combine together one or more other registries, producing a unified one.
341341
"""
342+
if registries == (self,):
343+
return self
342344
anchors = self._anchors
343345
resources = self._resources
344346
uncrawled = self._uncrawled

referencing/tests/test_core.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,18 @@ def test_combine(self):
218218
),
219219
).with_resource("http://example.com/foo/bar", two)
220220

221+
def test_combine_self(self):
222+
"""
223+
Combining a registry with itself short-circuits.
224+
225+
This is a performance optimization -- otherwise we do lots more work
226+
(in jsonschema this seems to correspond to making the test suite take
227+
*3x* longer).
228+
"""
229+
230+
registry = Registry({"urn:foo": "bar"})
231+
assert registry.combine(registry) is registry
232+
221233
def test_combine_with_uncrawled_resources(self):
222234
one = Resource.opaque(contents={})
223235
two = ID_AND_CHILDREN.create_resource({"foo": "bar"})

0 commit comments

Comments
 (0)