diff options
author | Phil Sphicas <pgs2111@columbia.edu> | 2020-09-18 00:29:02 -0700 |
---|---|---|
committer | Ingy döt Net <ingy@ingy.net> | 2021-01-13 16:58:40 -0500 |
commit | fc914d52c43f499224f7fb4c2d4c47623adc5b33 (patch) | |
tree | 212ead62a34af237998feb44c454292e492869f6 | |
parent | a001f2782501ad2d24986959f0239a354675f9dc (diff) | |
download | pyyaml-git-fc914d52c43f499224f7fb4c2d4c47623adc5b33.tar.gz |
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
-rw-r--r-- | lib/yaml/resolver.py | 4 | ||||
-rw-r--r-- | lib3/yaml/resolver.py | 4 |
2 files changed, 4 insertions, 4 deletions
diff --git a/lib/yaml/resolver.py b/lib/yaml/resolver.py index 528fbc0..ba9aeab 100644 --- a/lib/yaml/resolver.py +++ b/lib/yaml/resolver.py @@ -146,8 +146,8 @@ class BaseResolver(object): resolvers = self.yaml_implicit_resolvers.get(u'', []) else: resolvers = self.yaml_implicit_resolvers.get(value[0], []) - resolvers += self.yaml_implicit_resolvers.get(None, []) - for tag, regexp in resolvers: + wildcard_resolvers = self.yaml_implicit_resolvers.get(None, []) + for tag, regexp in resolvers + wildcard_resolvers: if regexp.match(value): return tag implicit = implicit[1] diff --git a/lib3/yaml/resolver.py b/lib3/yaml/resolver.py index 02b82e7..013896d 100644 --- a/lib3/yaml/resolver.py +++ b/lib3/yaml/resolver.py @@ -146,8 +146,8 @@ class BaseResolver: resolvers = self.yaml_implicit_resolvers.get('', []) else: resolvers = self.yaml_implicit_resolvers.get(value[0], []) - resolvers += self.yaml_implicit_resolvers.get(None, []) - for tag, regexp in resolvers: + wildcard_resolvers = self.yaml_implicit_resolvers.get(None, []) + for tag, regexp in resolvers + wildcard_resolvers: if regexp.match(value): return tag implicit = implicit[1] |