Skip to content

Commit 330cd39

Browse files
committed
Use a larger initial empty pmap for registries.
Cuts runtime on the validator creation benchmark in half, as otherwise we get lots of reallocations when initially building up a registry.
1 parent 66b6ff7 commit 330cd39

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

referencing/_core.py

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

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

1111
from referencing import exceptions
1212
from referencing._attrs import frozen
1313
from referencing.typing import URI, Anchor as AnchorType, D, Mapping
1414

15+
EMPTY64 = pmap(pre_size=64)
16+
1517

1618
class _MaybeInSubresource(Protocol[D]):
1719
def __call__(
@@ -231,11 +233,11 @@ class Registry(Mapping[URI, Resource[D]]):
231233
"""
232234

233235
_resources: PMap[URI, Resource[D]] = field(
234-
default=pmap(), # type: ignore[reportUnknownArgumentType]
236+
default=EMPTY64, # type: ignore[reportUnknownArgumentType]
235237
converter=_to_pmap,
236238
)
237-
_anchors: PMap[tuple[URI, str], AnchorType[D]] = field(default=pmap()) # type: ignore[reportUnknownArgumentType] # noqa: E501
238-
_uncrawled: PSet[URI] = field(default=s()) # type: ignore[reportUnknownArgumentType] # noqa: E501
239+
_anchors: PMap[tuple[URI, str], AnchorType[D]] = field(default=EMPTY64) # type: ignore[reportUnknownArgumentType] # noqa: E501
240+
_uncrawled: PSet[URI] = field(default=pset()) # type: ignore[reportUnknownArgumentType] # noqa: E501
239241
_retrieve: Callable[[URI], Resource[D]] = field(default=_fail_to_retrieve)
240242

241243
def __getitem__(self, uri: URI) -> Resource[D]:
@@ -326,7 +328,7 @@ def crawl(self) -> Registry[D]:
326328
self,
327329
resources=resources.persistent(),
328330
anchors=anchors.persistent(),
329-
uncrawled=s(),
331+
uncrawled=pset(),
330332
)
331333

332334
def with_resource(self, uri: URI, resource: Resource[D]):

0 commit comments

Comments
 (0)