diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2018-06-19 15:18:35 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-06-19 15:18:35 -0400 |
commit | 897946f2141662a072a8917946737218df82e204 (patch) | |
tree | 55f7b2e46f33072a36aec2fe432cdc2a52632b27 /activerecord/lib | |
parent | ee21e058424b2cb55bf74981c28b1ac0fb98b576 (diff) | |
parent | 13f0d6ebd38ad08f7550db784d64c5c6ab7f56f9 (diff) | |
download | rails-897946f2141662a072a8917946737218df82e204.tar.gz rails-897946f2141662a072a8917946737218df82e204.tar.bz2 rails-897946f2141662a072a8917946737218df82e204.zip |
Merge pull request #33074 from lsylvester/optimize-pluck
Reduce Memory Allocation when using .pluck
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/result.rb | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/result.rb b/activerecord/lib/active_record/result.rb index 11626c8e31..ffef229be2 100644 --- a/activerecord/lib/active_record/result.rb +++ b/activerecord/lib/active_record/result.rb @@ -97,12 +97,21 @@ module ActiveRecord end def cast_values(type_overrides = {}) # :nodoc: - types = columns.map { |name| column_type(name, type_overrides) } - result = rows.map do |values| - types.zip(values).map { |type, value| type.deserialize(value) } - end + if columns.one? + # Separated to avoid allocating an array per row + + type = column_type(columns.first, type_overrides) - columns.one? ? result.map!(&:first) : result + rows.map do |(value)| + type.deserialize(value) + end + else + types = columns.map { |name| column_type(name, type_overrides) } + + rows.map do |values| + Array.new(values.size) { |i| types[i].deserialize(values[i]) } + end + end end def initialize_copy(other) |