private List<AcknowledgementBatch> maybeOptimiseAcknowledgementTypes(AcknowledgementBatch currentAcknowledgeBatch) {
        List<AcknowledgementBatch> batches = new ArrayList<>();
        if (currentAcknowledgeBatch == null) return batches;

        long currentOffset = currentAcknowledgeBatch.firstOffset();
        int currentStartIndex = 0;
        int recordsWithSameAcknowledgeType = 1;
        for (int i = 1; i < currentAcknowledgeBatch.acknowledgeTypes().size(); i++) {
            byte acknowledgeType = currentAcknowledgeBatch.acknowledgeTypes().get(i);
            // If we have a continuous set of records with the same acknowledgement type exceeding the default count,
            // then we optimise the batches to include only start and end offset and have only 1 acknowledge type in the array.
            byte prevAcknowledgeType = currentAcknowledgeBatch.acknowledgeTypes().get(i - 1);
            if (acknowledgeType == prevAcknowledgeType && recordsWithSameAcknowledgeType >= MAX_RECORDS_WITH_SAME_ACKNOWLEDGE_TYPE) {
                // We continue traversing until we have the same acknowledge type.
                while (i < currentAcknowledgeBatch.acknowledgeTypes().size()) {
                    byte acknowledgeType2 = currentAcknowledgeBatch.acknowledgeTypes().get(i);

                    if (acknowledgeType2 != currentAcknowledgeBatch.acknowledgeTypes().get(i - 1)) break;
                    i++;
                    recordsWithSameAcknowledgeType++;
                }

                // Now we prepare 2 batches, one starting just before the batch with single acknowledge type
                // and one with the single acknowledge type.
                AcknowledgementBatch batch1 = new AcknowledgementBatch();
                batch1.setFirstOffset(currentOffset);
                batch1.setLastOffset(currentOffset + i - recordsWithSameAcknowledgeType - currentStartIndex - 1);
                if (batch1.lastOffset() >= batch1.firstOffset()) {
                    batch1.setAcknowledgeTypes(new ArrayList<>(currentAcknowledgeBatch.acknowledgeTypes().subList(currentStartIndex,
                            i - recordsWithSameAcknowledgeType)));
                    batches.add(batch1);
                }

                AcknowledgementBatch batch2 = new AcknowledgementBatch();
                batch2.setFirstOffset(currentOffset + i - recordsWithSameAcknowledgeType - currentStartIndex);
                batch2.setLastOffset(currentOffset + i - currentStartIndex - 1);
                batch2.acknowledgeTypes().add(acknowledgeType);

                batches.add(batch2);
                recordsWithSameAcknowledgeType = 1;

                // Updating the offset and startIndex for further iterations.
                currentOffset = currentOffset + i - currentStartIndex;
                currentStartIndex = i;
            } else if (acknowledgeType == prevAcknowledgeType) {
                // The maximum limit has not yet been reached, we increment the count and move ahead.
                recordsWithSameAcknowledgeType++;
            } else {
                recordsWithSameAcknowledgeType = 1;
            }
        }
        if (currentStartIndex < currentAcknowledgeBatch.acknowledgeTypes().size()) {
            AcknowledgementBatch batch = new AcknowledgementBatch();
            batch.setFirstOffset(currentOffset);
            batch.setLastOffset(currentOffset + currentAcknowledgeBatch.acknowledgeTypes().size() - currentStartIndex - 1);
            batch.setAcknowledgeTypes(new ArrayList<>(currentAcknowledgeBatch.acknowledgeTypes().subList(currentStartIndex,
                    currentAcknowledgeBatch.acknowledgeTypes().size())));
            batches.add(batch);
        }
        return batches;
    }
Traverses the acknowledgementBatch and splits it into optimal batches wherever possible.
Optimisation happens when a batch has continuous records of the same acknowledge type
whose count exceeds the default value. In this case, the batch is split into 2 such that the
batch with the continuous records has only 1 acknowledge type in its array.
clients/src/main/java/org/apache/kafka/clients/consumer/internals/Acknowledgements.java