Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Avoid recursion in _extract_serialize #4258

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 35 additions & 23 deletions distributed/protocol/serialize.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from array import array
from collections import deque
from functools import partial
from itertools import repeat
import traceback
import importlib
from enum import Enum
Expand Down Expand Up @@ -445,34 +445,46 @@ def extract_serialize(x):
>>> extract_serialize(msg)
({'op': 'update'}, {('data',): <Serialize: 123>}, set())
"""
x2 = type(x)()
ser = {}
bytestrings = set()
_extract_serialize(x, x2, ser, bytestrings)
return x2, ser, bytestrings


def _extract_serialize(x, x2, ser, bytestrings, path=()):
typ_x = type(x)
if typ_x is dict:
x_items = x.items()
x2 = {}
elif typ_x is list:
x_items = enumerate(x)
x2.extend(repeat(None, len(x)))

for k, v in x_items:
path_k = path + (k,)
typ_v = type(v)
if typ_v is dict or typ_v is list:
x2[k] = v2 = typ_v()
_extract_serialize(v, v2, ser, bytestrings, path_k)
elif typ_v is Serialize or typ_v is Serialized:
ser[path_k] = v
elif (typ_v is bytes or typ_v is bytearray) and len(v) > 2 ** 16:
ser[path_k] = to_serialize(v)
bytestrings.add(path_k)
x2 = len(x) * [None]

ser = {}
bytestrings = set()
_extract_serialize(x_items, x2, ser, bytestrings)
return x2, ser, bytestrings


def _extract_serialize(x_items, x2, ser, bytestrings, path=()):
q = deque()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're using this as only a stack then maybe this should be a list rather than a deque

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I tried both. This performed better based on the benchmarking I did. That said, it seems that recursion itself is already handled quite efficiently.

while True:
for k, v in x_items:
path_k = path + (k,)
typ_v = type(v)
if typ_v is dict:
v_items = v.items()
x2[k] = v2 = {}
q.append((v_items, v2, path_k))
elif typ_v is list:
v_items = enumerate(v)
x2[k] = v2 = len(v) * [None]
q.append((v_items, v2, path_k))
elif typ_v is Serialize or typ_v is Serialized:
ser[path_k] = v
elif (typ_v is bytes or typ_v is bytearray) and len(v) > 2 ** 16:
ser[path_k] = to_serialize(v)
bytestrings.add(path_k)
else:
x2[k] = v

if q:
x_items, x2, path = q.pop()
else:
x2[k] = v
break


def nested_deserialize(x):
Expand Down