Skip to content

Commit fc914d5

Browse files
psphicasingydotnet
authored andcommitted
Avoid repeatedly appending to yaml_implicit_resolvers
Repeated calls to `resolve` can experience performance degredation, if `add_implicit_resolver` has been called with `first=None` (to add an implicit resolver with an unspecified first character). For example, every time `foo` is encountered, the "wildcard implicit resolvers" (with `first=None`) will be appended to the list of implicit resolvers for strings starting with `f`, which will normally be the resolver for booleans. The list `yaml_implicit_resolvers['f']` will keep getting longer. The same behavior applies for any first-letter matches with existing implicit resolvers. This change avoids unintentionally mutating the lists in the class-level dict `yaml_implicit_resolvers` by looping through a temporary copy. Fixes: #439
1 parent a001f27 commit fc914d5

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

lib/yaml/resolver.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ def resolve(self, kind, value, implicit):
146146
resolvers = self.yaml_implicit_resolvers.get(u'', [])
147147
else:
148148
resolvers = self.yaml_implicit_resolvers.get(value[0], [])
149-
resolvers += self.yaml_implicit_resolvers.get(None, [])
150-
for tag, regexp in resolvers:
149+
wildcard_resolvers = self.yaml_implicit_resolvers.get(None, [])
150+
for tag, regexp in resolvers + wildcard_resolvers:
151151
if regexp.match(value):
152152
return tag
153153
implicit = implicit[1]

lib3/yaml/resolver.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ def resolve(self, kind, value, implicit):
146146
resolvers = self.yaml_implicit_resolvers.get('', [])
147147
else:
148148
resolvers = self.yaml_implicit_resolvers.get(value[0], [])
149-
resolvers += self.yaml_implicit_resolvers.get(None, [])
150-
for tag, regexp in resolvers:
149+
wildcard_resolvers = self.yaml_implicit_resolvers.get(None, [])
150+
for tag, regexp in resolvers + wildcard_resolvers:
151151
if regexp.match(value):
152152
return tag
153153
implicit = implicit[1]

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy