From ebd96610a9d54e51f0b40afd8946f4ac6538c6e3 Mon Sep 17 00:00:00 2001 From: Jonathan Hefner Date: Thu, 9 Nov 2023 16:11:09 -0600 Subject: [PATCH] Avoid extra array allocation for `where(x: [...])` 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!`. --- .../active_record/relation/predicate_builder/array_handler.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/relation/predicate_builder/array_handler.rb b/activerecord/lib/active_record/relation/predicate_builder/array_handler.rb index f07afd0d0f7a5..c8455799ad19d 100644 --- a/activerecord/lib/active_record/relation/predicate_builder/array_handler.rb +++ b/activerecord/lib/active_record/relation/predicate_builder/array_handler.rb @@ -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 = @@ -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