3 ms·
Ruby follows this same model. See the macro FIXNUM_P()[0] and FIXNUM_FLAG[1]. Edit: Every ruby value is either an fixnum (invalid pointer marked with FIXNUM_FLA
by loeg 14y ago
Ruby follows this same model. See the macro FIXNUM_P()[0] and FIXNUM_FLAG[1]. Edit: Every ruby value is either an fixnum (invalid pointer marked with FIXNUM_FLAG), a pointer to a real ruby value, or possibly an invalid, sentinel pointer. The comment block starting at [2] is informative.
[0]: https://github.com/ruby/ruby/blob/cbd1f2571af6bf93d5c4d4c25bbb692aad90b864/include/ruby/ruby.h#L355 https://github.com/ruby/ruby/blob/cbd1f2571af6bf93d5c4d4c25b...
[1]: https://github.com/ruby/ruby/blob/cbd1f2571af6bf93d5c4d4c25bbb692aad90b864/include/ruby/ruby.h#L425 https://github.com/ruby/ruby/blob/cbd1f2571af6bf93d5c4d4c25b...
Edit: It is assumed that pointers will be at least 16-bit aligned and thus, an odd-value pointer is never valid. So they can safely re-use half of the value space for integer values. So, you get fast integer arithmetic, but you don't get the fast floating point math like you do with NaN-boxing.
[2]: https://github.com/ruby/ruby/blob/cbd1f2571af6bf93d5c4d4c25bbb692aad90b864/include/ruby/ruby.h#L383 https://github.com/ruby/ruby/blob/cbd1f2571af6bf93d5c4d4c25b...
- loeg 14y agoEdit-period has expired, so I'm replying to myself. The sentinel pointers work like this: the zero page is assumed to never point to a valid Ruby object (NULL through NULL+PAGE_SIZE-1), so they just use some otherwise "pointer" shaped numbers in that range as special values (nil, true, false).