diff --git a/CHANGELOG.md b/CHANGELOG.md index 59728345..ed25cfaf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Unreleased +* (patch) RSpec: Speed up the time tracker formatter by recording test path timings in place instead of rebuilding the whole collection after each top-level example group. + ### 10.0.1 * Add support for [File Paths Encryption](https://docs.knapsackpro.com/ruby/encryption/) to [Retry only Failures](https://docs.knapsackpro.com/ruby/retry-only-failures/). diff --git a/lib/knapsack_pro/formatters/time_tracker.rb b/lib/knapsack_pro/formatters/time_tracker.rb index 01baabd6..d277ddab 100644 --- a/lib/knapsack_pro/formatters/time_tracker.rb +++ b/lib/knapsack_pro/formatters/time_tracker.rb @@ -81,7 +81,7 @@ def example_group_finished(notification) add_hooks_time(@group, @time_all_by_group_id_path) @time_all_by_group_id_path = Hash.new(0) - @paths = merge(@paths, @group) + merge_into(@paths, @group) @group = {} end @@ -198,6 +198,18 @@ def merge(h1, h2) end end + # `merge` rebuilds the whole accumulator, which is quadratic when called + # once per top level group, so accumulate in place instead. + def merge_into(accumulator, other) + other.each do |path, example| + if accumulator.key?(path) + accumulator[path][:time_execution] += example[:time_execution] + else + accumulator[path] = example + end + end + end + def now KnapsackPro::Utils.time_now end