You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As I've been building Kybra I've come across the need to include the entire frozen stdlib, along with freezing all of the developer's Python files and dependencies. When using the frozen stdlib, the resulting binary is far too large (~30 mb) to be deployed to our environment. I suggest RustPython look into ways to eliminate Wasm binary size bloat in all of its forms to optimize the final Wasm binary output, especially when freezing modules.
Detailed Explanation
As I've been building Kybra (a Python environment for a decentralized cloud), I've run into issues with the final Wasm binary produced when including the stdlib and the frozen stdlib. The Wasm environment we are deploying to has very strict requirements for Wasm binary size, which right now is ~2mb. That limit will hopefully be lifted over time. But right now RustPython will create Wasm binaries up to ~30mb in size unoptimized, and even optimizing and compressing with gzip results in binaries far too large to deploy to our environment.
I suggest RustPython implement means to reduce the size of the final binary produced when using the freezing features. This could be done in a number of ways, including module bundling, tree shaking, dead code elimination, and perhaps other ways as well.
Module bundling
For module bundling, the idea is to follow the statically defined Python imports from an entry file, and create an output directory that only includes the exact files/folders referenced from the imports. RustPython can then freeze this directory. It would be desirable for RustPython to do this type of module bundling automatically when freezing, only following the imports as statically defined.
The JS ecosystem builders/bundlers make heavy use of this kind of static analysis to create JS bundles.
This will obviously take some work and possibly add complexity to RustPython. Also it isn't common for the Python community to attempt to optimize in such ways, apparent from the fact that there is not mature tooling to achieve concise bundles like in the JS community. Nevertheless, my project (until our environment increases the Wasm binary limit) and I would hope many others would greatly benefit from reduced Wasm binary sizes.
It is possible that the environment we are deploying to, the Internet Computer, increases the Wasm binary size limit. I do not know when that will happen or how high the limit will be. But even in that case, the compile time when including the frozen stdlib (even sometimes just one import using the module bundler that I created) is quite long.
Unresolved Questions
I am not sure how effective my suggestions will be and which path RustPython would like to take, but I hope that some combination of module bundling, tree shaking, dead code elimination, or something else will allow us to produce concise Wasm binaries with frozen Python modules.
The text was updated successfully, but these errors were encountered:
So I've got some data on binary sizes to share. I've tested out just a couple examples. What's shown below is the before and after binary size when adding vm.add_frozen(rustpython_pylib::frozen_stdlib()); inside rustpython_vm::Interpreter::with_init.
The conclusion from this simple experiment shows an increase of ~11.5 MiB. This is far too large for our environment currently, the maximum we can allow is 10 MiB.
Uh oh!
There was an error while loading. Please reload this page.
Summary
As I've been building Kybra I've come across the need to include the entire frozen stdlib, along with freezing all of the developer's Python files and dependencies. When using the frozen stdlib, the resulting binary is far too large (~30 mb) to be deployed to our environment. I suggest RustPython look into ways to eliminate Wasm binary size bloat in all of its forms to optimize the final Wasm binary output, especially when freezing modules.
Detailed Explanation
As I've been building Kybra (a Python environment for a decentralized cloud), I've run into issues with the final Wasm binary produced when including the
stdlib
and thefrozen stdlib
. The Wasm environment we are deploying to has very strict requirements for Wasm binary size, which right now is ~2mb. That limit will hopefully be lifted over time. But right now RustPython will create Wasm binaries up to ~30mb in size unoptimized, and even optimizing and compressing with gzip results in binaries far too large to deploy to our environment.I suggest RustPython implement means to reduce the size of the final binary produced when using the freezing features. This could be done in a number of ways, including module bundling, tree shaking, dead code elimination, and perhaps other ways as well.
Module bundling
For module bundling, the idea is to follow the statically defined Python imports from an entry file, and create an output directory that only includes the exact files/folders referenced from the imports. RustPython can then freeze this directory. It would be desirable for RustPython to do this type of module bundling automatically when freezing, only following the imports as statically defined.
The JS ecosystem builders/bundlers make heavy use of this kind of static analysis to create JS bundles.
Some possibly useful projects:
Some rough code that has been working locally to start the bundling process:
Tree shaking
The idea is to remove unnecessary imports. Some imports may be statically defined, but never actually used.
Some possibly useful projects:
Dead code elimination
The idea is to remove unnecessary code.
Some possibly useful projects:
Drawbacks, Rationale, and Alternatives
This will obviously take some work and possibly add complexity to RustPython. Also it isn't common for the Python community to attempt to optimize in such ways, apparent from the fact that there is not mature tooling to achieve concise bundles like in the JS community. Nevertheless, my project (until our environment increases the Wasm binary limit) and I would hope many others would greatly benefit from reduced Wasm binary sizes.
It is possible that the environment we are deploying to, the Internet Computer, increases the Wasm binary size limit. I do not know when that will happen or how high the limit will be. But even in that case, the compile time when including the frozen stdlib (even sometimes just one import using the module bundler that I created) is quite long.
Unresolved Questions
I am not sure how effective my suggestions will be and which path RustPython would like to take, but I hope that some combination of module bundling, tree shaking, dead code elimination, or something else will allow us to produce concise Wasm binaries with frozen Python modules.
The text was updated successfully, but these errors were encountered: