Skip to content

Commit

Permalink
Workaround slow compile times for big StaticArray
Browse files Browse the repository at this point in the history
Fixes #2485
  • Loading branch information
j8r committed Jun 16, 2020
1 parent f584ff6 commit 0e971dd
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/static_array.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
# Unroll the loop in Crystal land to avoid slow compile times in release mode. See https://github.com/crystal-lang/crystal/issues/2485#issuecomment-644416515
{% for i in 0...N %}
buf[{{i}}] = yield {{i}}
{% end %}
array
end

Expand Down

0 comments on commit 0e971dd

Please sign in to comment.