serverless/serverless-python-requirements

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

hrist0stoichev opened this issue · 6 comments

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__.

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

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.

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?)

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.

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

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

🤔