Crystal compiler memory experiments (- 13-18% total, - 30-40% heap) for fun…

…and maybe profit. Had some time to play around with the compiler and tried to reduce memory consumption.

Some results for three test subjects (compiler itself, Lucky, and one own app), measured on a 2021 MacBook Pro against d8d99aa

Build Max Heap Peak footprint Instructions Wall time
Compiler, cold −40% 5.46 → 4.52 GB (−17%) −9.3% 21.8 → 19.9 s (−8.4%)
Compiler, warm −36% 3.61 → 2.73 GB (−24%) −13.7% 14.0 → 12.4 s (−11.4%)
Lucky, cold −35% 2.63 → 2.29 GB (−13%) −4.3% 17.2 → 16.5 s (−4.4%)
Lucky, warm −43% 1.11 → 0.92 GB (−18%) −15.5% 5.7 → 5.0 s (−13.1%)
App, cold −33% 3.71 → 3.22 GB (−13%) −4.4% 18.2 → 17.1 s (−5.7%)
App, warm −32% 2.18 → 1.79 GB (−18%) −4.1% 11.3 → 10.6 s (−5.9%)

The patches are available here, and are experimental and unpolished. Some also (for now) break the Compiler API in small ways (e.g. returning Indexable instead of Array). No work put in to make them nice. Compiler newbie here, and I am also not sure if every one of them should really be applied… I would be very grateful im someone could have a look if these patches make sense and the results are reproducible.

Also, up to here it was fun. However, my motivation to put more work into something that will not get merged is somewhat limited, so please feel free to give an indication which ones make sense to you, and I will polish them and submit a PR for those.

What I tried:

# Commit Patch What it does
1 054f97253 Report memory use and each garbage collection in --stats and --gc-stats --stats prints the heap in use, the heap size and the process memory; --gc-stats adds a line per collection. Measurement only, no change to the compiler’s behavior.
2 6010b2ac1 Store nilable value fields of AST nodes without a union Stores always-positive Int32? fields as an Int32 with -1 for nil: 4 bytes instead of a 16-byte union.
3 9eb30e53c Store small properties of AST nodes in bit fields Adds property_flag, which packs Bools into 1 bit and enums into as few bits as they need in one @flags field.
4 610f832ee Store more properties of AST nodes in bit fields Moves the flags of Def and other nodes into the bit field and widens @flags to 32 bits.
5 273d2f4c0 Store the special variables a def assigns as bit fields Replaces the Set(String)? of Def#special_vars with two flags for $~ and $?. Def and External get 16 bytes smaller.
6 ee72f03d4 Store the definition a def overrides without its metadata Def#previous stores the overridden Def instead of a 32-byte DefWithMetadata? and builds the metadata on request.
7 20473b097 Store rarely used fields of Call in an extension object Moves fields that few calls use into Call::Extension, allocated on first use. Call gets 64 bytes smaller.
8 d02d07dde Store rarely used fields of Def in an extension object Does the same for Def with Def::Extension; extension_property moves to ASTNode. Def gets 96 bytes smaller.
9 7eb40e587 Keep dependencies and observers in two shared slots Extend on the existing SmallNodeList optimization by sharing link slots between dependencies and observers, to allocate less arrays.
10 a74b96ba6 Replace NoReturnFilter with a node that holds the variable NoReturnFilteredNode holds the variable itself, so an if with an unreachable branch no longer allocates a filter object per variable.
11 a0d28d0e0 Store a single target def in the call without an array A call stores its only target def directly and keeps an array only for several.
12 a6a179698 Store a single name and one target in Path A Path with one name stores the string itself and keeps an array only for several names.
13 532d2eb27 Store the enclosing call in a third link slot The rarely set enclosing call moves into a third slot shared with dependencies and observers, replacing a field of every node.
14 0ec143424 Move the type binding state of nodes into ASTBase Adds ASTBase above ASTNode with everything except the location, so the nodes the type inference creates for itself go without one.
15 6d919bb1a Release the type bindings before codegen Clears all type bindings after the semantic phase, so codegen runs with that memory freed (with option to disable).
16 3a859e37f Release the visitors that calls, blocks and constants keep The release pass also drops the MainVisitor of each call and constant, and the binder and variables of each block (with option to disable).

There are a lot of good ideas there. Reducing the size of AST nodes is certainly a way to improve performance and reduce memory usage.

Some of the proposed ideas may not be ideal solutions, but they provide food for though.

I haven’t looked at everything, so just taking Store a single name and one target in `Path` · jochenseeber/crystal@a6a1796 · GitHub as an example: merging the mutually exclusive target properties of Path into one looks like a no-brainer. We could even consider adjusting the API and make this more transparent.
However, changing Path#names from Array(String) to String | Array(String) with a custom struct wrapper is perhaps not the best choice: paths with more than one component still allocate a large Array instance. If we go for immutability (which seems very reasonable to me), Slice(String) could be a more efficient choice.

Are compiler API changes ok? A lot of stuff e.g. should be Indexable instead of an array imo.