News for hackers. Stuff that matters.
Re:Making Python 100x faster with less than 100 lines of Rust by korijn on Thursday March 30, 2023 @02:53AM (#35369018)
A vectorized implementation of find_close_polygons wouldn't be very complex or hard to maintain at all, but the authors would also have to ditch their OOP class based design, and that's the real issue here. The object model doesn't lend itself to performant, vectorized numpy code.
Re:Making Python 100x faster with less than 100 lines of Rust by pbowyer on Thursday March 30, 2023 @04:47AM (#35369813)
What's a good guide to learn how to make (and see) vectorized code? It's a mindshift and not one I find easy.
Re:Making Python 100x faster with less than 100 lines of Rust by appeldorian on Thursday March 30, 2023 @05:01AM (#35369906)
I think a great start is to make arrays of similar data. Instead of an array of (x,y,z) use an array for x, an array for y and another one for z. If you then square these and sum them for example, the compiler might figure out good optimizations for it if you write it as a simple loop.

Also read about SIMD instructions like AVX2. They are often used under the hood when possible, but just knowing what they require can help "triggering" them, depending on which language you use. In C++ for example, the compiler really looks for opportunities to use those instructions. You can tell the compiler did it, by looking in the assembly code if any XMM or YMM registers are being used (these are the names of the SIMD registers).

Re:Making Python 100x faster with less than 100 lines of Rust by akasakahakada on Thursday March 30, 2023 @06:32AM (#35370576)
A more accruate keyword for googling is "SIMD". Single Instruction Multiple Data.

Numpy's tutorial for broadcasting is also a good starting point.

https://numpy.org/doc/stable/user/basics.broadcasting.html

Re:Making Python 100x faster with less than 100 lines of Rust by korijn on Thursday March 30, 2023 @01:47PM (#35376520)
The gist of it is that you give numpy two arrays, and what operation to apply. Then numpy will figure out what the for loop(s) should look like depending on the shape of the arrays.

You can look at various tutorials to see how it works. For example: https://jakevdp.github.io/PythonDataScienceHandbook/02.05-co...

Re:Making Python 100x faster with less than 100 lines of Rust by rwalle on Thursday March 30, 2023 @06:20AM (#35370471)
Exactly, that is the real issue, vectorization might be good enough in terms of performance. It doesn't seem to be mentioned in the article at all.
Re:Making Python 100x faster with less than 100 lines of Rust by hn92726819 on Thursday March 30, 2023 @08:26AM (#35371560)
It might have been added later, but the author mentions vectorization in the beginning:

> It’s worth noting that converting parts of / everything to vectorized numpy might be possible for this toy library, but will be nearly impossible for the real library while making the code much less readable and modifiable, and the gains are going to be limited (here’s a partially vertorized version, which is faster but far from the results we are going to achieve).

Re:Making Python 100x faster with less than 100 lines of Rust by akasakahakada on Thursday March 30, 2023 @09:12AM (#35372134)
Semi Vectorized code:

https://github.com/ohadravid/poly-match/blob/main/poly_match...

Expecting Python engineers unable to read defacto standard numpy code but meanwhile expect everyone can read Rust.....

Not to mention that the semi-vectorized code is still suboptimal. Too many for loops despite the author clearly know they can all be vectorized.

For example instead the author can just write something like:

   np.argmin(
    distances[distances<=threshold]
    )
Also in oneplace there is:

    np.xxx( np.xxx, np.xxx + np.xxx)
You can just slap numexpr on top of it to compile this line on the fly.

https://github.com/pydata/numexpr

Re:Making Python 100x faster with less than 100 lines of Rust by Tarq0n on Thursday March 30, 2023 @12:48PM (#35375595)
Not super familiar with Python but isn't that append call within a loop going to cause a lot of allocations?
Re:Making Python 100x faster with less than 100 lines of Rust by akasakahakada on Thursday March 30, 2023 @01:08PM (#35375917)
3000 calls to list.append() cost only 2ms. In a computational intense program, no one bothers. Because usually one call to do matrix multiplication is already costing 500ms or so.

Of couse you can prelocate memory for size=3000, and append stuff in a loop. But this saves only 10ms. Too insignificant.

Re:Making Python 100x faster with less than 100 lines of Rust by ohr on Friday March 31, 2023 @07:34AM (#35385481)
Author here:

For the original library we did all the numpy tricks we could think of, but we really needed to do this type of exhaustive search for some of the data.

If someone wants to open a PR with a "fully optimized" numpy code, that would be very cool just for comparison :)