Source:
<code>public ProduceResponse(Struct struct) { responses = new HashMap<>(); for (Object topicResponse : struct.getArray(RESPONSES_KEY_NAME)) { Struct topicRespStruct = (Struct) topicResponse; String topic = topicRespStruct.get(TOPIC_NAME); for (Object partResponse : topicRespStruct.getArray(PARTITION_RESPONSES_KEY_NAME)) { Struct partRespStruct = (Struct) partResponse; int partition = partRespStruct.get(PARTITION_ID); Errors error = Errors.forCode(partRespStruct.get(ERROR_CODE)); long offset = partRespStruct.getLong(BASE_OFFSET_KEY_NAME); long logAppendTime = partRespStruct.getLong(LOG_APPEND_TIME_KEY_NAME); long logStartOffset = partRespStruct.getOrElse(LOG_START_OFFSET_FIELD, INVALID_OFFSET); <START> List<ErrorRecord> errorRecords = new ArrayList<>(); <END> if (partRespStruct.hasField(ERROR_RECORDS_KEY_NAME)) { for (Object recordOffsetAndMessage : partRespStruct.getArray(ERROR_RECORDS_KEY_NAME)) { Struct recordOffsetAndMessageStruct = (Struct) recordOffsetAndMessage; errorRecords.add(new ErrorRecord( recordOffsetAndMessageStruct.getInt(RELATIVE_OFFSET_KEY_NAME), recordOffsetAndMessageStruct.getOrElse(RELATIVE_OFFSET_ERROR_MESSAGE_FIELD, ""))); } } String errorMessage = partRespStruct.getOrElse(ERROR_MESSAGE_FIELD, ""); TopicPartition tp = new TopicPartition(topic, partition); responses.put(tp, new PartitionResponse(error, offset, logAppendTime, logStartOffset, errorRecords, errorMessage)); } } this.throttleTimeMs = struct.getOrElse(THROTTLE_TIME_MS, DEFAULT_THROTTLE_TIME); }</code><technical_language>Record errors unlikely, initialize this data structure lazily. simple this: java List<ErrorRecord> errorRecords = Collections.emptyList(); if (partRespStruct.hasField(ERROR_RECORDS_KEY_NAME)) { Object[] recordErrorsArray = partRespStruct.getArray(ERROR_RECORDS_KEY_NAME) if (recordErrorsArray.length > 0) { errorRecords = new ArrayList<>(recordErrorsArray.length); ... } }</technical_language>

Target:
public ProduceResponse(Struct struct) { responses = new HashMap<>(); for (Object topicResponse : struct.getArray(RESPONSES_KEY_NAME)) { Struct topicRespStruct = (Struct) topicResponse; String topic = topicRespStruct.get(TOPIC_NAME); for (Object partResponse : topicRespStruct.getArray(PARTITION_RESPONSES_KEY_NAME)) { Struct partRespStruct = (Struct) partResponse; int partition = partRespStruct.get(PARTITION_ID); Errors error = Errors.forCode(partRespStruct.get(ERROR_CODE)); long offset = partRespStruct.getLong(BASE_OFFSET_KEY_NAME); long logAppendTime = partRespStruct.getLong(LOG_APPEND_TIME_KEY_NAME); long logStartOffset = partRespStruct.getOrElse(LOG_START_OFFSET_FIELD, INVALID_OFFSET); List<RecordError> recordErrors = Collections.emptyList(); if (partRespStruct.hasField(RECORD_ERRORS_KEY_NAME)) { Object[] recordErrorsArray = partRespStruct.getArray(RECORD_ERRORS_KEY_NAME); if (recordErrorsArray.length > 0) { recordErrors = new ArrayList<>(recordErrorsArray.length); for (Object indexAndMessage : recordErrorsArray) { Struct indexAndMessageStruct = (Struct) indexAndMessage; recordErrors.add(new RecordError( indexAndMessageStruct.getInt(BATCH_INDEX_KEY_NAME), indexAndMessageStruct.get(BATCH_INDEX_ERROR_MESSAGE_FIELD) )); } } } String errorMessage = partRespStruct.getOrElse(ERROR_MESSAGE_FIELD, null); TopicPartition tp = new TopicPartition(topic, partition); responses.put(tp, new PartitionResponse(error, offset, logAppendTime, logStartOffset, recordErrors, errorMessage)); } } this.throttleTimeMs = struct.getOrElse(THROTTLE_TIME_MS, DEFAULT_THROTTLE_TIME); }