Crystal: Hash with initial_capacity calls `resize`

Created on 14 Sep 2019  路  2Comments  路  Source: crystal-lang/crystal

I noticed while running the profiling for this post that GC#realloc was getting invoked during calls to Hash#resize, made while populating hashes that were created with an initial_capacity.

Here is a minimized repro:

class MyHash < Hash(Int32, Int32)
  def resize : Nil
    puts "Resizing, current_size: #{size}"
    super
  end
end

size = 1000
hash = MyHash.new(initial_capacity: size)
size.times { |i| hash[i] = i }
# => Resizing, current_size: 512
pp hash.size
# => 1000

Every hash size I've tested this against on current master calls resize, but it doesn't happen in 0.30.1 so it appears to be new behavior.

bug topiccollection

Most helpful comment

Good catch, thanks!

All 2 comments

Good catch, thanks!

Perhaps there should also be a spec that resize is not immediately called like shown in this example

Was this page helpful?
0 / 5 - 0 ratings

Related issues

asterite picture asterite  路  3Comments

will picture will  路  3Comments

ArthurZ picture ArthurZ  路  3Comments

oprypin picture oprypin  路  3Comments

nabeelomer picture nabeelomer  路  3Comments