Skip to content

Commit a44b626

Browse files
committed
Don't convert pmaps to pmaps on initialization of Registries.
I.e. if it be a pmap, don't be pmapping it -- somewhat surprising pmap doesn't already short-circuit here, but I have vague memories of noticing this before. This gives a ~20% speedup on a microbenchmark (of validator creation in jsonschema which creates new Registries each time) and a ~10% speedup on a jsonschema test run. Thanks to @cfbolz for helping to discover it (though more problems are lurking somewhere to find...).
1 parent ee333fb commit a44b626

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

referencing/_core.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from urllib.parse import unquote, urldefrag, urljoin
66

77
from attrs import evolve, field
8-
from pyrsistent import m, plist, pmap, s
8+
from pyrsistent import plist, pmap, s
99
from pyrsistent.typing import PList, PMap, PSet
1010

1111
from referencing import exceptions
@@ -198,6 +198,10 @@ def _fail_to_retrieve(uri: URI):
198198
raise exceptions.NoSuchResource(ref=uri)
199199

200200

201+
def _to_pmap(mapping: dict[URI, Resource[D]] | PMap[URI, Resource[D]]):
202+
return pmap(mapping) if isinstance(mapping, dict) else mapping
203+
204+
201205
@frozen
202206
class Registry(Mapping[URI, Resource[D]]):
203207
r"""
@@ -222,8 +226,11 @@ class Registry(Mapping[URI, Resource[D]]):
222226
exception indicating that the resource is not retrievable.
223227
"""
224228

225-
_resources: PMap[URI, Resource[D]] = field(default=m(), converter=pmap) # type: ignore[reportUnknownArgumentType] # noqa: E501
226-
_anchors: PMap[tuple[URI, str], AnchorType[D]] = field(default=m()) # type: ignore[reportUnknownArgumentType] # noqa: E501
229+
_resources: PMap[URI, Resource[D]] = field(
230+
default=pmap(), # type: ignore[reportUnknownArgumentType]
231+
converter=_to_pmap,
232+
)
233+
_anchors: PMap[tuple[URI, str], AnchorType[D]] = field(default=pmap()) # type: ignore[reportUnknownArgumentType] # noqa: E501
227234
_uncrawled: PSet[URI] = field(default=s()) # type: ignore[reportUnknownArgumentType] # noqa: E501
228235
_retrieve: Callable[[URI], Resource[D]] = field(default=_fail_to_retrieve)
229236

0 commit comments

Comments
 (0)