perf: Replace generator-based tree teardown and propagation with an explicit collection pass (#3981)