@@ -483,6 +483,7 @@ def __init__(self, fields=None):
483483 self .names = [f .name for f in fields ]
484484 assert all (isinstance (f , StructField ) for f in fields ),\
485485 "fields should be a list of StructField"
486+ # Precalculated list of fields that need conversion with fromInternal/toInternal functions
486487 self ._needConversion = [f .needConversion () for f in self ]
487488 self ._needSerializeAnyField = any (self ._needConversion )
488489
@@ -529,6 +530,7 @@ def add(self, field, data_type=None, nullable=True, metadata=None):
529530 data_type_f = data_type
530531 self .fields .append (StructField (field , data_type_f , nullable , metadata ))
531532 self .names .append (field )
533+ # Precalculated list of fields that need conversion with fromInternal/toInternal functions
532534 self ._needConversion = [f .needConversion () for f in self ]
533535 self ._needSerializeAnyField = any (self ._needConversion )
534536 return self
@@ -592,6 +594,7 @@ def toInternal(self, obj):
592594 return
593595
594596 if self ._needSerializeAnyField :
597+ # Only calling toInternal function for fields that need conversion
595598 if isinstance (obj , dict ):
596599 return tuple (f .toInternal (obj .get (n )) if c else obj .get (n )
597600 for n , f , c in zip (self .names , self .fields , self ._needConversion ))
@@ -624,6 +627,7 @@ def fromInternal(self, obj):
624627 # it's already converted by pickler
625628 return obj
626629 if self ._needSerializeAnyField :
630+ # Only calling fromInternal function for fields that need conversion
627631 values = [f .fromInternal (v ) if c else v
628632 for f , v , c in zip (self .fields , obj , self ._needConversion )]
629633 else :
0 commit comments