Skip to content

[enhancement] Synthetic population lazy imports#798

Open
maxbriel wants to merge 2 commits intov2.3from
mb_import_speedup
Open

[enhancement] Synthetic population lazy imports#798
maxbriel wants to merge 2 commits intov2.3from
mb_import_speedup

Conversation

@maxbriel
Copy link
Collaborator

@maxbriel maxbriel commented Feb 9, 2026

importing from the synthetic population file often took longer than a few seconds, when loading for the first time
i.e. from posydon.popsyn.synthetic_population import Population.

This is mostly due to the amount of other parts of the code that are loaded.

This brings the loading down from 1870ms to 737ms
Tested with python -X importtime -c "from posydon.popsyn.synthetic_population import Population"

The imports are still not great. Part of this seems to be how much we import from other parts of posydon.
For example, posydon.popsyn.synthetic_population imports popsyn.binarypopulation, and posydon.binary_evol.binarystar,etc, which is quite a large "time sink". See the attached image for the import time usage.

Focus on the top-left of the below images. These show what is taking the longest to import.

original import time:
importtime

importtime from this rework.
importtime

@maxbriel maxbriel self-assigned this Feb 9, 2026
@maxbriel maxbriel added the enhancement New feature or request label Feb 9, 2026
@maxbriel maxbriel requested a review from a team February 9, 2026 14:57
@maxbriel maxbriel changed the title Synthetic population lazy imports [enhancement] Synthetic population lazy imports Feb 9, 2026
@sgossage
Copy link
Contributor

LGTM except I think @astroJeff had the suggestion to move some of the lighter weight imports like Pwarn back to the top.


# speed of light
c = const.c.to("Mpc/yr").value # Mpc/yr
c = clight * 1.0227121650456949e-17 # cm/s to Mpc/yr
Copy link
Contributor

Choose a reason for hiding this comment

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

Putting in that 1.0227121650456949e-17 factor make for a very hardcoded outlook :|

can we store this as a separate variable at the top instead? like this:

cm_per_sec_to_mpc_per_yr = 1.0227121650456949e-17
c = clight * cm_per_sec_to_mpc_per_yr

raise ValueError("Model weight identifier not provided!")

import posydon.visualization.plot_pop as plot_pop
from posydon.utils.constants import Zsun
Copy link
Contributor

Choose a reason for hiding this comment

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

Zsun seems to be used in a lot of places, we could keep this import at the top?

@astro-abhishek
Copy link
Contributor

@maxbriel since a lot of imports are now lazy, just checking if you’ve seen any new runtime errors?

@sgossage
Copy link
Contributor

I've approved, but I think some of the comments above may still need to be addressed regarding a few imports moved back to the top.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants