69 karma · joined Sep 11, 2014 (12y ago) · 38 submissions
1.
Faster, more memory-efficient Python JSON parsing with msgspec(pythonspeed.com ↗)
2 points · by jammycrisp · 3y ago · discuss
comment 3y ago · link · parent
comment 3y ago · link · parent
This is a neat approach! I'm curious how well it maps to actual perf degradations though. Valgrind models an old CPU with a more naive branch predictor. For low-level branch-y code (say a native JSON parser), I'd be curious how well valgrind's simulated numbers map to real world measurements?
My probably naive intuition guesses that some low-level branchy code that valgrind thinks may be slower may run fine on a modern CPU (better branch predictor, deeper cache hierarchy). I'd expect false negatives to be rarer though - if valgrind thinks it's faster it probably is? What's your experience been like here?
comment 3y ago · link · parent
comment 3y ago · link · parent
It works out-of-the-box with attrs objects (as well as its own faster `Struct` types), while being ~10-15x faster than cattrs for encoding/decoding/validating JSON. The hope is it's easy to integrate msgspec with other tools (like attrs!) rather than forcing the user to rewrite code to fit the new validation/serialization framework. It may not fit every use case, but if msgspec works for you it should be generally an order-of-magnitude faster than other Python options.
[1]: https://github.com/jcrist/msgspec
</blatant-evangelism>
comment 3y ago · link · parent
That is at least partly the case. I maintain msgspec[1], another Python JSON validation library. Pydantic V1 was ~100x slower at encoding/decoding/validating JSON than msgspec, which was more a testament to Pydantic's performance issues than msgspec's speed. Pydantic V2 is definitely faster than V1, but it's still ~10x slower than msgspec, and up to 2x slower than other pure-python implementations like mashumaro.
Recent benchmark here: https://gist.github.com/jcrist/d62f450594164d284fbea957fd48b...
comment 3y ago · link · parent
I maintain msgspec (https://github.com/jcrist/msgspec), a serialization/validation library which provides similar functionality to pydantic. Recent benchmarks of pydantic V2 against msgspec show msgspec is still 15-30x faster at JSON encoding, and 6-15x faster at JSON decoding/validating.
Benchmark (and conversation with Samuel) here: https://gist.github.com/jcrist/d62f450594164d284fbea957fd48b...
This is not to diminish the work of the pydantic team! For many users pydantic will be more than fast enough, and is definitely a more feature-filled tool. It's a good library, and people will be happy using it! But pydantic is not the only tool in this space, and rubbing some rust on it doesn't necessarily make it "fast".
9.
Show HN: Msgspec, a fast serialization/validation library for Python(github.com ↗)
3 points · by jammycrisp · 3y ago · 1 comment
comment 3y ago · link · parent
msgspec[1] is another parsing/validation library, written in C. It's on average 50-80x faster than pydantic for parsing and validating JSON [2]. This speedup is only possible because we make use of native code, letting us parse JSON directly and efficiently into the proper python types, removing any unnecessary allocations.
It's my understanding that pydantic V2 currently doesn't do this (they still have some unnecessary intermediate allocations during parsing), but having the validation logic already in compiled code makes integrating this with the parser theoretically possible later on. With the logic in python this efficiency gain wouldn't be possible.
[1]: https://github.com/jcrist/msgspec
[2]: https://jcristharif.com/msgspec/benchmarks.html#benchmark-sc...
comment 3y ago · link · parent
One of the design goals for msgspec (besides much higher performance) was simpler usage. Fewer concepts to wrap your head around, fewer config options to learn about. I personally find pydantic's kitchen sink approach means sometimes I have a hard time understanding what a model will do with a given json structure. IMO the serialization/validation part of your code shouldn't be the most complicated part.
comment 3y ago · link · parent
comment 3y ago · link · parent
disable_test_id_escaping_and_forfeit_all_rights_to_community_support = True
https://docs.pytest.org/en/6.2.x/parametrize.html#pytest-mar...comment 3y ago · link · parent
I maintain another dataclass-like library[1] that's written fully as a C extension. Moving this code to C means these types are typically 5-10x faster for common operations[2]. It'd be nice if the builtin dataclasses were equally performant.
[1]: https://github.com/jcrist/msgspec
[2]: https://jcristharif.com/msgspec/benchmarks.html#benchmark-st...
comment 3y ago · link · parent
comment 3y ago · link · parent
comment 3y ago · link · parent
comment 3y ago · link · parent
21.
Ibis-datasette: query datasettes via a Python dataframe API(jcristharif.com ↗)
1 points · by jammycrisp · 4y ago · discuss
22.
Faster, more memory-efficient Python JSON parsing with msgspec(pythonspeed.com ↗)
2 points · by jammycrisp · 4y ago · discuss
23.
Show HN: Msgspec – a fast JSON schema library for Python(jcristharif.com ↗)
2 points · by jammycrisp · 4y ago · discuss
comment 4y ago · link · parent
comment 4y ago · link · parent
Even with orjson, you're still paying the cost of creating a new PyObject for every node in the JSON blob. orjson is well engineered (as is the backing serde-json decoder), but any JSON decoder that isn't using naive algorithms is mostly bound by the cost of creating PyObjects. Allocating in Python is _slow_.
I wrote a quick benchmark (https://gist.github.com/jcrist/de29815389eaed4eaf5b24fbcfdab...) showing a handwritten query that accesses only a few fields in a 13 MiB JSON file. The same query is repeated with a number of different Python JSON libraries. Results:
$ python bench_repodata_query.py
msgspec: 45.018014032393694 ms
simdjson: 61.94157397840172 ms
orjson: 105.34720402210951 ms
ujson: 121.9699690118432 ms
json: 113.79130696877837 ms
While `orjson`, is faster than `ujson`/`json` here, it's only ~6% faster (in this benchmark). `simdjson` and `msgspec` (my library, see https://jcristharif.com/msgspec/) are much faster due to them avoiding creating PyObjects for fields that are never used.If spyql's query engine can determine the fields it will access statically before processing, you might find using `msgspec` for JSON gives a nice speedup (it'll also type check the JSON if you know the type of each field). If this information isn't known though, you may find using `pysimdjson` (https://pysimdjson.tkte.ch/) gives an easy speed boost, as it should be more of a drop-in for `orjson`.
26.
Show HN: Msgspec – a fast JSON/MessagePack library for Python(jcristharif.com ↗)
2 points · by jammycrisp · 4y ago · discuss
comment 5y ago · link · parent
comment 5y ago · link · parent
Mirroring the author's initialization benchmark:
In [1]: import msgspec
In [2]: from typing import NamedTuple
In [3]: class Point(msgspec.Struct):
...: x: int
...: y: int
...:
In [4]: class PointNT(NamedTuple):
...: x: int
...: y: int
...:
In [5]: %timeit Point(1, 2)
48.4 ns ± 0.195 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
In [6]: %timeit PointNT(1, 2)
185 ns ± 0.851 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)