2017-03-23 | Paul Boddie | file changeset files shortlog | 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 |
paul@2 | 1 | a = 4 |
paul@2 | 2 | |
paul@2 | 3 | def f(x): |
paul@266 | 4 | |
paul@266 | 5 | # Test function initialisation (f.$l0). |
paul@266 | 6 | |
paul@43 | 7 | def g(y, x=x): |
paul@266 | 8 | |
paul@266 | 9 | # Test function initialisation (f.$l0.$l0). |
paul@266 | 10 | |
paul@43 | 11 | def h(a, z, x=x, y=y): |
paul@43 | 12 | return x, y, z, a # parameter a overrides global scope |
paul@266 | 13 | |
paul@2 | 14 | return h |
paul@266 | 15 | |
paul@2 | 16 | return g |
paul@2 | 17 | |
paul@266 | 18 | fn = f(1) |
paul@266 | 19 | print fn # __main__.f.$l0 |
paul@266 | 20 | print fn(2) # __main__.f.$l0.$l0 |
paul@266 | 21 | print fn(2)(5, 3) # (1, 2, 3, 5) |
paul@266 | 22 | print fn(2)(5, 3, 6) # (6, 2, 3, 5) |
paul@266 | 23 | print fn(2)(5, 3, 6, 7) # (6, 7, 3, 5) |