Skip to content

Make importing cosmology faster #2479

Closed

Description

@nhmc

Currently the import speed of the cosmology package is slow. This matters if you want to import cosmology in a startup script for ipython, for example.

I assume this is because there is a lot of code in cosmology/__init__.py. This can probably be streamlined or deferred to make import times much faster.

Activity

  1. embray commented on May 14, 2014

    @embray
    Member

    Have you confirmed that it's the cosmology package in particular that's slow on import and not, say, just astropy itself? Lots of stuff happens in astropy/__init__.py for example.

    Is it just that initializing the built-in cosmology objects is slow? ISTM Cosmology initiation could be sped up by lazy computing some of the internal values.

  2. nhmc commented on May 15, 2014

    @nhmc
    ContributorAuthor

    Importing astropy is pretty slow, but astropy.cosmology is noticeably slower:

    $ python -m timeit -n 1 -r 1 'import astropy'
    1 loops, best of 1: 245 msec per loop
    
    $ python -m timeit -n 1 -r 1 'import astropy.cosmology'
    1 loops, best of 1: 703 msec per loop
    

    Incidentally if anyone knows a better way to measure import speeds, please let me know.

  3. nhmc commented on May 15, 2014

    @nhmc
    ContributorAuthor

    I just took a look at the code, and my comment in the initial post is wrong - there is hardly any code at all in cosmology/__init__.py. I must be remembering wrongly.

    This means that the problem is likely due to initialising the built-in cosmology objects, as @embray suggests.

  4. embray commented on May 15, 2014

    @embray
    Member

    Which happens in cosmology.core which is imported by cosmology.__init__ so close enough. I would strongly suspect that's it. I definitely thing it could be sped up by using lazy evaluation of the internal attributes set up by Cosmology.__init__.

  5. added this to the v0.4.1 milestone on May 21, 2014
  6. aconley commented on May 21, 2014

    @aconley
    Contributor

    @embray -- do you mean the attributes internal to each cosmology object, or to the cosmology package? The stuff that takes all the time is the initialization of the provided built in cosmologies (WMAP9, Planck13, etc.). And those aren't internal, so being lazy about creating them would take some thought. On the other hand -- the number of built in cosmologies probably only increases with time, so it would be nice to preemptively fight that bloat.

    Incidentally, profiling says that the most expensive part of initializing a cosmology object are all unit related. In particular, these lines are quite expensive:

        H0_s = self._H0.to(1.0 / u.s)
        self._hubble_time = (1. / H0_s).to(u.Gyr)
        self._critical_density0 = (3. * H0_s ** 2 / (8. * pi * const.G.cgs)).cgs
    

    We could certainly speed up the first two -- the definitions of Mpc, km, and s are fixed. For the latter it would be nice to keep the use of the constant library in case a better G measurement comes along. But we could precompute some of the factors for the module rather than on a per-object basis.

    I just tried it, and the initialization time for a cosmology object dropped from 300ms to 3ms. Yeah... I should turn that into a pull no matter what @embray meant about laziness!

  7. aconley commented on May 21, 2014

    @aconley
    Contributor

    More detail: it turns out that calling const.G.cgs is very, very expensive (140ms), accounting for almost half the initialization cost. Perhaps somebody should look into that! Without the .cgs it is blazingly fast (81ns). For now I'll just hardwire the factor of 1000, which assumes that constants is going to stay mks by default.

  8. mdboom commented on May 21, 2014

    @mdboom
    Contributor

    Yeah -- the unit compose method is very slow (called by cgs under the hood). That's a really tricky one to optimize, since there are so many side cases to take into account. However, just caching the result of cgs cut the import time by about a third for me, since the same cgs is computed again and again. I'll have a PR for that shortly.

  9. reopened this on May 21, 2014
  10. aconley commented on May 21, 2014

    @aconley
    Contributor

    Ah, no, don't PR. I'm in the middle of one.

  11. added a commit that references this issue on May 21, 2014
    ddbb3e2
  12. mdboom commented on May 21, 2014

    @mdboom
    Contributor

    I think I'll still want to improve the performance of units independent of this (which is what my PR does -- or hopefully will do if I can get it to work). I don't think it hurts to be optimized on both levels.

  13. aconley commented on May 21, 2014

    @aconley
    Contributor

    Agreed. So... what do I do about the comment in #2542 that it closes this issue? Ignore it and just have you reopen (presuming #2542 is accepted)?

  14. mdboom commented on May 21, 2014

    @mdboom
    Contributor

    That's fine. I think you can also edit the description to change the wording and avoid the automatic closing.

  15. embray commented on May 22, 2014

    @embray
    Member

    I was referring to creation of some of the internal attributes of cosmology objects which are slow. Many of them could just as well be @lazypropertys or something.

  16. aconley commented on May 22, 2014

    @aconley
    Contributor

    Ok, that would be a lot easier. But something like 98% of the init time per object has now been removed, so it's probably not as interesting.

  17. modified the milestones: v0.4.0, v0.4.1 on Jun 1, 2014
  18. added a commit that references this issue on Dec 7, 2020
    1d4f15c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions