From 8c57404f25c4f9e43efb717582cec308e6bc1ffa Mon Sep 17 00:00:00 2001 From: Genar Trias Ortiz Date: Mon, 16 Dec 2024 13:43:04 +0100 Subject: [PATCH] Using sets instead of arrays and empty? instead of any? --- lib/zeitwerk/loader.rb | 10 +++++----- lib/zeitwerk/loader/config.rb | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/zeitwerk/loader.rb b/lib/zeitwerk/loader.rb index 2731dd2..2fbca6b 100644 --- a/lib/zeitwerk/loader.rb +++ b/lib/zeitwerk/loader.rb @@ -539,17 +539,17 @@ def all_dirs # If manual incepted namespaces are present we skip the default behavior # and we will only incept the manually defined namespaces for improved # performance - if manual_incepted_namespaces.any? - if manual_incepted_namespaces.include?(cref.cname.name) - Registry.register_inception(cref.path, abspath, self) - end - else + if manual_incepted_namespaces.empty? # This operation can impact performance in big codebases since it will # be evaluated for every single auoloaded constant. # See why in the documentation of Zeitwerk::Registry.inceptions. unless cref.autoload? Registry.register_inception(cref.path, abspath, self) end + else + if manual_incepted_namespaces.include?(cref.cname.name) + Registry.register_inception(cref.path, abspath, self) + end end end diff --git a/lib/zeitwerk/loader/config.rb b/lib/zeitwerk/loader/config.rb index 1b9aa4e..9aeb955 100644 --- a/lib/zeitwerk/loader/config.rb +++ b/lib/zeitwerk/loader/config.rb @@ -13,7 +13,7 @@ module Zeitwerk::Loader::Config # @sig #call | #debug | nil attr_accessor :logger - # @sig nil + # @sig Set[String] attr_accessor :manual_incepted_namespaces # Absolute paths of the root directories, mapped to their respective root namespaces: @@ -89,7 +89,7 @@ module Zeitwerk::Loader::Config def initialize @inflector = Zeitwerk::Inflector.new @logger = self.class.default_logger - @manual_incepted_namespaces = [] + @manual_incepted_namespaces = Set.new @tag = SecureRandom.hex(3) @initialized_at = Time.now @roots = {}