2017-03-23 | Paul Boddie | raw annotate files changeset graph | Represent integers as __attr values with bit 0 set. Since pointers must be aligned (to four-byte boundaries on many modern systems, maybe two-byte boundaries on older or embedded systems, eight-byte boundaries on some 64-bit systems), any value with bit 0 set will not be referencing an object. This avoids allocating objects for integers and instead just allows them to be passed around. Bit 0 needs to be tested when attributes are accessed, and for integers, a common instance is employed to provide any instance attributes, an instance table reference and a reference to the integer class. | integers-as-tagged-attrs |
1 class C: 2 def f(self, x, y, z): 3 return z 4 5 class D: 6 def f(self, a, b, c): 7 return c 8 9 def xyz(obj): 10 return obj.f(1, 2, z=3) 11 12 def abc(obj): 13 return obj.f(4, 5, c=6) 14 15 c = C() 16 d = D() 17 18 print xyz(c) # 3 19 print abc(d) # 6 20 21 try: 22 print xyz(d) # should raise an exception 23 except TypeError: 24 print "xyz(d): argument cannot be used"