Closed ZLLentz closed 2 years ago
One more thought before I go make my one-line PR: does importing a submodule in __init__.py
do anything functionally either than forcing the on-import code to run? I understand that importing resources from a module into init gives us some new top-level imports, but importing a module into init seems to just... create a duplicate route to the same namespace? Am I missing something here?
It was for convenience, as the following won't work without that submodule import:
import transfocate
transfocate.table.x
Instead
import transfocate.table
transfocate.table.x
Ok, that makes a lot of sense and I'll keep it in mind for future cases. I've double-checked that in this case it isn't used within the transfocate module in the current implementation- it must have been used in a previous iteration.
I've been trying to track down and eliminate startup slowness in our python environments. This
transfocate
module has a slow import so I wanted to break it down and see if there was anything we can do to fix it.First, the top of the current
happi benchmark --sort_key import_time
output:Okay, so it takes >4s to import this module when we create the mfx transfocator. Let's break down why this happens.
From other projects I know that we can't really fix the pkg_resources import. Some of these imports are red herrings because importing a dev package is slow, but we can't avoid most of them anyway. There are two things in this output that seem easily fixable to me:
table
submodule, and is only done when loading the transfocator device because of this line in the top-level__init__
: https://github.com/pcdshub/transfocate/blob/7d6f7ba3d6fadb09114c63da39a80325a625980a/transfocate/__init__.py#L6transfocate.table.info
is slow because it reads and parses a spreadsheet on import. This could be adjusted to only run when first needed, but it also could be avoided with the same fix as point 1.table
submoduleThe
table
module is used when running the automated checkout to generate nice reports and is valuable, but there is no need to import it when running the transfocator in a hutch python session.So I think this means that I made a giant issue to suggest a 1-line edit to remove a single top-level import... I'll do this myself after lunch and after the after lunch meetings pending other thoughts.