[Experimentation] Implementing the parser in WASM (C++)

20 views
Skip to first unread message

Denis Migdal

unread,
May 25, 2025, 8:39:39 AMMay 25
to brython
Hi,

I started a new experimentation (that I may not continue immediately), trying to see if a WASM implementation would be interesting.

Indeed, Brython is quite slow on big Python file (~1sec), and while SBrython is much faster (while doing less), performances aren't that great.

The idea of using WASM would enable :
- to have better control over allocations ;
- to have more choice in the structure I can use ;
- to specify the type of variables, enabling consistant and immediate optimizations.

I will look at 3 things :
- execution time ;
- startup time ;
- library file size.

I started with a trivial example, which weren't easy to properly configure xD.

The generated WASM file is 4kB (343 bytes once compressed), and load time is <0.5ms.
The question is thus whereas this will remain constant or grow on larger code base.

I copy/pasted ~100 times a function, the generated WASM is still 4kB (is this some kind of minimal size ?), load time is then ~2ms.

Currently, my SBrython code is ~484kB (strange, I though it was way less).
I generated 11500 C++ function to produce a code source of ~484kB in order to have a raw comparison.

The generated WASM file is 296K (I though it would be larger), 54k once compressed (similar to SBrython).
Load time is 51ms (vs 10-30ms for SBrython).

This seems quite bad for WASM, but the test isn't quite fair.
Therefore, I generated a JS test file with 11500 functions, and now the JS load time is ~3x slower than WASM.

It seems that WASM should be better.
I think I may try WASM on another (smaller) project before trying it in SBrython.

Ed

unread,
May 25, 2025, 9:10:55 AMMay 25
to bry...@googlegroups.com
Interesting idea.  So only the parser would be implemented in WASM?  And generated code would still run in JS?  That would solve the issue that (afaik) WASM can't access the DOM.

What about using a python to WASM compiler?  Instead of C++, you could write the parser in python, making it easier to maintain.  Then compile to WASM with something like py2wasm.  Might be doable.  I don't know, haven't done much with WASM myself yet.

Just a thought.  Thanks for your investigation.


--
You received this message because you are subscribed to the Google Groups "brython" group.
To unsubscribe from this group and stop receiving emails from it, send an email to brython+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/brython/f9a367d7-c379-450f-93ea-d577d448f8ccn%40googlegroups.com.

Denis Migdal

unread,
May 25, 2025, 9:58:59 AMMay 25
to bry...@googlegroups.com
The parser would be in WASM, and would generate JS code.

I do not think using a python to wasm compiler would be interesting as it'd either include the whole python interpreter (bad), or would have likely some incompatibilities with the Python standard. Using a language like C++ also enables to better manage allocations and structures (else better using JS directly).

The main issue is that such experimentations are quite time consuming, but necessary if we want to really know.
Learning how to use JS/TS is way faster xD.

And, as I take some shortcuts, experimenting is still way faster than building a "final product".
This would be easily a 3 persons full time job.

Denis Migdal

unread,
May 25, 2025, 10:34:20 AMMay 25
to bry...@googlegroups.com
Another possible thing would be, if we give up transcription in the browser and focus instead on AoT without worrying about performances (as it is done outside of the browser), we could use Python's AST and type deductions, and would "only" need to translate the AST/symtable into JS (+ all the standard libraries).

That'd remove a big chunk of work, and in this case, and you could implement it directly in Python.
Meaning also that you could convert this step in JS using itself. With appropriate ast2js rules, this could be as fast as a JS implementation of the ast2js rules.


But the question is what do we want, and who is ready to work on it.

I'm just playing around, testing things.
Doing it seriously requires a MASSIVE amount of work.

Another solution, using chatGPT API to convert Python code into JS xD
Reply all
Reply to author
Forward
0 new messages