dataclass support - #218
Merged
Merged
Conversation
Member
Author
|
A simple encoding benchmark: In [1]: import msgspec, orjson
In [2]: from dataclasses import dataclass
In [3]: enc = msgspec.json.Encoder()
In [4]: @dataclass
...: class NoSlots:
...: field_one: int
...: field_two: int
...:
In [5]: @dataclass(slots=True)
...: class Slots:
...: field_one: int
...: field_two: int
...:
In [6]: no_slots = [NoSlots(i - 1, i + 1) for i in range(10000)]
In [7]: with_slots = [Slots(i - 1, i + 1) for i in range(10000)]
In [8]: %timeit enc.encode(no_slots) # msgspec, no slots
561 µs ± 2.04 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
In [9]: %timeit orjson.dumps(no_slots) # orjson, no slots
834 µs ± 1.69 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
In [10]: %timeit enc.encode(with_slots) # msgspec, with slots
779 µs ± 20 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
In [11]: %timeit orjson.dumps(with_slots) # orjson, with slots
3.71 ms ± 90.1 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [12]: class Struct(msgspec.Struct):
...: field_one: int
...: field_two: int
...:
In [13]: structs = [Struct(i - 1, i + 1) for i in range(10000)]
In [14]: %timeit enc.encode(structs) # msgspec structs
356 µs ± 307 ns per loop (mean ± std. dev. of 7 runs, 1,000 loops each)For these type definitions on my machine:
|
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds encoding/decoding support for dataclasses, mirroring our support for
typing.NamedTuple/collections.namedtuple/typing.TypedDict. Dataclasses are encoded to/decoded from mappings containing all attributes assigned to the object (union of__dict__and__slots__).I do not intend to add support for additional configuration (e.g. renaming fields using
camelCase) - if the user wants more customization they should switch to using structs. Structs support all the common features of dataclasses, but are more performant to create/use/encode/decode, and are more customizable. The intent of adding dataclass support is:orjsonis capable of encoding efficiently. Initial benchmarks show the implementation here encodes measurably faster than the one inorjson, especially ifslots=Trueis used, see below.Todo:
Fixes #57.