Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve start-up time by deferring slow imports. #6

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions configurator/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
from copy import deepcopy
from io import open, StringIO
from os.path import exists, expanduser

Expand All @@ -16,8 +15,7 @@ class Config(ConfigNode):
"""

__slots__ = ('data', '_previous')

parsers = Parsers.from_entrypoints()
parsers = None

def __init__(self, data=None):
super(Config, self).__init__(data)
Expand Down Expand Up @@ -51,6 +49,11 @@ def from_stream(cls, stream, parser=None):
except ValueError:
pass
if not callable(parser):
# Loading the parsers takes a long time (≈200 ms) because it causes
# a lot of code to be imported (`pkg_resources`, `toml`, `yaml`,
# etc). So don't load the parsers until just before we need them.
if cls.parsers is None:
cls.parsers = Parsers.from_entrypoints()
parser = cls.parsers.get(parser)
return cls(parser(stream))

Expand Down Expand Up @@ -156,6 +159,7 @@ def push(self, config=None, empty=False):
if empty:
base = Config()
else:
from copy import deepcopy
base = Config(deepcopy(self.data))
if not isinstance(config, Config):
config = Config(config)
Expand Down
3 changes: 1 addition & 2 deletions configurator/node.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from pprint import pformat


class ConfigNode(object):
"""
Expand Down Expand Up @@ -81,6 +79,7 @@ def __iter__(self):
yield self._wrap(item)

def __repr__(self):
from pprint import pformat
cls = type(self)
pretty = pformat(self.data, width=70)
if '\n' in pretty:
Expand Down
5 changes: 3 additions & 2 deletions configurator/parsers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from pkg_resources import iter_entry_points


class ParseError(Exception):
"""
Expand All @@ -12,6 +10,9 @@ class Parsers(dict):

@classmethod
def from_entrypoints(cls):
# `pkg_resources` is slow to import, so defer until we need it.
from pkg_resources import iter_entry_points
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit ugly. Why is important pkg_resources so slow? Have you reported the issue upstream at https://github.com/pypa/setuptools?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a bit ugly. I don't know why it's so slow, but there are already a couple issues open relating to this: pypa/setuptools#510 pypa/setuptools#926.


parsers = cls()
for entrypoint in iter_entry_points(group='configurator.parser'):
try:
Expand Down