Peter Rong d0d0a665c2
[DWARFVerifier] rewrite DieRangeInfo::insert to remove O(N^2) loop (#185915)
We have a dSYM that is ~5.6 GB and has 69,840 compile units that took 3+
hours to verify (`dsymutil --verify-dwarf=auto`). This severely slowed
our build time. So I investigated by `perf record` for a few minutes and
found that `DWARFVerifier::DieRangeInfo::insert(const DieRangeInfo &RI)`
was consuming 83% of CPU time — 48% in `_Rb_tree_increment` (iterator
traversal) and 36% in the `insert` function itself.

It turns out the function was linearly scanning all children in a
std::set to check for range overlaps, when it could leverage the sorted
property of the set and check only the immediate neighbors after a O(log
N) insertion. The worst compile unit had ~189K DIEs, making this O(N²)
scan take over 4 minutes for a single unit.

The fix: insert into the sorted set first (O(log N)), then check only
the predecessor and successor for intersection. Since children are
verified non-overlapping as they are inserted incrementally, if the
immediate neighbors don't intersect, no other child can either.

Results:
- Full verification: 181 minutes → 3.5 minutes (~52x speedup)
- Worst single compile unit: 245 seconds → 1.7 seconds (146x)
- Unit test with N=100,000 inserts (old → new):
    - Forward order: 262s → 33ms
    - Reverse order: 288s → 18ms
    - Random order: 369s → 59ms

---------

Co-authored-by: J. Ryan Stinnett <ryan@convolv.es>
2026-03-13 11:06:25 -07:00
..