next.js
aef6ddc0 - Turbopack: Rewrite IntervalMap using a BTreeMap (#81095)

Commit
209 days ago
Turbopack: Rewrite IntervalMap using a BTreeMap (#81095) Changes: - Rewrote using a `BTreeMap`. This makes `update` `O(log n)`. It was previously `O(n)` which could give accidentally-quadratic time complexity when inserting `n` intervals. We don't think we have enough SST files for this to have been an issue, but how this data structure is used could change over time, and it was a bit risky. - There was some implicit `Option<T>` behavior in `IntervalMap` making it act like a sparse array. This made the type signature and behavior of some of its methods confusing. Removed that from `IntervalMap` and made the few callsites that actually need it explicitly use `Option<T>`. - Added a small `NaiveIntervalMap` implementation along with a narrowly bounded `TinyInt` type, and prove the correctness of `replace`, `update`, and `iter` by exhaustively comparing against it. - Added logic to merge adjacent intervals. This isn't really needed given how the map is currently used, but it's a possible optimization for some use-cases, and it helped align the behavior with `NaiveIntervalMap`. - Use `RangeInclusive<T>` or `impl RangeBounds<T>` instead of just `(u64, u64)` so that the inclusive/exclusive behaviors are always as explicit as possible. The implementation is not entirely optimal, but in part that's because it avoids using the nightly `btree_cursors` feature. However, the big-O time complexity should still be optimal. Some additional minor changes to `turbopack/crates/turbo-persistence/src/compaction/selector.rs`: - Avoid implicitly-overflowing `as u64` casts, prefer `from` and `try_from`. - Make `spread` return a `u128`, since that's slightly more correct than a `saturating_add`, the callsites were casting it to a `u128` anyways. - Add some more comments that are hopefully correct.
Author
bgw bgw
Parents
Loading