perf: special case single-child nodes in DiscrTree - #14805
Conversation
38be04a to
bbd620d
Compare
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as outdated.
This comment was marked as outdated.
|
Mathlib CI status (docs):
|
|
Reference manual CI status:
|
|
The adaptation PR for this PR is leanprover/downstream-lean4#23. |
1e5a4d6 to
3048b87
Compare
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as outdated.
This comment was marked as outdated.
8c23b20 to
08ea7af
Compare
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as duplicate.
This comment was marked as duplicate.
|
Related attempt: #12838 |
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as outdated.
This comment was marked as outdated.
|
Aah cool @nomeata! This is intentionally trying to be less ambitious, and @Vtec234 convinced me that there were some reasons to be cautious about the I think your separation of leaf nodes (values) from path/branch nodes also really makes sense, but from a "that's a good refactor that makes the data structure operations clearer" sense, I doubt it would be a huge optimization. |
a2d16ea to
020ac29
Compare
ca29358 to
d62a3fd
Compare
020ac29 to
9bd1c68
Compare
d62a3fd to
c6f7c1a
Compare
73da2fd to
4ffd4cf
Compare
This PR creates an additional view abstraction on `DiscrTree.Trie` nodes that allows nodes to be viewed and inspected without direct case analysis. This interface allows downstream code to tolerate future minor changes to DiscrTree representations (e.g. leanprover#12838 or leanprover#14805) more gracefully than is currently possible.
more tests, mapArrays fix and test cases better test coverage for basic discrimination tree options refactor getMatchLoop(s) for .chain Add public interface Trie.mkNode / Trie.asNode for downstream's sake formatting
4ffd4cf to
210a648
Compare
This comment was marked as resolved.
This comment was marked as resolved.
|
Benchmark results for 0ad131b against e991a05 are in. There are significant results. @robsimmons
Large changes (3🟥)
Medium changes (7✅)
Small changes (266✅, 4🟥)
|
|
downstream-lean4's mathlib radar shows a significant -104MB decrease in maxrss for |
This PR adds a new branch to the
DiscrTreedata structure, giving a memory-optimized way of representing discrimination tree keys with no values and a single node. The value.child k vis treated as equivalent to.node #[] #[(k, v)].Rationale: in many applications, many or most of the nodes in a trie are single-child-no-value-carrying nodes (the robot assistant claimed this was >80% in the case of the Simp post-tree for all of Mathlib, but I wouldn't stand behind that). Special-casing this common case is often an easy win for memory usage and for performance, and this PR is in part a generalization of the match optimization from #11889.
Memory usage reduction
The big change not captured in benchmarking is that this PR makes it ~9% less expensive, in terms of unshared memory usage, for a file worker to
import Lean, and about ~20% less expensive for a file worker toimport Mathlib. To easily see the ~9% improvement on a linux/x86 system, you can create a lean file Mem.leanand then run these commands:
(you may need to rerun it a few times until the Pss_Anon and Private_Dirty are close to each other, the number isn't stable right when the toolchain downloads for reasons I don't fully understand)
Full Glossy Claude Writeup if that interests you.