serverless / serverless-python-requirements

⚡️🐍📦 Serverless plugin to bundle Python packages
MIT License
1.11k stars 291 forks source link

Make use of compiled .pyc files to speed-up lambda cold starts and further slim the package size #762

Open hrist0stoichev opened 1 year ago

hrist0stoichev commented 1 year ago

Is there an existing issue for this?

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 commented 1 year ago

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

hrist0stoichev commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 6 months ago

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

jameshalsall commented 6 months ago

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

🤔