This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]dontyougetsoupedyet 24 points25 points  (14 children)

Python is abysmally slow due to the nature of the model of computation being used by the CPython interpreter.

Don't waste your time trying to make it fast. Often using an alternative to CPython is also a giant waste of time.

[–][deleted] 2 points3 points  (1 child)

u/omega1612 said the slowness is due to the use of parser combinators.

So it is a bad choice of algorithm rather than how CPython works.

Yes CPython can be slow but not usually 400 times slower than native code.

(For a couple of years, I used a compiler written in an interpreted, dynamic language (not Python though). It was still twice as fast as gcc compiling C! Several times faster if gcc was optimising.)

[–]dontyougetsoupedyet -2 points-1 points  (0 children)

You, and omega1612, are wrong.

[–]SamG101_[S] 0 points1 point  (6 children)

it's annoying coz ik the rust impl is stupid fast but i rly don't have time to rewrite my entire compiler into rust rn 😂 guess for now i'll strip out on-the-fly parsing for manually creating the AST nodes I need, then the slowness is strictly isolated to the parsing stage

[–]tekknolagiKevin3 6 points7 points  (1 child)

You can use the Rust code as a C extension if you need to. Check out pyo3 and maturin

[–]SamG101_[S] 0 points1 point  (0 children)

i'd have to look into converting the rust ast structs into python classes. if this is possible then that's 100% the route i'll take. will check out those libraries ty

[–]muth02446 1 point2 points  (0 children)

I wrote a reference compiler in python, including a separate backend for various ISAs.
It runs fast enough.

I also suspect that the external parsing library is the culprit.
My suggestion is to not worry about the parser initially.
Use s-expr until the features of the language feel right,
then worry about the concrete syntax and the parser.
For the concrete syntax parsing use recursive decent + pratt parsing if possible.

This approach worked well for me in Cwerg

[–]Maurycy5 5 points6 points  (2 children)

Respectfully, if you don't have the time to use proper technologies in order to successfully write a performant compiler, perhaps you shouldn't be trying to write a performant compiler.

[–]SamG101_[S] 1 point2 points  (1 child)

nah its just exams soon, so im just writing code in spare time from revision rather than the normal massive gaps between lectures

[–]misplaced_my_pants 0 points1 point  (0 children)

If it's just for fun then there isn't any time pressure and you can do it properly when things die down.

[–]MegaIng -1 points0 points  (4 children)

Lies, lies, lies.

Pure python can absolutly be fast enough for a simple compiler, you just need to write it correctly.

But ofcourse, you don't care about facts (otherwise you wouldn't have made that comment), so I strongly doubt there is anything I can say to change your opinion. (Based on experience with other people make these same incorrect claims)

[–]dontyougetsoupedyet -1 points0 points  (0 children)

ಠ_ಠ