pads in a more pythonic way which is significantly faster.
@@ -179,11 +179,14 @@ def pad_sequence_to_length(
else:
padded_sequence = sequence[-desired_length:]
# Continues to pad with default_value() until we reach the desired length.
- for _ in range(desired_length - len(padded_sequence)):
+ pad_length = desired_length - len(padded_sequence)
+ # This just creates the default value once, so if it's a list, and if it gets mutated
+ # later, it could cause subtle bugs. But the risk there is low, and this is much faster.
+ values_to_pad = [default_value()] * pad_length
if padding_on_right:
- padded_sequence.append(default_value())
+ padded_sequence = padded_sequence + values_to_pad
else:
- padded_sequence.insert(0, default_value())
+ padded_sequence = values_to_pad + padded_sequence
return padded_sequence
