From 304c0e78bcb0af2a3ddf0aa1843bc376f083c12d Mon Sep 17 00:00:00 2001 From: Julien Reichardt Date: Tue, 16 Jun 2020 00:18:58 +0200 Subject: [PATCH] Fix slow compile times for big StaticArray Fixes #2485 --- src/static_array.cr | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/static_array.cr b/src/static_array.cr index 646b31da09a0..b8a32eff0070 100644 --- a/src/static_array.cr +++ b/src/static_array.cr @@ -62,11 +62,13 @@ struct StaticArray(T, N) # ``` # StaticArray(Int32, 3).new { |i| i * 2 } # => StaticArray[0, 2, 4] # ``` - def self.new(&block : Int32 -> T) + def self.new(& : Int32 -> T) array = uninitialized self - N.times do |i| - array.to_unsafe[i] = yield i - end + buf = array.to_unsafe + # Using a macro prevents slow compile times: https://github.com/crystal-lang/crystal/issues/2485 + {% for i in 0...N %} + buf[{{i.id}}] = yield {{i.id}} + {% end %} array end