Skip to content

Commit

Permalink
Avoid extra array allocation for where(x: [...])
Browse files Browse the repository at this point in the history
We do not need to construct an array of `nil`s; we merely need to
determine whether any are present in the values array and remove them.
Thus, we can use `compact!` instead of `extract!`.
  • Loading branch information
jonathanhefner committed Nov 9, 2023
1 parent 377f6f6 commit ebd9661
Showing 1 changed file with 2 additions and 2 deletions.
Expand Up @@ -13,7 +13,7 @@ def call(attribute, value)
return attribute.in([]) if value.empty?

values = value.map { |x| x.is_a?(Base) ? x.id : x }
nils = values.extract!(&:nil?)
nils = values.compact!
ranges = values.extract! { |v| v.is_a?(Range) }

values_predicate =
Expand All @@ -23,7 +23,7 @@ def call(attribute, value)
else Arel::Nodes::HomogeneousIn.new(values, attribute, :in)
end

unless nils.empty?
if nils
values_predicate = values_predicate.or(attribute.eq(nil))
end

Expand Down

0 comments on commit ebd9661

Please sign in to comment.