Skip to content

Commit

Permalink
Drop extra type check in _extract_serialize
Browse files Browse the repository at this point in the history
  • Loading branch information
jakirkham committed Nov 25, 2020
1 parent 723f481 commit 23181f2
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions distributed/protocol/serialize.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from array import array
from functools import partial
from itertools import repeat
import traceback
import importlib
from enum import Enum
Expand Down Expand Up @@ -445,27 +444,32 @@ 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 = dict()
elif typ_x is list:
x_items = enumerate(x)
x2.extend(repeat(None, len(x)))
x2 = len(x) * [None]

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


def _extract_serialize(x, x_items, x2, ser, bytestrings, path=()):
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)
if typ_v is dict:
v_items = v.items()
x2[k] = v2 = dict()
_extract_serialize(v, v_items, v2, ser, bytestrings, path_k)
elif typ_v is list:
v_items = enumerate(v)
x2[k] = v2 = len(v) * [None]
_extract_serialize(v, v_items, 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:
Expand Down

0 comments on commit 23181f2

Please sign in to comment.