Commit b64a933
committed
fix: bound is_chart_bar_cluster's cubic cost on dense rect clusters
bar_family (used by is_chart_bar_cluster to distinguish bar charts
from cell-rect grids) iterates every rect as an anchor, rebuilds a
same-breadth "family" per anchor, then runs a nested any/filter over
that family plus a per-member full-page item scan — O(n^3) in the
cluster size in the worst case.
Profiling a real fixture (tests/fixtures/bits_pilani_feedback.pdf, a
dense feedback-form PDF) with samply + macOS `sample` showed
is_chart_bar_cluster consuming ~25% of total runtime, taking the file
from a documented ~200ms to 43s. A dense form/checkbox-grid page
clusters into hundreds of touching rects via cluster_rects, which is
exactly the case that makes the O(n^3) term dominate.
No real bar chart has hundreds of bars, so this adds
MAX_CHART_CLUSTER_RECTS (300, mirroring the existing MAX_CLUSTER_RECTS
pattern in this file) as an early bailout — clusters above that size
are treated as "not chart-like" instead of running the expensive
checks, which costs no detections against realistic PDFs. Also hoists
the redundant per-item `it.page == page` filter out of the
per-rect-checked closure since it's invariant for the whole call.
Verified: the fixture drops from 43s to 4.5s (~9.5x); full cargo test
suite (721 tests, including a new regression test asserting the
bailout fires in <100ms on an oversized cluster) passes unchanged.1 parent b3d2158 commit b64a933
1 file changed
Lines changed: 49 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2006 | 2006 | | |
2007 | 2007 | | |
2008 | 2008 | | |
| 2009 | + | |
| 2010 | + | |
| 2011 | + | |
| 2012 | + | |
| 2013 | + | |
| 2014 | + | |
| 2015 | + | |
| 2016 | + | |
| 2017 | + | |
| 2018 | + | |
2009 | 2019 | | |
2010 | 2020 | | |
2011 | 2021 | | |
2012 | 2022 | | |
2013 | 2023 | | |
| 2024 | + | |
| 2025 | + | |
| 2026 | + | |
| 2027 | + | |
| 2028 | + | |
| 2029 | + | |
| 2030 | + | |
| 2031 | + | |
| 2032 | + | |
| 2033 | + | |
2014 | 2034 | | |
2015 | | - | |
| 2035 | + | |
2016 | 2036 | | |
2017 | 2037 | | |
2018 | 2038 | | |
2019 | | - | |
| 2039 | + | |
2020 | 2040 | | |
2021 | 2041 | | |
2022 | 2042 | | |
| |||
4668 | 4688 | | |
4669 | 4689 | | |
4670 | 4690 | | |
| 4691 | + | |
| 4692 | + | |
| 4693 | + | |
| 4694 | + | |
| 4695 | + | |
| 4696 | + | |
| 4697 | + | |
| 4698 | + | |
| 4699 | + | |
| 4700 | + | |
| 4701 | + | |
| 4702 | + | |
| 4703 | + | |
| 4704 | + | |
| 4705 | + | |
| 4706 | + | |
| 4707 | + | |
| 4708 | + | |
| 4709 | + | |
| 4710 | + | |
| 4711 | + | |
| 4712 | + | |
| 4713 | + | |
| 4714 | + | |
| 4715 | + | |
| 4716 | + | |
| 4717 | + | |
4671 | 4718 | | |
0 commit comments