5 ms·
Same thing happens in Java - Integer objects are the same between -128 and 127.
by pedrosorio 9y ago
Same thing happens in Java - Integer objects are the same between -128 and 127.
- detaro 9y agoAnother common trick along those lines is pointer tagging, where you use some of the bits in a pointer/reference to store flags and if those are set, put data in there directly. E.g. you could on a 32-bit machine reserve the least significant bit to mark an integer, and if it is set the high 31 bits are treated as a 31-bit integer instead of a pointer. Since your objects are likely bigger than a byte, you don't actually need all 32 bits for addresses and thus don't even loose addressable memory space. On 64bit machines you can do it even more, since they are far from being able to use all bits for actual memory, and you might even fit a short string or other more complex type in there.
- perone 9y agoClever trick.
- saagarjha 9y agoHere's an interesting case study by Mike Ash on how the Objective-C runtime does tagged pointers for strings: https://mikeash.com/pyblog/friday-qa-2015-07-31-tagged-pointer-strings.html https://mikeash.com/pyblog/friday-qa-2015-07-31-tagged-point...
- stochastic_monk 9y agolua stores extra data in 64-bit pointers. I feel like I should take advantage of unused space in pointers myself (in C++). x86 uses only 48 of the 64 bits.
- saagarjha 9y agoJava does string interning too: all constant Strings are stored in an interned cache, as well as Strings that have manually been interned with String.intern() method (which makes teaching "don't use == to compare Strings" in AP Computer Science all the more difficult!)
- nih0 9y agoIt's a optimization strategy. Many languages have an configurable integer cache, including python.