In the stack of the last lessons, every slot held one thing: a VALUE, one machine word. Here that is 32 bits. Most VALUEs are pointers to an object on the heap – but small things are packed into the word itself, and the lowest bits say which kind it is. Here are seven, bit by bit:
show_value(7, nil, true, false, :fox, "bacon", 1.5)- 7 is
0b1111= 15: an Integer that fits is stored shifted one bit left, with the lowest bit 1. That bit is the tag: "I am a Fixnum". So a small Integer is no object on the heap at all, and7.object_idis the word itself: 15. - nil, true, false are fixed words: 0x02, 0x06 and 0. On a 64-bit laptop
nilis 0x08 andtrue0x14 – a different table in the same header file, chosen byUSE_FLONUM. - 1.5: on 64 bits a Float fits into the word too (a "flonum", tag bits
10). On 32 bits there is no room:USE_FLONUMis 0, and every Float is an object on the heap. The picture shows a pointer.
You can measure both limits. ObjectSpace.memsize_of is 0 for what lives in the word:
require "objspace"
[2**30 - 1, 2**30, 1.5].map { |n| [n, ObjectSpace.memsize_of(n)] }230 − 1 is the largest Fixnum here (31 bits minus the sign); one more, and the Integer becomes a Bignum on the heap. On 64 bits the line is at 262 − 1. Everything that is not packed into the word lives in a slot on the heap. Slots come in five sizes, each twice the last – here 24, 48, 96, 192 and 384 bytes (40 to 640 on 64 bits). An object takes the smallest slot its contents fit into; what does not fit goes to memory from malloc, and the slot only points there:
show_slots("fox", "a" * 40, "a" * 200, "a" * 400, [1, 2, 3], { fox: 1 })"Embedded" means: the characters are inside the slot itself, one memory access away. The 400-character string no longer fits into the biggest slot, so its slot holds a pointer to the characters. A Hash has a trick of its own: up to a few entries it keeps them in a small array (an "ar_table") and searches it from start to end; above that it switches to a real hash table. How many is "a few"? The bars show the Hash's size in bytes for 0 to 10 entries:
sizes = (0..10).to_h { |n| [n.to_s, ObjectSpace.memsize_of((1..n).to_h { |i| [i, i] })] }
show_bars(sizes, title: "Hash: bytes by number of entries")The jump comes at 5. Books say 8 – and on a 64-bit laptop they are right: the limit is RHASH_AR_TABLE_MAX_SIZE, defined as SIZEOF_VALUE, the size of a VALUE in bytes. Here that is 4. (The next jump, at 9, is the real table growing.) ObjectSpace.dump tells you all of this about one object, as JSON:
require "json"
JSON.parse(ObjectSpace.dump("chunky bacon"))- The words of
nil,true, the tags – two tables, with and without flonum:special_consts.h:98;USE_FLONUMneeds 64 bits:include/ruby/internal/special_consts.h:45. object_idof an immediate is the word itself:rb_find_object_id:2163.- Five slot sizes:
HEAP_COUNT:190, the smallestBASE_SLOT_SIZE:677. The Hash's limit:RHASH_AR_TABLE_MAX_SIZE:17.
longest_embedded: try "a" * n for n = 0, 1, 2 … and look at "embedded" in its dump; give back the last n for which it was true.require "json"
require "objspace"
def embedded?(string)
JSON.parse(ObjectSpace.dump(string))["embedded"] == true
end
def longest_embedded
n = 0
# count n up while "a" * (n + 1) is still embedded
n
end
longest_embedded