What Are the Meanings of the Hash Keys When Calling Objectspace.Count_Objects

What is the meaning of :FREE in ObjectSpace.count_objects on ruby MRI

We can find the meaning of :FREE straight from the implementation itself (from gc.c )

*  The keys starting with +:T_+ means live objects.
* For example, +:T_ARRAY+ is the number of arrays.
* +:FREE+ means object slots which is not used now.
* +:TOTAL+ means sum of above.

Then we can take a look at the tests for it (from test_gc.rb) :

assert_equal(count[:TOTAL]-count[:FREE], stat[:heap_live_slots])
assert_equal(count[:FREE], stat[:heap_free_slots])

And finally, we can double check there's no funny business going on with:
GC.stat[:heap_free_slot] == ObjectSpace.count_objects[:FREE]

irb(main):001:0> GC.stat[:heap_free_slot] == ObjectSpace.count_objects[:FREE]
=> true

So, :FREE indicates the number of allocated slots on the heap that haven't been used.

Is there a performance difference between `select` and `select!` when called on a Ruby hash?

select! will perform better (I'll show the source for MRI, but it should be the same for the others).

The reason for this is that select needs to create a whole new Hash object, and will, for each entry in the hash, copy the entry - if the block succeeds.

On the other hand, select!, will, for each key, remove the value - if the block doesn't succeed - in-place (with no need for new object creation).



Related Topics



Leave a reply



Submit