From 07a308ad1b3e4ac1c6a3d81a60dac5c4254281b9 Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Wed, 24 Jul 2019 14:08:25 -0700 Subject: [PATCH] Enable Rubocop Performance/ReverseEach `Array.reverse_each` is faster than `Array.reverse.each` because: * reverse.each creates a new array then loops each element * reverse_each loops in reverse order (no intermediate array created) --- .rubocop_todo.yml | 8 -------- app/models/commit.rb | 2 +- ...222051615_add_indexes_for_merge_request_diffs_query.rb | 2 +- lib/gitlab/profiler.rb | 2 +- 4 files changed, 3 insertions(+), 11 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 72de4f0b2aa..3898206e3b5 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -262,14 +262,6 @@ Naming/HeredocDelimiterNaming: Naming/RescuedExceptionsVariableName: Enabled: false -# Offense count: 3 -# Cop supports --auto-correct. -Performance/ReverseEach: - Exclude: - - 'app/models/commit.rb' - - 'db/migrate/20190222051615_add_indexes_for_merge_request_diffs_query.rb' - - 'lib/gitlab/profiler.rb' - # Offense count: 7081 # Configuration parameters: Prefixes. # Prefixes: when, with, without diff --git a/app/models/commit.rb b/app/models/commit.rb index be37fa2e76f..0889ce7e287 100644 --- a/app/models/commit.rb +++ b/app/models/commit.rb @@ -346,7 +346,7 @@ class Commit if commits_in_merge_request.present? message_body << "" - commits_in_merge_request.reverse.each do |commit_in_merge| + commits_in_merge_request.reverse_each do |commit_in_merge| message_body << "#{commit_in_merge.short_id} #{commit_in_merge.title}" end end diff --git a/db/migrate/20190222051615_add_indexes_for_merge_request_diffs_query.rb b/db/migrate/20190222051615_add_indexes_for_merge_request_diffs_query.rb index 0048268ca6f..bf7f7b44dec 100644 --- a/db/migrate/20190222051615_add_indexes_for_merge_request_diffs_query.rb +++ b/db/migrate/20190222051615_add_indexes_for_merge_request_diffs_query.rb @@ -35,7 +35,7 @@ class AddIndexesForMergeRequestDiffsQuery < ActiveRecord::Migration[5.0] end def down - INDEX_SPECS.reverse.each do |spec| + INDEX_SPECS.reverse_each do |spec| remove_concurrent_index(*spec) end end diff --git a/lib/gitlab/profiler.rb b/lib/gitlab/profiler.rb index 890228e5e78..615c0ec374c 100644 --- a/lib/gitlab/profiler.rb +++ b/lib/gitlab/profiler.rb @@ -166,7 +166,7 @@ module Gitlab [model, times.count, times.sum] end - summarised_load_times.sort_by(&:last).reverse.each do |(model, query_count, time)| + summarised_load_times.sort_by(&:last).reverse_each do |(model, query_count, time)| logger.info("#{model} total (#{query_count}): #{time.round(2)}ms") end end