From 2e071518913bdcecb715600a2da56055755bef72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Ma=C5=9Blanka?= Date: Fri, 6 Aug 2021 18:05:19 +0200 Subject: [PATCH] v2.8 - faster serialization of dict's without use_strict_order --- CHANGELOG.md | 6 ------ docs/changelog.md | 6 ++++++ docs/usage.rst | 3 ++- minijson.pyx | 28 ++++++++++++++++++---------- setup.cfg | 2 +- 5 files changed, 27 insertions(+), 18 deletions(-) delete mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md deleted file mode 100644 index 5faaf8c..0000000 --- a/CHANGELOG.md +++ /dev/null @@ -1,6 +0,0 @@ -Changelog is kept at [GitHub](https://github.com/Dronehub/minijson/releases), -here's only the changelog for the version in development - -# v2.7 - -* added support for strict ordering diff --git a/docs/changelog.md b/docs/changelog.md index ab65e4a..05cc967 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -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 ---- diff --git a/docs/usage.rst b/docs/usage.rst index 06145a5..26d67c9 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -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. diff --git a/minijson.pyx b/minijson.pyx index 4cc6240..6bd155b 100644 --- a/minijson.pyx +++ b/minijson.pyx @@ -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: @@ -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) diff --git a/setup.cfg b/setup.cfg index 64b684a..8a68a91 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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