Better HN
topnewbestaskshowjobs
Log inSign up

itamarst

5,611 karma · joined Feb 24, 2016 (10y ago) · 1,522 submissions

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

comment 4mo ago · link · parent

When this was posted to lobsters someone shared this relevant link: https://curiouscoding.nl/posts/static-search-tree/

comment 4mo ago · link · parent

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.

comment 4mo ago · link · parent

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.

4.

Testing the compiler optimizations your code relies on(pythonspeed.com ↗)

1 points · by itamarst · 1y ago · discuss

5.

500× faster: Four different ways to speed up your code(pythonspeed.com ↗)

3 points · by itamarst · 1y ago · discuss

comment 1y ago · link · parent

That's great! Would also be cool (separately from Pydantic use case) to add jiter backend to ijson.

comment 1y ago · link · parent

https://remarkjs.com/

comment 1y ago · link · parent

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.

comment 1y ago · link · parent

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.

comment 1y ago · link · parent

msgspec is much more memory efficient out of the box, yes. Also quite fast.

comment 1y ago · link · parent

You can't add extra attributes that weren't part of the original dataclass definition:

  >>> from dataclasses import dataclass
  >>> @dataclass
  ... class C: pass
  ... 
  >>> C().x = 1
  >>> @dataclass(slots=True)
  ... class D: pass
  ... 
  >>> D().x = 1
  Traceback (most recent call last):
    File "<python-input-4>", line 1, in <module>
      D().x = 1
      ^^^^^
  AttributeError: 'D' object has no attribute 'x' and no __dict__ for setting new attributes
Most of the time this is not a thing you actually need to do.

comment 1y ago · link · parent

For my PyCon 2025 talk I did this. Video isn't up yet, but slides are here: https://pythonspeed.com/pycon2025/slides/

The linked-from-original-article ijson article was the inspiration for the talk: https://pythonspeed.com/articles/json-memory-streaming/

comment 1y ago · link · parent

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.

14.

Loading Pydantic models from JSON without running out of memory(pythonspeed.com ↗)

134 points · by itamarst · 1y ago · 45 comments

15.

The surprising way to save memory with BytesIO(pythonspeed.com ↗)

4 points · by itamarst · 1y ago · discuss

16.

Faster pip installs: caching, bytecode compilation, and uv(pythonspeed.com ↗)

2 points · by itamarst · 1y ago · discuss

17.

Catching memory leaks with your test suite(pythonspeed.com ↗)

3 points · by itamarst · 1y ago · discuss

18.

Reducing CO₂ emissions with faster software(pythonspeed.com ↗)

3 points · by itamarst · 1y ago · discuss

19.

Using portable SIMD in stable Rust(pythonspeed.com ↗)

1 points · by itamarst · 2y ago · discuss

comment 2y ago · link · parent

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.

21.

Beyond multi-core parallelism: faster Mandelbrot with SIMD(pythonspeed.com ↗)

4 points · by itamarst · 2y ago · discuss

22.

Let's build and optimize a Rust extension for Python(pythonspeed.com ↗)

4 points · by itamarst · 2y ago · discuss

comment 2y ago · link · parent

It's much faster! There's been significant performance improvements since 3.8.

24.

It's time to stop using Python 3.8(pythonspeed.com ↗)

5 points · by itamarst · 2y ago · 5 comments

12345
51