From cc336319ffb58368dc344ccca0c2781bbeefa158 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Sun, 15 Jan 2023 14:22:06 +0100 Subject: [PATCH] Avoid `Array` allocation in `Channel.select(Tuple)` --- src/channel.cr | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/channel.cr b/src/channel.cr index 151972418012..c9107f00222c 100644 --- a/src/channel.cr +++ b/src/channel.cr @@ -431,6 +431,11 @@ class Channel(T) # * `StaticArray`: This avoids a heap allocation because we can dup a # static array on the stack. ops_locks = ops.dup + elsif ops.responds_to?(:to_static_array) + # If the collection type implements `to_static_array` we can create a + # copy without allocating an array. This applies to `Tuple` types, which + # the compiler generates for `select` expressions. + ops_locks = ops.to_static_array else ops_locks = ops.to_a end