Super-flat ASTs

54 points by asb


LesleyLai

Nice post! I am vaguely aware of the flat AST idea from the Cornell blog post and from various talks of Carbon and Zig. But storing children contiguously is something I've never thought about. Definitely something to explore in my future language projects.

This is close to how we typically store a BVH (Bounding Volume Hierarchies, a kind of binary search tree to accelerate spatial queries) node in graphics:

struct BVHNode {
  AABB aabb; // bound of the node

  // either refers to the index of the first child or the first primitive, assume they are contiguous
  u32 first_child_or_primitive = 0;

  // If primitive count is 0, we have an inner node
  u32 primitive_count = 0;
};

I think I first seen this layout in How to build a BVH – Part 1: Basics, but PBRT also uses something similar

mitchellh

For a good example of this sort of pattern in the real world, take a look at the Zig compiler source code. I'm sure others might do it but Zig definitely does. I have a now very outdated series on some of the Zig internals: https://mitchellh.com/zig/parser And Andrew's old DoD talk is very good and relevant to this: https://vimeo.com/649009599

More generally, I believe its fair to call this a form of handle-based designs: https://en.wikipedia.org/wiki/Handle_(computing) Which are EXTREMELY useful for a variety of reasons and imo woefully underused above the lowest system level.

icefox

Lovely work, storing children contiguously is indeed a nice touch!

If the author's in the mood for a follow-up I'd love benchmarks for traversals/transformations as well as just construction. ...Ooh, the source is on github, I might have just nerd-sniped myself...

conartist6

Flat trees are interesting, but if the tree needs to support incremental changes is that still possible? It seems like they only way to change a bit of the tree then it's to make a whole new tree.