3 ms·
In Racket, they use the alignment of the memory blocks: * The even integer numbers are pointers. * The odd integers represent fixnums (small integers) (n <--
by gus_massa 14y ago
In Racket, they use the alignment of the memory blocks:
* The even integer numbers are pointers.
* The odd integers represent fixnums (small integers) (n <---> 2*n+1).
With this representation the calculations with fixnums are quite fast because they don't need unboxing.
- apaprocki 14y agoIt probably isn't relevant to most people anymore, but misaligned memory access on certain architectures (e.g. Sparc) incurs a penalty.
- rayiner 14y agoThe tagging scheme mentioned above accounts for that. Objects are stored at aligned addresses, which leaves a couple or three bits available for signifying integers and other data types. On architectures that trap on unaligned accesses, you can actually use the trap to avoid an explicit type check if you're expecting a pointer and someone passes an integer.
- dfox 14y agoAlmost any implementation masks off the bit(s) before using the value as pointer (when they are not defined as zero for pointers as in this case :)) and stores the objects aligned in memory. As for performance penalty, unaligned memory accesses are (at least potentially) slower than aligned on almost any architecture. On some architectures they are simple not implemented in hardware and cause an exception (which includes SPARC and essentially anything purporting to be any kind of RISC) As for SPARC: SPARC actually even has hardware support for similar tagging mechanism: TADDcc and related instructions. Idea is that integers are taged by having two LSB's set to 00. These additional instructions do arithmetic as normal, but also set overflow flag (and optionally cause exception) when any of the operands have tag different from 00. Thus code can do the common case of fixnum arithmetic in one instruction (and possibly OF check) and only when overflow is detected fallback to slower codepath that handles fixnum->bignum overflows and non-fixnum datatypes.
- awda 14y agoStill totally relevant -- x86 misaligned memory accesses hurt.
- cygx 14y agoSee also the blue book (Smalltalk-80: The Language and its Implementation), keyword 'Small Integers'.
- loeg 14y agoRuby 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).