From 0a12d2928bc384f5197fc3431a897f2edbae730d Mon Sep 17 00:00:00 2001 From: Erik van Oosten Date: Sat, 24 Aug 2024 15:27:17 +0200 Subject: [PATCH] Iterate over promises in reverse order It is likely that the last promise completes last. If that is the case, we only wait once, the other promises are already completed and awaiting them has no overhead. --- zio-kafka/src/main/scala/zio/kafka/producer/Producer.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/zio-kafka/src/main/scala/zio/kafka/producer/Producer.scala b/zio-kafka/src/main/scala/zio/kafka/producer/Producer.scala index e4dc34496..705770e86 100644 --- a/zio-kafka/src/main/scala/zio/kafka/producer/Producer.scala +++ b/zio-kafka/src/main/scala/zio/kafka/producer/Producer.scala @@ -486,8 +486,8 @@ private[producer] final class ProducerLive( ): ZIO[Any, Nothing, Chunk[Either[Throwable, RecordMetadata]]] = for { promises <- ZIO.foreach(serializedRecords)(sendRecord(runtime)) - results <- ZIO.foreach(promises)(_.await.either) - } yield results + results <- ZIO.foreach(promises.reverse)(_.await.either) + } yield results.reverse private def sendRecord( runtime: Runtime[Any]