Skip to content

safe_load with implicit_resolver slows down  #439

@AlexeyTrekin

Description

@AlexeyTrekin

Every time safe_load is called, it turns out to be slower than previous one, if implicit resolver is used.
Without implicit_resolvers the safe_load() works the same time every time.

pyyaml versions: '5.1.2', '5.3.1'
python 3.6.5, ubuntu 18.04

My output (code below, test data attached):

Run 1. 100 tries: 0.18349623680114746
Run 2. 100 tries: 0.2797386646270752
Run 3. 100 tries: 0.3783113956451416
Run 4. 100 tries: 0.47731661796569824
Run 5. 100 tries: 0.5768036842346191
Run 6. 100 tries: 0.6758952140808105
Run 7. 100 tries: 0.7751777172088623
Run 8. 100 tries: 0.8736646175384521
Run 9. 100 tries: 0.9729092121124268
Run 10. 100 tries: 1.0713348388671875

from time import time
import re
import yaml
        

tag= ""
pattern = re.compile('.*?\${(\w+)}.*?')

yaml.SafeLoader.add_implicit_resolver(tag, pattern, None)

path = './sample.txt'

for run in range(10):
    t0 = time()
    for _ in range(100):
        with open(path) as conf_data:
            res = yaml.safe_load(conf_data)

    t1 = time()
    print(f'Run {run+1}. 100 tries: {t1-t0}')

sample.txt

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions