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 use of compiled .pyc files to speed-up lambda cold starts and further slim the package size #762

Open
1 task done
hrist0stoichev opened this issue Feb 24, 2023 · 6 comments

Comments

@hrist0stoichev
Copy link

Is there an existing issue for this?

  • I have searched existing issues, it hasn't been reported yet

Use case description

Right now slim: true will remove all .pyc and .pyo files but those are actually more useful than .py files for 2 main reasons:

  1. They are smaller in size as compared to .py files.
  2. They will be generated on cold starts anyways, so having them will make cold-starts faster.

Going even deeper, the -OO flag can be used so that .pyo files are even smaller.

Proposed solution (optional)

On this flow chart you can see how the python lookup-mechanism works.

So, the easiest solution would be to just replace all .py files with their .py[c|o] counterparts coming from __pycache__.

@mattfysh
Copy link

mattfysh commented Oct 6, 2023

Can you solve this by using a custom slim pattern to remove the .py files?

@hrist0stoichev
Copy link
Author

No, it is a 2 step process

  1. Remove .py files
  2. Move .pyc files from __pycache__ to the parent directory.

The 2nd step is currently not achievable.

@mattfysh
Copy link

mattfysh commented Oct 6, 2023

Ah thanks @hrist0stoichev - perhaps there is a way to hook into the packaging, I may also try to provide a custom built artifact. I'm noticing ~3 seconds of START_INIT time on cold boot, and my only direct dependency is prefect - if I managed to get this working, will it remove that time (which I assume was being used to compile?)

@hrist0stoichev
Copy link
Author

I doubt compiling takes that long in your case. You can try creating an import profile with https://github.com/nschloe/tuna and see where you spend the majority of time.

@jameshalsall
Copy link

is there any way, currently, to intercept or hook into the requirements installation to do this in userland?

@jameshalsall
Copy link

Only way I can think of doing this right now is to build + deploy the package in two separate steps, and before deploying unzip the package, modify as needed (generate .pyc files, remove .py files) and then zip up again

🤔

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

No branches or pull requests

3 participants