r/ProgrammingLanguages • u/BeamMeUpBiscotti • Jan 10 '26
Discussion ISO: literature on efficient representation of types in a compiler
I was told that one could reduce the memory usage in the semantic analysis stage by allocating each unique type in the type system once in heap memory, and making all instances of that type be pointers to that instance.
Classes are typically already represented this way, but this would apply to other types like unions. So instead of representing a union type as (for example) an array containing the constituent types, you would create a single instance of every possible union in the program in the heap, and any occurrence of that union would just be a pointer to that instance.
That way, total memory usage is lower and there is less time spent allocating memory or copying data.
Does anyone know of existing literature, blogs, or examples of this approach? I'd also be curious to hear any potential drawbacks/pitfalls, aside from implementation complexity.
TIA!
14
u/ianzen Jan 10 '26 edited Jan 10 '26
Yes, there is a very general technique of “hashconsing”.
https://github.com/backtracking/ocaml-hashcons
Two objects constructed through hashconsing will be pointers to the same underlying object if they are semantically equal.
6
u/WittyStick Jan 10 '26 edited Jan 10 '26
For unions you typically need to give an order to their constituent types, because we want
A | Bto be semantically the same type asB | A. If you sort the members with a total ordering, concatenate the sorted IDs and then hash that, you can generate a key and use a hashtable to store the type info.Another technique, related to hashing, is to use a Bloom Filter, but this may require larger IDs, and may give false positives.
The bloom filter has some useful properties. The order we specify the constituent types doesn't matter, they will produce the same filter.
The bitwise
orof two filters is equal to a filter created by adding the two items:We can also use bitwise
andfor intersection types, though this is a bit weaker and may give more false positives.Since false positives are possible, we would still need to store the constituent type information in the type and check the constituents when the bloom filter check passes. The bloom filter just skips having to unnecessarily resolve the type and perform this check in the majority of cases where the types don't match - we only need to perform some trivial bitwise operations which have very low cost. Essentially it says either "this type might match" or "this type definitely does not match".
2
u/whothewildonesare Jan 10 '26 edited Jan 10 '26
You can do it with ordinary interning or caching, a bit like you would for Strings.
In my compiler I use a database-like architecture, with an array of each different kind of entity in the compiler, including types. Then hash-maps which map an index into a particular array to some other index or object.
One of the hash-maps maps type objects to type indices, thereby implementing a bidirectional lookup. If you want index -> type, you use the array, and if you want type -> index you use the map. Before you insert into the array, you see if there’s a type in the map, and take that index instead.
It is probably not that time efficient but it gives you relative memory efficiency with trivial implementation complexity.
Providing you don’t store the names of types on their objects, you also in effect get structural typing for free, which is still useful (say, for pointers) in nominal languages.
2
u/Breadmaker4billion Jan 10 '26
I kinda winged my approach to this, but basically, once you have a procedure for deciding if two types are equal, you can make sure your list of types is unique. To speed things up, I also made a hash for types, so that a look up that a type is unique could be done using a hashmap.
Once all types are identified by their ID, comparison is super cheap. It also opens up the possibility of carrying type information to the runtime, for example, if your language has a top type. Debugging can be easier too, since now you have a relation of all types your program generates, inference tests can be automated slightly.
edit: Their ID is basically the index of the type in the global type list, it's nothing fancy.
0
u/kwan_e tonal-lang Jan 11 '26
Surely this is basic data-structures/design-patterns stuff? You might as well just read any of the many design patterns books.