@@ -332,7 +332,6 @@ def replace_type(items, spec, loader, found):
332
332
# type: (Any, Dict[unicode, Any], Loader, Set[unicode]) -> Any
333
333
""" Go through and replace types in the 'spec' mapping"""
334
334
335
- items = copy .deepcopy (items )
336
335
if isinstance (items , dict ):
337
336
# recursively check these fields for types to replace
338
337
if "type" in items and items ["type" ] in ("record" , "enum" ):
@@ -342,6 +341,7 @@ def replace_type(items, spec, loader, found):
342
341
else :
343
342
found .add (items ["name" ])
344
343
344
+ items = copy .copy (items )
345
345
for n in ("type" , "items" , "fields" ):
346
346
if n in items :
347
347
items [n ] = replace_type (items [n ], spec , loader , found )
@@ -388,8 +388,8 @@ def make_valid_avro(items, # type: Avro
388
388
union = False # type: bool
389
389
):
390
390
# type: (...) -> Union[Avro, Dict]
391
- items = copy .deepcopy (items )
392
391
if isinstance (items , dict ):
392
+ items = copy .copy (items )
393
393
if items .get ("name" ):
394
394
items ["name" ] = avro_name (items ["name" ])
395
395
@@ -424,19 +424,31 @@ def make_valid_avro(items, # type: Avro
424
424
items = avro_name (items )
425
425
return items
426
426
427
+ def deepcopy_strip (item ): # type: (Any) -> Any
428
+ """Make a deep copy of list and dict objects.
429
+
430
+ Intentionally do not copy attributes. This is to discard CommentedMap and
431
+ CommentedSeq metadata which is very expensive with regular copy.deepcopy.
432
+
433
+ """
434
+
435
+ if isinstance (item , dict ):
436
+ return {k : deepcopy_strip (v ) for k ,v in item .iteritems ()}
437
+ elif isinstance (item , list ):
438
+ return [deepcopy_strip (k ) for k in item ]
439
+ else :
440
+ return item
427
441
428
442
def extend_and_specialize (items , loader ):
429
443
# type: (List[Dict[unicode, Any]], Loader) -> List[Dict[unicode, Any]]
430
444
"""Apply 'extend' and 'specialize' to fully materialize derived record
431
445
types."""
432
446
433
- types = {} # type: Dict[unicode, Any]
434
- for t in items :
435
- types [t ["name" ]] = t
447
+ items = deepcopy_strip (items )
448
+ types = {t ["name" ]: t for t in items } # type: Dict[unicode, Any]
436
449
n = []
437
450
438
451
for t in items :
439
- t = copy .deepcopy (t )
440
452
if "extends" in t :
441
453
spec = {} # type: Dict[unicode, unicode]
442
454
if "specialize" in t :
@@ -450,7 +462,7 @@ def extend_and_specialize(items, loader):
450
462
raise Exception ("Extends %s in %s refers to invalid base type" % (
451
463
t ["extends" ], t ["name" ]))
452
464
453
- basetype = copy .deepcopy (types [ex ])
465
+ basetype = copy .copy (types [ex ])
454
466
455
467
if t ["type" ] == "record" :
456
468
if spec :
@@ -466,6 +478,7 @@ def extend_and_specialize(items, loader):
466
478
exsym .extend (basetype .get ("symbols" , []))
467
479
468
480
if t ["type" ] == "record" :
481
+ t = copy .copy (t )
469
482
exfields .extend (t .get ("fields" , []))
470
483
t ["fields" ] = exfields
471
484
@@ -477,6 +490,7 @@ def extend_and_specialize(items, loader):
477
490
else :
478
491
fieldnames .add (field ["name" ])
479
492
elif t ["type" ] == "enum" :
493
+ t = copy .copy (t )
480
494
exsym .extend (t .get ("symbols" , []))
481
495
t ["symbol" ] = exsym
482
496
@@ -507,7 +521,6 @@ def extend_and_specialize(items, loader):
507
521
508
522
return n
509
523
510
-
511
524
def make_avro_schema (i , # type: List[Dict[unicode, Any]]
512
525
loader # type: Loader
513
526
):
0 commit comments