Replies: 4 comments 14 replies
-
Thanks for your measurements. I think you should instead :
Also one good thing with pre-transcription is that you don't need to whole Brython.js to run it (>600kB removed?). So less to download, and to startup. |
Beta Was this translation helpful? Give feedback.
-
This assertion is at least surprising (if it was true, why are there browser caches ?) and a little unfair in the context of this discussion, since it allows you to ignore the built-in tools used by Brython to avoid explicit pre-translation to Javascript. |
Beta Was this translation helpful? Give feedback.
-
@ed2050 I tested the biggest of your examples (tifffile.py) There was a specific issue with multi-line annotations, which are present in many places in this script. I fixed it in commit 0d4fe31. Then I tried another approach to generate the tokens fed to the parser. So far it was done by a Javascript generator that yields tokens or raises an error; replacing it by a function that returns the list of tokens, stopping if an error is detected, reduced the parsing time by around 30%. This is implemented in commit 003c1c7. With these commits the compile time of this script should be significantly smaller. The second commit should also bring visible improvements for scripts that have a few thousands of lines. |
Beta Was this translation helpful? Give feedback.
-
Not directly related to this discussion, but in recent commits I have modified |
Beta Was this translation helpful? Give feedback.
-
As title says, this post is about time measurements for precompiled brython scripts.
Rationale
As Pierre said in another discussion:
Before building such an app, I wanted to measure how much time is saved by using precompiled brython scripts (i.e. running
brython.pythonToJS ()
on the python code and loading the resulting js code in the web page, instead of having brython parse and generate each time).Some of this can already be achieved with exec option
<brython-options debug="1" cache="true">
and with compiling stdlib modules. But there's a gap on first script load these tools don't cover. The savings can potentially be significant: think hundreds or thousands of users a day on an internal network visiting a brython page.Data
Let's get straight to the data. Methodology is below for anyone who wants details.
I tested 5 scripts of python code of varying sizes. First 2 from Gallery on brython website, other 3 from pypi projects (they aren't brython code per se, just straight python, but it doesn't matter. the scripts don't need to run successfully, just be parsed and generate js).
Data gathered on chrome with webserver running on localhost.
Observations
Brython parsing scales well. There's a fixed penalty to parsing and generating code, so short files like pie chart have a high cost relative to their line count. But overall the fixed penalty is quite modest <100ms. Larger files compile much faster, with cost decreasing faster than line count increases (appears logarithmic).
Despite this, compile time becomes noticeable around 5000 lines when it approaches nearly half a second. At 20,000 lines parsing time is nearly 1 second.
Let's consider energy usage across a network of users. A 500 ms delay for 1000 users is 500 secs of duplicated processor time, when the script could be compiled once on the server instead of separately in each user's browser. Intel CPUs can consume 100 W more power at high load, let's assume +50W for brython parsing. That's 0.007 kwh per day x 250 working days = 1.7 kwh per year. Not a significant amount until you get into millions of users (1 million users a day = 1,736 kwh per year x 0.40 $ / kwh = $694). Negligible in most cases.
Precompilation only seems to be worth it under these conditions:
Methodology
Measuring the time it takes a script to compile is tricky without access to brython internals. In theory, I could read through brython.js source, find the entry and exit points for parsing and js code generation, and measure time between those points. I tried that approach and quickly gave up as it takes too long in practice.
Instead I decided to measure two points as a proxy:
To measure #1, I inserted this at the bottom of each html page in the test, right before the closing tag:
To measure #2, I inserted this code at the top of every brython script in the test:
These points aren't perfect. A bit more happens after the
start
time before brython is invoked: browser finishes parsing DOM, might do cleanup, might fetch remote resources, etc, before issuing DOMContentLoaded event and invoking brython (). And brython does more than just parse the user script and generate js code: general setup and bookkeeping, environment detection, searching DOM for script tags, among other tasks.To account for this, I took time measurements for both the raw (normal) brython script and the precompiled js version. The difference between them should reflect the time it takes brython to parse and compile the script. Here's the full version of the table above, showing the values Compile time was derived from.
<script type="text/python">
For sake of completeness:
<script src="script.py.js" type="text/javascript" defer>
. note that defer is required to start script after DOM loads (and after brython () starts)Conclusion
This post is a bit long, but I wanted to thoroughly describe what I did. Hope it makes sense for anyone interested.
Beta Was this translation helpful? Give feedback.
All reactions