Making ast.walk 220x Faster
Posted by palashawas 16 hours ago
Comments
Comment by teo_zero 3 hours ago
Comment by Loranubi 5 minutes ago
Comment by eska 15 hours ago
Comment by rcxdude 7 minutes ago
Comment by jerf 15 hours ago
The truly rough thing about Python though is that that is the speed when the code is being written to a benchmark. It is really, really easy to write Python that is multiples slower than that when not writing to a benchmark and just trying to get work done without hyperoptimizing. I did some testing of Python [1] to back some other commentary I was making that compared the time it took to set an attribute repeatedly on a particular instance of an empty class to the time it took to setting it on a subclass of a subclass of a class that had a property setter that was wrapped by a decorator. The latter was about 4.6 time slower than the direct attribute setting, which was itself already ~100x slower than an attribute setting in a static language.
And it's not like a three-deep nested class with a property wrapped by a decorator is all that absurd in Python or anything. That's a completely normal case, not some absurd example I made up to skew the test.
In practice the 40-50x number is more lower bound than what you can count on. If you are actually using Python's features I think you can easily score another order of magnitude slower without anything jumping out at you as being an obviously bad idea.
[1]: https://jerf.org/iri/post/2024/not_about_python_addendum/
Comment by elevation 12 hours ago
This is about what I observe. I had a utility based on `scapy`; there were no obviously bad ideas in the python source, but porting the work loop into a cpython extension module yielded a 500x speedup.
Comment by anitil 7 hours ago
Comment by colechristensen 14 hours ago
echo "Python sucks, use something else when you can" >> ~/CLAUDE.md
Python was cool in 2005 in academia IT, all the rage in startup 2012. These days...
Comment by flockonus 14 hours ago
Absolutely disagree here, something that is considered good practice is very interesting to compare to!
Comment by eska 12 hours ago
Comment by bionhoward 11 hours ago
Comment by anitil 6 hours ago
Comment by fwip 13 hours ago
Comment by westurner 16 hours ago
libCST: https://github.com/Instagram/LibCST
bandit: https://github.com/PyCQA/bandit
Links to codemod tools; "Baby Steps into Genetic Programming" https://news.ycombinator.com/item?id=43617655
Comment by adhami 15 hours ago
It seems bandit is using some decent optimizations already, looking at the `@test.checks("Call")` seems like they already captured some easy wins.
The largest win honestly would be using the same ast.walk for multiple rules, which we also did, but not mentioned in the blog.
Comment by westurner 1 hour ago
Docs for ruff_python_ast: https://docs.rs/littrs-ruff-python-ast/latest/ruff_python_as...
src: ruff/crates/ruff_python_ast: https://github.com/astral-sh/ruff/tree/main/crates/ruff_pyth...
ast.toml, generate.py, Cargo.toml, src/ https://github.com/astral-sh/ruff/blob/main/crates/ruff_pyth...
Just rewrote docutils and myst-md-parser in rust with byte-for-byte equivalent output the other day; as westurner/dsport/src/docutilsrs and sphinxdocrs and so on. Pygmentsrs required fancy-regex because rust regex is linear. rstest and cargo-insta helped.
PyCQA/docformatter https://github.com/PyCQA/docformatter and https://github.com/PyCQA/doc8 would be useful in rust, too. This from the other day: "A benchmark for catching when code doesn't do what its documentation claims"; docs fidelity evals https://news.ycombinator.com/item?id=48530786
Comment by westurner 16 hours ago
FST: Full Syntax Tree
CST: Concrete Syntax Tree
Comment preservation is a feature
Comment by 123rust123 16 hours ago
Comment by adhami 16 hours ago
Comment by mananaysiempre 14 hours ago
(Or you could switch to C++ and use pybind11, but now you’re just switching from one quite complex and somewhat off-putting language to another really complicated and very ugly one, so the win is less clear.)
Comment by ozgrakkurt 8 hours ago