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.
Add
order
kwarg to encodersThis adds an
order
kwarg to all encoders for configuring how unorderedcollections/objects are encoded. Options are:
None
: the default. All objects are encoded in the most efficientmanner corresponding to their in-memory representation.
'deterministic'
: Unordered collections (sets, dicts) are sortedbefore encoding. This ensures a consistent output between runs, which
may be useful when comparing/hashing the encoded binary
representation.
'sorted'
: same as'deterministic'
, but all objet-like objectswill have their fields encoded in alphabetical order by name. This is
more expensive than
'deterministic'
, but may be useful for makingthe output more human readable.
The
'deterministic'
output has been heavily optimized - given the workrequired to accomplish this feature, I wouldn't expect we can speed up
this operation much more. The
'sorted'
option has not been fullyoptimized (the assumption being a human-readable output is rarely perf
sensitive). If needed, there are some rather simple optimizations we can
add here to speed this up further.
In general,
msgspec.json.encode(obj, order="deterministic")
should beas fast or faster than
orjson.dumps(obj, option=orjson.OPT_SORT_KEYS)
.For common small object sizes we average a ~20% speedup over
orjson
for key sorting.
Fixes #609.