Conversation

What's the most efficient representation of lexically scoped local contexts for a compiler in C/C++/Rust? Like, you enter a scope, add some variables, and, upon leaving, restore the state from before it was entered, all the while supporting lookups of variables at any depth.
6
9
So you want the context save/restore to be fast, lookups to be fast, sharing of the outer scopes... it doesn't seem like you can have it all. Using flat hash tables and cloning them and throwing them away every time is right out.
1
1
Fine-grained persistent structures are good at sharing and save/restore, but are full of allocations, indirections, and cache inefficiency. Coarse-grained ones seem like they'd degrade to being the same as a flat structure, because there aren't *that* many local variables.
2
1
So a flat hash table where you individually delete the new variables when exiting the scope seems like the least-bad option, but it leaves me with the nagging sense that something better must be Out There.
2
1
What I *really* want to do is push local variables onto a stack and then pop off an entire scope at a time, but then lookups ???
2
2
[I'm also not sure that that's /necessarily/ superior to storing a var->{struct-of-all-associated-things} mapping directly; most likely that depends on the actual usage patterns in the compiler.]
2
1
Let me know if you find a solution! I want it for Pikelet. Atm I do names->debruijn indices using the slow lookup, fast push/pop method in my elaborator. I do need persistent data structures for variable environments though :(
1
2