I write about speeding up Python software development, and code, at https://pythonspeed.com
I write about fundamental engineering skills and programmer career advice at https://codewithoutrules.com
I also write a weekly email about all the mistakes I've made both coding and in my career over the past 20 years, so that you can learn and avoid them: https://softwareclown.com
Submissions
[untitled]
1 points·by itamarst··0 comments
[untitled]
1 points·by itamarst··0 comments
[untitled]
1 points·by itamarst··0 comments
[untitled]
1 points·by itamarst··0 comments
Testing the compiler optimizations your code relies on
pythonspeed.com
1 points·by itamarst··0 comments
500× faster: Four different ways to speed up your code
pythonspeed.com
3 points·by itamarst··0 comments
Loading Pydantic models from JSON without running out of memory
pythonspeed.com
134 points·by itamarst··45 comments
The surprising way to save memory with BytesIO
pythonspeed.com
4 points·by itamarst··0 comments
Faster pip installs: caching, bytecode compilation, and uv
pythonspeed.com
2 points·by itamarst··0 comments
Catching memory leaks with your test suite
pythonspeed.com
3 points·by itamarst··0 comments
Reducing CO₂ emissions with faster software
pythonspeed.com
3 points·by itamarst··0 comments
Using portable SIMD in stable Rust
pythonspeed.com
1 points·by itamarst··0 comments
Beyond multi-core parallelism: faster Mandelbrot with SIMD
pythonspeed.com
4 points·by itamarst··0 comments
Let's build and optimize a Rust extension for Python
pythonspeed.com
4 points·by itamarst··0 comments
It's time to stop using Python 3.8
pythonspeed.com
5 points·by itamarst··5 comments
Running 15× faster with a situation-specific algorithm
pythonspeed.com
4 points·by itamarst··0 comments
Jevons Paradox doesn't always apply to software
pythonspeed.com
2 points·by itamarst··0 comments
Not just Nvidia: GPU programming that runs everywhere
The bucket boundaries are often chosen from a random sample of the data, if the input data is very large. Sorting is O(nlogn), but using binary search per value to assign a bucket is O(n), plus the cost of creating the buckets on a sample. So once you hit a large enough number of values this scales better.
Binary search does give random access but in this case there's only up to 255 buckets typically, so it's random access on cached memory.
Better guesses reduce the number of guesses, so there will be less branch misprediction, but there will still be mispredictions for each remaining branch. So I would guess branchless interpolation search would still help.
In practice because the real code in scikit-learn is used in parallel, memory bandwidth starts being a problem in real usage. Plus, in the overall algorithm (this is just a small part) the time spent on binary search is now low enough that there are other, more significant bottlenecks elsewhere. So in practice the branchless optimization had enough impact on the original motivating code base that there didn't seem much point spending more time on it.
I talk about this more explicitly in the PyCon talk (https://pythonspeed.com/pycon2025/slides/ - video soon) though that's not specifically about Pydantic, but basically:
1. Inefficient parser implementation.
It's just... very easy to allocate way too much memory if you don't think about large-scale documents, and very difficult to measure. Common problem with many (but not all) JSON parsers.
2. CPython in-memory representation is large compared to compiled languages.
So e.g. 4-digit integer is 5-6 bytes in JSON, 8 in Rust if you do i64, 25ish in CPython. An empty dictionary is 64 bytes.
Once you switch to ijson it will not save any memory, no, because ijson essentially uses zero memory for the parsing. You're just left with the in-memory representation.
One key question in these sort of things is how free() works: it is given a pointer, and it has to decide whether this was sampled or not, with _minimum_ effort.
Poireau does this, IIRC, by putting the pointers it sampled in a different memory address.
Sciagraph (https://sciagraph.com), a profiler I created, uses allocation size. If an allocation is chosen for sampling the profiler makes sure its size is at least 16KiB. Then free() will assume that any allocation 16KiB or larger is sampled. This may not be true, it might be false positive, but it means you don't have to do anything beyond malloc_usable_size() if you have free() on lots and lots of small allocations. A previous iteration used alignment as a heuristic, so that's another option.
It's somewhat domain specific. Pure Python libraries have easier time supporting new releases than libraries that rely on C APIs, and even slower are those that deal with less stable implementation details like bytecode (e.g. Numba). But definitely getting better and faster every release.
There's overhead in transferring data from CPU to GPU and back. I'm not sure how this works with internal GPUs, though, insofar as RAM is shared.
In general, though, as I understand it (not a GPU programmer) you want to pass data to the GPU, have it do a lot of operations, and only then pass it back. Doing one tiny operation isn't worth it.
NumPy default is that you iterate over the earlier dimensions first.
The slow code is likely at least partially slow due to branch misprediction (this is specific to my CPU, not true on CPUs with AVX-512), see https://pythonspeed.com/articles/speeding-up-numba/ where I use `perf stat` to get branch misprediction numbers on similar code.
With SIMD disabled there's also a clear difference in IPC, I believe.
The bigger picture though is that the goal of this article is not to demonstrate speeding up code, it's to ask about level of parallelism given unchanging code. Obviously all things being equal you'll do better if you can make your code faster, but code does get deployed, and when it's deployed you need to choose parallelism levels, regardless of how good the code is.
That's not it. I updated the article with an experiment of processing 5 items at a time. The fast function doing 5 images at a time is slower than the slow function doing 1 image at a time (24*5 > 90).
If your theory was correct, we would expect the optimal number of threads for the fast function processing 5 images at a time to be similar to that of the slow function processing 1 image at a time.
In fact, the optimal threads in this case (5 images at a time) was 20 for slow function, 10 for fast function, so essentially the same as the original setup.
There's presumably a reason they've spent the past 20 years adding additional instructions to CPUs, yeah :) And a large part of the Python ecosystem just ignores all of them. (NumPy has a bunch of SIMD with function-level dispatch, and they add more over time.)
I write about fundamental engineering skills and programmer career advice at https://codewithoutrules.com
I also write a weekly email about all the mistakes I've made both coding and in my career over the past 20 years, so that you can learn and avoid them: https://softwareclown.com