Readit News logoReadit News
itamarst commented on Loading Pydantic models from JSON without running out of memory   pythonspeed.com/articles/... · Posted by u/itamarst
scolvin · 10 months ago
Pydantic author here. We have plans for an improvement to pydantic where JSON is parsed iteratively, which will make way for reading a file as we parse it. Details in https://github.com/pydantic/pydantic/issues/10032.

Our JSON parser, jiter (https://github.com/pydantic/jiter) already supports iterative parsing, so it's "just" a matter of solving the lifetimes in pydantic-core to validate as we parse.

This should make pydantic around 3x faster at parsing JSON and significantly reduce the memory overhead.

itamarst · 10 months ago
That's great! Would also be cool (separately from Pydantic use case) to add jiter backend to ijson.
itamarst commented on Loading Pydantic models from JSON without running out of memory   pythonspeed.com/articles/... · Posted by u/itamarst
tomrod · 10 months ago
I have a side question -- what did you use for slides?
itamarst commented on Loading Pydantic models from JSON without running out of memory   pythonspeed.com/articles/... · Posted by u/itamarst
fidotron · 10 months ago
Having only recently encountered this, does anyone have any insight as to why it takes 2GB to handle a 100MB file?

This looks highly reminiscent (though not exactly the same, pedants) of why people used to get excited about using SAX instead of DOM for xml parsing.

itamarst · 10 months ago
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.

itamarst commented on Loading Pydantic models from JSON without running out of memory   pythonspeed.com/articles/... · Posted by u/itamarst
zxilly · 10 months ago
Maybe using mmap would also save some memory, I'm not quite sure if this can be implemented in Python.
itamarst · 10 months ago
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.
itamarst commented on Loading Pydantic models from JSON without running out of memory   pythonspeed.com/articles/... · Posted by u/itamarst
m_ke · 10 months ago
Or just dump pydantic and use msgspec instead: https://jcristharif.com/msgspec/
itamarst · 10 months ago
msgspec is much more memory efficient out of the box, yes. Also quite fast.

u/itamarst

KarmaCake day5606February 24, 2016
About
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

View Original