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 l = (1, 2, 3, "four") 2 print len(l) # 4 3 print l[0] # 1 4 print l[1] # 2 5 print l[2] # 3 6 print l[3] # four 7 print l[-1] # four 8 print l[-2] # 3 9 print l[-3] # 2 10 print l[-4] # 1 11 print l # (1, 2, 3, "four") 12 13 l = [1, 2, 3, "four"] 14 t = tuple(l) 15 print t # (1, 2, 3, "four") 16 17 try: 18 print t[4] # should raise an exception 19 except IndexError, exc: 20 print "t[4]: failed with argument", exc.index 21 22 try: 23 print t[-5] # should raise an exception 24 except IndexError, exc: 25 print "t[-5]: failed with argument", exc.index