rails/activesupport/lib/active_support
Ricardo Díaz de154095ed Replace usage of Array#? with Array#intersect? for efficiency
`Array#intersect?` was introduced in Ruby 3.1.0 and it's more efficient and
useful when the result of the intersection is not needed as the
following benchmarks show:

```
require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  gem "rails", path: "./"
  # If you want to test against edge Rails replace the previous line with this:
  # gem "rails", github: "rails/rails", branch: "main"
  gem "benchmark-ips"
end

require "active_support"

SCENARIOS = [
  [(1..100).to_a, (90..200).to_a],    # Case 1
  [("a".."m").to_a, ("j".."z").to_a], # Case 2
  [(1..100).to_a, (101..200).to_a],   # Case 3
]

SCENARIOS.each_with_index do |values, n|
  puts
  puts " Case #{n + 1} ".center(80, "=")
  puts
  Benchmark.ips do |x|
    x.report("Array#?") { !(values[0] & values[1]).empty? }
    x.report("Array#intersect?")      { values[0].intersect?(values[1]) }
    x.compare!
  end
end
```

Results:

```
==================================== Case 1 ====================================

ruby 3.2.2 (2023-03-30 revision e51014f9c0) [arm64-darwin21]
Warming up --------------------------------------
             Array#?    34.221k i/100ms
    Array#intersect?    62.035k i/100ms
Calculating -------------------------------------
             Array#?    343.119k (± 1.1%) i/s -      1.745M in   5.087078s
    Array#intersect?    615.394k (± 1.1%) i/s -      3.102M in   5.040838s

Comparison:
    Array#intersect?:   615393.7 i/s
             Array#?:   343119.4 i/s - 1.79x  slower

==================================== Case 2 ====================================

ruby 3.2.2 (2023-03-30 revision e51014f9c0) [arm64-darwin21]
Warming up --------------------------------------
             Array#?   103.256k i/100ms
    Array#intersect?   185.104k i/100ms
Calculating -------------------------------------
             Array#?      1.039M (± 1.3%) i/s -      5.266M in   5.066847s
    Array#intersect?      1.873M (± 1.6%) i/s -      9.440M in   5.041740s

Comparison:
    Array#intersect?:  1872932.7 i/s
             Array#?:  1039482.4 i/s - 1.80x  slower

==================================== Case 3 ====================================

ruby 3.2.2 (2023-03-30 revision e51014f9c0) [arm64-darwin21]
Warming up --------------------------------------
             Array#?    37.070k i/100ms
    Array#intersect?    41.438k i/100ms
Calculating -------------------------------------
             Array#?    370.902k (± 0.8%) i/s -      1.891M in   5.097584s
    Array#intersect?    409.902k (± 1.0%) i/s -      2.072M in   5.055185s

Comparison:
    Array#intersect?:   409901.8 i/s
             Array#?:   370902.3 i/s - 1.11x  slower
```
2024-01-05 15:15:30 -05:00
..
cache Improve RedisCacheStore docs 2023-12-30 23:38:38 -05:00
concurrency Make AbstractAdapter#lock thread local by default 2022-11-23 14:34:40 +01:00
core_ext Replace usage of Array#? with Array#intersect? for efficiency 2024-01-05 15:15:30 -05:00
current_attributes Revert "Call Executor#wrap around each test" 2021-10-28 00:35:07 +02:00
dependencies Remove all code to work with Ruby < 3.1 2024-01-03 19:02:31 +00:00
deprecation Improve Deprecation API docs and guide 2023-12-14 17:10:44 -05:00
duration Replace usage of Array#? with Array#intersect? for efficiency 2024-01-05 15:15:30 -05:00
error_reporter Add raise_on_invalid_cache_expiration_time config to ActiveSupport::Cache::Store 2022-08-30 10:10:28 -05:00
execution_context Extract ActiveSupport::ExecutionContext out of ActiveRecord::QueryLogs 2021-11-10 09:36:02 +01:00
executor Call Executor#wrap around each test 2021-10-28 15:18:29 +02:00
inflector Ensure {down,up}case_first returns non-frozen string 2023-10-29 12:28:06 -05:00
json [FIX] Fix Activesupport json encode for hash 2023-12-30 20:22:11 +05:30
locale add zetta to number_to_human_size 2023-03-28 19:50:13 +09:00
log_subscriber Add Oxford commas [ci-skip] 2022-02-21 11:11:11 -06:00
message_pack Fix AS::MessagePack with ENV["RAILS_MAX_THREADS"] 2023-10-01 15:22:05 -05:00
messages Fix decoding data encoded using a non-String purpose 2023-10-17 10:02:10 +02:00
multibyte Add missing headers to Active Support docs [ci-skip] 2023-04-23 16:02:56 +02:00
notifications Point rubocop to ruby 3.1 2024-01-03 19:02:32 +00:00
number_helper Handle negative numbers in NumberToHumanSizeConverter 2023-10-26 18:07:15 +02:00
testing Fix Time.now/DateTime.now/Date.today to return results in a system timezone after #travel_to 2023-12-02 10:05:31 +01:00
values 🔗 Remove RDoc auto-link from Rails module everywhere 2023-06-23 10:49:30 +09:00
xml_mini Change load error messages to use Kernel#warn instead of $stderr.puts 2023-07-21 00:38:12 +03:00
actionable_error.rb Replace all occurrences of '<tt>(\w+::\w+)</tt>' with '+$1+' 2023-05-25 06:52:32 +09:00
all.rb
array_inquirer.rb Add missing headers to Active Support docs [ci-skip] 2023-04-23 16:02:56 +02:00
backtrace_cleaner.rb Better example for backtrace filter add_filter 2023-10-16 17:38:18 +02:00
benchmarkable.rb Add missing headers to Active Support docs [ci-skip] 2023-04-23 16:02:56 +02:00
broadcast_logger.rb Fix BroadcastLogger#dup so that it duplicates the logger's broadcasts. 2023-10-23 18:12:28 -04:00
builder.rb Change load error messages to use Kernel#warn instead of $stderr.puts 2023-07-21 00:38:12 +03:00
cache.rb Merge pull request #50498 from skipkayhil/hm-doc-write-options 2023-12-31 00:45:26 -05:00
callbacks.rb Eager load ActiveSupport::Callback procs 2023-11-07 16:19:00 -08:00
code_generator.rb Optimize CurrentAttributes method generation 2021-11-02 15:52:25 +01:00
concern.rb Replace all occurrences of '<tt>(\w+::\w+)</tt>' with '+$1+' 2023-05-25 06:52:32 +09:00
configurable.rb Add missing headers to Active Support docs [ci-skip] 2023-04-23 16:02:56 +02:00
configuration_file.rb Fix ruby-master test suite (Psych 4.0.0) 2021-05-19 14:21:21 +02:00
core_ext.rb Remove deprecated URI.parser 2021-11-19 21:43:23 +00:00
current_attributes.rb Add missing headers to Active Support docs [ci-skip] 2023-04-23 16:02:56 +02:00
deep_mergeable.rb Factor out deep_merge into AS::DeepMergeable 2022-06-20 11:40:17 -05:00
dependencies.rb Enable Style/ExplicitBlockArgument cop 2021-09-05 17:06:19 +02:00
deprecation.rb Move singleton require. 2023-12-30 12:02:01 +00:00
deprecator.rb Use the singleton instance as ActiveSupport deprecator 2023-03-14 17:33:48 +01:00
descendants_tracker.rb Remove all code to work with Ruby < 3.1 2024-01-03 19:02:31 +00:00
digest.rb Standardize nodoc comments 2021-07-29 21:18:07 +00:00
duration.rb Optimize many delegated methods on ActiveSupport::Duration 2023-12-08 15:41:05 +01:00
encrypted_configuration.rb Fix inconsistencies writing credentials values 2023-07-21 11:32:57 +02:00
encrypted_file.rb Use a temporary file for storing unencrypted files while editing 2023-08-22 10:23:15 -07:00
environment_inquirer.rb Fix anonymous evals 2023-01-11 11:22:42 +01:00
error_reporter.rb ErrorReporter#unexpected to report in production but raise in development 2023-11-20 09:42:56 +01:00
evented_file_update_checker.rb Remove require of ActiveSupport::ForkTracker 2023-10-07 23:29:02 -04:00
execution_context.rb Use IsolatedExecutionState across Active Support 2021-11-19 09:58:15 +01:00
execution_wrapper.rb Remove unnecessary require 2023-12-31 17:12:31 -03:00
executor.rb
file_update_checker.rb 🔗 Remove RDoc auto-link from Rails module everywhere 2023-06-23 10:49:30 +09:00
fork_tracker.rb Remove all code to work with Ruby < 3.1 2024-01-03 19:02:31 +00:00
gem_version.rb Development of Rails 7.2 starts now 2023-09-27 03:59:11 +00:00
gzip.rb Add missing headers to Active Support docs [ci-skip] 2023-04-23 16:02:56 +02:00
hash_with_indifferent_access.rb Implement HashWithIndifferentAccess#to_proc 2023-10-03 21:26:08 +03:00
html_safe_translation.rb Extract common behavior of html safe translation to a module 2021-10-12 18:03:46 +00:00
i18n_railtie.rb Remove old raise_on_missing_translations behaviour 2023-09-09 19:59:49 +00:00
i18n.rb Change load error messages to use Kernel#warn instead of $stderr.puts 2023-07-21 00:38:12 +03:00
inflections.rb
inflector.rb
isolated_execution_state.rb Copy over the IsolatedExecutionState in AC::Live 2022-02-21 11:40:52 +01:00
json.rb
key_generator.rb Don't show secrets for MessageVerifier#inspect and KeyGenerator#inspect 2023-07-06 21:51:22 +02:00
lazy_load_hooks.rb 🔗 Remove RDoc auto-link from Rails module everywhere 2023-06-23 10:49:30 +09:00
log_subscriber.rb ActiveSupport::LogSubscriber restore compatibility with SemanticLogger 2023-10-13 14:21:23 +02:00
logger_silence.rb Remove deprecated LoggerSilence constant 2020-10-30 00:25:50 +00:00
logger_thread_safe_level.rb Update requires in LoggerThreadSafeLevel 2023-09-24 21:59:55 -04:00
logger.rb Replace usage of Array#? with Array#intersect? for efficiency 2024-01-05 15:15:30 -05:00
message_encryptor.rb Don't show secrets for MessageEncryptor#inspect 2023-06-16 21:14:13 +02:00
message_encryptors.rb Ensure on_rotation appears in RDoc [ci-skip] 2023-10-07 12:26:20 -05:00
message_pack.rb Add ActiveSupport::MessagePack 2023-04-17 11:56:06 -05:00
message_verifier.rb Don't show secrets for MessageVerifier#inspect and KeyGenerator#inspect 2023-07-06 21:51:22 +02:00
message_verifiers.rb Ensure on_rotation appears in RDoc [ci-skip] 2023-10-07 12:26:20 -05:00
multibyte.rb Standardize nodoc comments 2021-07-29 21:18:07 +00:00
notifications.rb Stop building AS::Notifications::Event manually 2023-09-29 12:34:23 -04:00
number_helper.rb Use 2 spaces for identation in ActiveSupport code examples [ci-skip] 2022-11-16 22:44:22 +01:00
option_merger.rb Fix with_options bug when first argument is a Proc 2022-05-28 16:07:34 -04:00
ordered_hash.rb Replace all occurrences of '<tt>(\w+::\w+)</tt>' with '+$1+' 2023-05-25 06:52:32 +09:00
ordered_options.rb Add #to_s and pretty print for ActiveSupport::InheritableOptions 2023-11-28 17:49:15 -05:00
parameter_filter.rb Add missing headers to Active Support docs [ci-skip] 2023-04-23 16:02:56 +02:00
proxy_object.rb Add missing headers to Active Support docs [ci-skip] 2023-04-23 16:02:56 +02:00
rails.rb [ci skip] Small fix to ActiveSupport docs 2019-10-24 09:36:14 +02:00
railtie.rb Automatically eager load TZInfo 2023-11-09 18:44:27 +01:00
reloader.rb Add missing headers to Active Support docs [ci-skip] 2023-04-23 16:02:56 +02:00
rescuable.rb Add missing headers to Active Support docs [ci-skip] 2023-04-23 16:02:56 +02:00
secure_compare_rotator.rb Add missing headers to Active Support docs [ci-skip] 2023-04-23 16:02:56 +02:00
security_utils.rb secure_compare: Check byte size instead of length 2021-04-02 12:45:29 -04:00
string_inquirer.rb Add missing headers to Active Support docs [ci-skip] 2023-04-23 16:02:56 +02:00
subscriber.rb Replace all occurrences of '<tt>(\w+::\w+)</tt>' with '+$1+' 2023-05-25 06:52:32 +09:00
syntax_error_proxy.rb Addressed an issue where syntax errors generated inside an eval 2023-12-05 06:40:19 +00:00
tagged_logging.rb Add missing headers to Active Support docs [ci-skip] 2023-04-23 16:02:56 +02:00
test_case.rb Allow setting number of parallel workers to 1 2023-08-05 02:15:32 +00:00
time_with_zone.rb Specialize various #present? implementations 2023-11-04 08:45:51 +01:00
time.rb
version.rb Replace all occurrences of '<tt>(\w+::\w+)</tt>' with '+$1+' 2023-05-25 06:52:32 +09:00
xml_mini.rb Use double quotes more consistenly in doc and error messages 2023-10-28 11:38:49 +02:00