evincarofautumn / kitten

A statically typed concatenative systems programming language.
http://kittenlang.org/
Other
1.09k stars 39 forks source link

Unbox vector elements #130

Closed evincarofautumn closed 7 years ago

evincarofautumn commented 9 years ago

At the moment, [1, 2, 3] :: [int] is represented as:

[type=K_VECTOR]
[data         ]-->[refcount]
                  [begin   ]----->[type=K_INT]
                  [end     ]---+  [data=1    ]
                  [capacity]-+ |  [type=K_INT]
                             | |  [data=2    ]
                             | |  [type=K_INT]
                             | |  [data=3    ]
                             | +->[...       ]
                             |    [...       ]
                             +--->

We can move the type tag into the vector data block to avoid repeating it for each element:

[type=K_UNBOXED_VECTOR]
[data             ]-->[refcount  ]
                      [type=K_INT]
                      [begin     ]----->[data=1    ]
                      [end       ]---+  [data=2    ]
                      [capacity  ]-+ |  [data=3    ]
                                   | +->[...       ]
                                   +--->

This requires changing the representation of K_NONE and K_SOME:

[type=K_NONE]    [type=K_SOME]
[data=0     ]    [data       ]-->[...]

To a generic K_OPTION with a nullable pointer:

[type=K_OPTION]    [type=K_OPTION]
[data=NULL    ]    [data         ]-->[...]

Similarly, choices should be restructured to place the tag in the KBox instead of in the type field. This shouldn’t be too much of a performance issue, since matching on choices usually needs to load the box anyway.