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

Make imports lazy (to speed up asdf extension) #502

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

braingram
Copy link
Collaborator

@braingram braingram commented Jul 5, 2024

The general idea is to speed up the asdf entry point (which is loaded when the asdf extension manager is created). The entrypoint lists gwcs.extensions:

gwcs = "gwcs.extension:get_extensions"

This will trigger the * imports in gwcs.__init__.py:

gwcs/gwcs/__init__.py

Lines 69 to 72 in 53025a9

from .wcs import * # noqa
from .wcstools import * # noqa
from .coordinate_frames import * # noqa
from .selector import * # noqa

which end up importing lots of "heavy hitters" (like scipy, big parts of astropy etc).

Minimal code for testing the entry point is:

from importlib_metadata import entry_points
list(entry_points(group="asdf.extensions", name="gwcs"))[0].load()

To check which modules this import (and ignoring asdf as that's unavoidable):

import asdf
import sys
from importlib_metadata import entry_points
pre_modules = set(sys.modules.keys())
list(entry_points(group="asdf.extensions", name="gwcs"))[0].load()
imported = pre_modules ^ set(sys.modules.keys())

With the current gwcs this imports 747 modules including many parts of scipy and astropy and on my system the entry point loading takes ~600ms.

With this PR the same code imports 90 modules with none from scipy and takes ~250ms. The remaining imports are as follows:

Click to expand
{'asdf._core',
 'asdf._core._integration',
 'asdf_astropy',
 'asdf_astropy._astropy_init',
 'asdf_astropy._version',
 'asdf_astropy.converters',
 'asdf_astropy.converters.coordinates',
 'asdf_astropy.converters.coordinates.angle',
 'asdf_astropy.converters.coordinates.earth_location',
 'asdf_astropy.converters.coordinates.frame',
 'asdf_astropy.converters.coordinates.representation',
 'asdf_astropy.converters.coordinates.sky_coord',
 'asdf_astropy.converters.coordinates.spectral_coord',
 'asdf_astropy.converters.fits',
 'asdf_astropy.converters.fits.fits',
 'asdf_astropy.converters.helpers',
 'asdf_astropy.converters.table',
 'asdf_astropy.converters.table.table',
 'asdf_astropy.converters.time',
 'asdf_astropy.converters.time.time',
 'asdf_astropy.converters.time.time_delta',
 'asdf_astropy.converters.transform',
 'asdf_astropy.converters.transform.compound',
 'asdf_astropy.converters.transform.core',
 'asdf_astropy.converters.transform.functional_models',
 'asdf_astropy.converters.transform.mappings',
 'asdf_astropy.converters.transform.math_functions',
 'asdf_astropy.converters.transform.polynomial',
 'asdf_astropy.converters.transform.projections',
 'asdf_astropy.converters.transform.properties',
 'asdf_astropy.converters.transform.rotations',
 'asdf_astropy.converters.transform.spline',
 'asdf_astropy.converters.transform.tabular',
 'asdf_astropy.converters.unit',
 'asdf_astropy.converters.unit.equivalency',
 'asdf_astropy.converters.unit.magunit',
 'asdf_astropy.converters.unit.quantity',
 'asdf_astropy.converters.unit.unit',
 'asdf_astropy.converters.utils',
 'asdf_astropy.integration',
 'asdf_astropy.resources',
 'asdf_coordinates_schemas',
 'asdf_coordinates_schemas._version',
 'asdf_coordinates_schemas.integration',
 'asdf_standard.integration',
 'asdf_transform_schemas',
 'asdf_transform_schemas._version',
 'asdf_transform_schemas.integration',
 'asdf_unit_schemas',
 'asdf_unit_schemas._version',
 'asdf_unit_schemas.integration',
 'asdf_wcs_schemas',
 'asdf_wcs_schemas._version',
 'asdf_wcs_schemas.integration',
 'asdf_wcs_schemas.resources',
 'astropy',
 'astropy._version',
 'astropy.config',
 'astropy.config.configuration',
 'astropy.config.paths',
 'astropy.extern',
 'astropy.extern.configobj',
 'astropy.extern.configobj.configobj',
 'astropy.extern.configobj.validate',
 'astropy.logger',
 'astropy.tests',
 'astropy.tests.runner',
 'astropy.utils',
 'astropy.utils._compiler',
 'astropy.utils.codegen',
 'astropy.utils.compat',
 'astropy.utils.compat.misc',
 'astropy.utils.compat.numpycompat',
 'astropy.utils.decorators',
 'astropy.utils.exceptions',
 'astropy.utils.introspection',
 'astropy.utils.misc',
 'astropy.utils.shapes',
 'astropy.utils.state',
 'astropy.version',
 'difflib',
 'glob',
 'gwcs',
 'gwcs.converters',
 'gwcs.converters.geometry',
 'gwcs.converters.selector',
 'gwcs.converters.spectroscopy',
 'gwcs.converters.wcs',
 'gwcs.extension',
 'shlex'}

Copy link

codecov bot commented Jul 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.27%. Comparing base (eb9d316) to head (6b5c599).
Report is 6 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #502      +/-   ##
==========================================
- Coverage   87.28%   87.27%   -0.01%     
==========================================
  Files          22       21       -1     
  Lines        3821     3820       -1     
==========================================
- Hits         3335     3334       -1     
  Misses        486      486              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@braingram braingram changed the title TST: make import lazy for asdf extension Make imports lazy (to speed up asdf extension) Jul 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant