rails/rails — pull request #14240 by sferik (2014-03-01 07:02:53+00:00)
Title: Replace map.flatten(1) with flat_map

Using `flat_map` is equivalent `map.flatten(1)` but simpler and an order of magnitude faster.

```ruby
require 'benchmark'

array = 10_000.times.map { (0..999).to_a }

Benchmark.bm 8 do |x|
x.report('#flatten') { array.map { |x| x }.flatten(1) }
x.report('#flat_map') { array.flat_map { |x| x } }
end
```
user system total real
#flatten 0.460000 0.030000 0.490000 ( 0.529242)
#flat_map 0.030000 0.030000 0.060000 ( 0.049880)

There are many other instances of `map.flatten` (without the `1` argument) in this codebase, that may or may not be safe to replace with `flat_map`, since `flat_map` isn’t recursive like `flatten`. I can work on a separate pull request that replaces those cases but this seemed like a quick and low-risk win. :smile_cat: