Skip to content

Optimize merging line coverage - #1121

Closed
splattael wants to merge 2 commits into
simplecov-ruby:mainfrom
splattael:optimize-merge_line_coverage
Closed

splattael wants to merge 2 commits into
simplecov-ruby:mainfrom
splattael:optimize-merge_line_coverage

Conversation

@splattael

@splattael splattael commented Jan 7, 2025 •

Copy link
Copy Markdown
Contributor

This PR optimizes merging line coverage:

  • Integer#to_i and NilClass#to_i showed up in stack profiler which resulted to slow down merge_line_coverage a lot
  • Array#zip created a lot of intermediate objects

These changes brought down the run time from 1m28s to 1m8s on a large resultset (GitLab).

Stack profile

Before

Screenshot from 2025-01-07 11-59-26

After

Screenshot from 2025-01-07 11-59-36

@splattael

Copy link
Copy Markdown
Contributor Author

👋 @amatsuda Do you mind reviewing this optimization?

splattael added 2 commits July 1, 2025 13:47
Prior this change `Integer#to_i` and `NilClass#to_i` showed up in
stack profiler which resulted to slow down `merge_line_coverage` a lot.

This change brought down the run time from 1m28s to 1m18s on a large
resultset (GitLab).
Create less intermediate objects.
@splattael

Copy link
Copy Markdown
Contributor Author

Closing this PR in favor of #1144.

This PR only slightly improves performance compared to #1144:

  • This PR: 15s -> 8s
  • 1144/now main: 15s -> 9s

main is good enough though 💪

@splattael splattael closed this Oct 29, 2025
@splattael

splattael commented Oct 29, 2025 •

Copy link
Copy Markdown
Contributor Author

I've tested main with the following diff:

diff --git a/lib/simplecov/combine/lines_combiner.rb b/lib/simplecov/combine/lines_combiner.rb
index cddffa5..97a21cc 100644
--- a/lib/simplecov/combine/lines_combiner.rb
+++ b/lib/simplecov/combine/lines_combiner.rb
@@ -32,12 +32,12 @@ module SimpleCov
       #
       # @return [Integer || nil]
       def merge_line_coverage(first_val, second_val)
-        sum = first_val.to_i + second_val.to_i
-
-        if sum.zero? && (first_val.nil? || second_val.nil?)
-          nil
-        else
-          sum
+        if first_val && second_val
+          first_val + second_val
+        elsif first_val
+          first_val.zero? ? nil : first_val
+        elsif second_val
+          second_val.zero? ? nil : second_val
         end
       end
     end

and it improved performance even more: 9s -> 6.8s.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant