Enable `ChainArrayAllocation` Cop for import/export
In #205749 (closed) we found out that the ChainArrayAllocation
RoboCop rule is currently disabled (it is in fact disabled by default.)
However, this rule detects memory-inefficient use of Ruby collection transformations, which can lead to GC churn since collections are copied prior to transformation and then discarded again. This is particularly harmful to performance when done in loops, and when the collections are large.
Ex.:
$ bundle exec rubocop --only Performance/ChainArrayAllocation lib/gitlab/runtime.rb
Inspecting 1 file
C
Offenses:
lib/gitlab/runtime.rb:25:26: C: Performance/ChainArrayAllocation: Use unchained flatten! and uniq! (followed by return array if required) instead of chaining flatten...uniq.
res = arr.flatten.uniq.map { |x| x + 1 }
^^^^^
lib/gitlab/runtime.rb:25:31: C: Performance/ChainArrayAllocation: Use unchained uniq! and map! (followed by return array if required) instead of chaining uniq...map.
res = arr.flatten.uniq.map { |x| x + 1 }
^^^^
1 file inspected, 2 offenses detected
Because chaining transformations is idomatic Ruby, I would not advise to enable this Cop everywhere, but in areas such as import/export where we know memory to be a critical factor and in-memory data to grow large, we should always enable this Cop.
Edited by Matthias Käppler