From bbc27171c0bddf684ece3c0f12c6d35a2925b77e Mon Sep 17 00:00:00 2001 From: Dave Lucia Date: Thu, 9 Jan 2025 00:14:46 -0500 Subject: [PATCH] Tiny optimization to KV.list_buckets/1 (#185) Removes the unnecessary flat map. The performance difference should be pretty small (tbh I didn't measure), but slightly faster and less memory. --- lib/gnat/jetstream/api/kv.ex | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/lib/gnat/jetstream/api/kv.ex b/lib/gnat/jetstream/api/kv.ex index 2d63b6e..fa37580 100644 --- a/lib/gnat/jetstream/api/kv.ex +++ b/lib/gnat/jetstream/api/kv.ex @@ -309,14 +309,9 @@ defmodule Gnat.Jetstream.API.KV do def list_buckets(conn) do with {:ok, %{streams: streams}} <- Stream.list(conn) do stream_names = - streams - |> Enum.flat_map(fn bucket -> - if kv_bucket_stream?(bucket) do - [bucket |> String.trim_leading(@stream_prefix)] - else - [] - end - end) + for bucket <- streams, kv_bucket_stream?(bucket) do + String.trim_leading(bucket, @stream_prefix) + end {:ok, stream_names} else