Skip to content

Commit

Permalink
v2.8 - faster serialization of dict's without use_strict_order
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmaslanka committed Aug 6, 2021
1 parent 02131b2 commit 2e07151
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 18 deletions.
6 changes: 0 additions & 6 deletions CHANGELOG.md

This file was deleted.

6 changes: 6 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
=========

v2.8
----

* serializing a dict without :code:`use_strict_order` won't construct a list of it's items,
allowing you to serialize large dictionaries

v2.7
----

Expand Down
3 changes: 2 additions & 1 deletion docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,6 @@ There's also a class available for encoding. Use it like you would a normal Pyth
and sort them before dumping them to binary output. By enabling this feature you guarantee
that identical dicts, serialized by identical Pythons will have the exact same binary representation.


So take care when serializing that with large dicts, :code:`use_strict_order` will construct a list
of all it's items, while serializing a normal dict won't.
Only then strict order will be guaranteed. Your keys must be comparable anyway.
28 changes: 18 additions & 10 deletions minijson.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -514,13 +514,18 @@ cdef class MiniJSONEncoder:
cio.write(b'\x12')
cio.write(STRUCT_L.pack(length))
length = 5
items = list(data.items())
if self.use_strict_order:
items = list(data.items())
items.sort()
for field_name, elem in items:
cio.write(bytearray([len(field_name)]))
cio.write(field_name.encode('utf-8'))
length += self.dump(elem, cio)
for field_name, elem in items:
cio.write(bytearray([len(field_name)]))
cio.write(field_name.encode('utf-8'))
length += self.dump(elem, cio)
else:
for field_name, elem in data.items():
cio.write(bytearray([len(field_name)]))
cio.write(field_name.encode('utf-8'))
length += self.dump(elem, cio)
return length
else:
if length <= 0xF:
Expand All @@ -537,13 +542,16 @@ cdef class MiniJSONEncoder:
cio.write(b'\x13')
cio.write(STRUCT_L.pack(length))
offset = 5

items = list(data.items())
if self.use_strict_order:
items = list(data.items())
items.sort()
for key, value in items:
offset += self.dump(key, cio)
offset += self.dump(value, cio)
for key, value in items:
offset += self.dump(key, cio)
offset += self.dump(value, cio)
else:
for key, value in data.items():
offset += self.dump(key, cio)
offset += self.dump(value, cio)
return offset
else:
v = self.default(data)
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# coding: utf-8
[metadata]
version = 2.7
version = 2.8
name = minijson
long_description = file: README.md
long_description_content_type = text/markdown; charset=UTF-8
Expand Down

0 comments on commit 2e07151

Please sign in to comment.