Skip to content

Explore mutability in the Core AST

Looking at the Core for e.g. idSetCprInfo is quite devastating. It allocates a new Id record and a new IdInfo record, thereby copying 15-20 words. That happens every time we update the analysis info of a single Id in a Core to Core pass, which is quite often. Also IdInfo makes for a visible chunk in basically every heap profile I looked at.

Now, allocation is quite cheap, I know. But you can't tell me that copying 20 words everytime it would suffice to modify a single word does not impact performance! Maybe we could split IdInfo into more sub-records, but I don't expect much of an improvement. So ideally, we'd allocate all IdInfos in some arena and mutate them in our analyses, possibly with some copy-on-write mechanism for vanillaIdInfo.

But mutation is very unidiomatic und risks losing referential transparency. Hence I propose to exploit Linear types for a safe abstraction, and hope that someone picks up https://github.com/simonmar/ghc-proposals/blob/mutable-fields/proposals/0000-mutable-fields.rst in time.

After having applied this to IdInfo, we may well apply it to the Core AST, although I haven't found a killer app there, yet. Maybe an algorithm like Counting Immutable Beans that allows re-use of heap cells becomes possible, because any pass can be sure that it "owns" the AST.

Anyway, lot's of stuff to explore in this area. Hopefully we can reap significant speedups this way.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information