rubinius/rubinius — pull request #3066 by sferik (2014-06-18 13:22:28+00:00)
Title: Optimize flat_map to iterate once instead of twice

```ruby
require 'benchmark/ips'

ARRAY = [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]]
PROC = proc { |x| x }

def slow(enum, &block)
enum.map(&block).flatten(1)
end

def fast(enum, &block)
enum.inject([]) { |a, e| e.is_a?(Array) ? a.concat(block.call(e)) : a.push(block.call(e)) }
end

Benchmark.ips do |x|
x.report("slow") { slow(ARRAY, &PROC) }
x.report("fast") { fast(ARRAY, &PROC) }
end
```

```
slow 289362.8 (±7.2%) i/s - 1440698 in 5.004836s
fast 450063.1 (±6.7%) i/s - 2244970 in 5.010861s
```