Skip to content

Commit

Permalink
Remove empty? call
Browse files Browse the repository at this point in the history
  • Loading branch information
fxn committed Oct 5, 2024
1 parent 284a6fb commit b5f2ade
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 21 deletions.
8 changes: 7 additions & 1 deletion lib/zeitwerk/core_ext/module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

module Zeitwerk::ExplicitNamespacesRegistry
def const_added(cname)
Zeitwerk::ExplicitNamespace.__on_const_added(self, cname)
# Module#const_added is triggered when an autoload is defined too. We are
# only interested in constants that are defined for real. In the case of
# inceptions we get a false nil, but this is covered in the loader by doing
# things in a certain order.
unless autoload?(cname, false)
Zeitwerk::ExplicitNamespace.__on_const_added(self, cname)
end
super
end

Expand Down
20 changes: 0 additions & 20 deletions lib/zeitwerk/explicit_namespace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,6 @@ class << self
#
# @sig (String) -> Zeitwerk::Loader?
private def loader_for(mod, cname)
# @cpaths.empty? is cheap and, depending on the code base, often true.
#
# Note that due to the way Zeitwerk works, namespaces are registered
# necessarily before their constant is defined, so the race codintion
# due to the gap from here to the delete call down below would not
# introduce a logic flaw.
#
# On one hand, if @cpaths is empty and a new entry is created after this
# check, it won't be for mod::cname anyway.
#
# On the other hand, if @cpaths is not empty, what happens with @cpaths
# during the gap is fine, because delete has the last word anyway.
return if @cpaths.empty?

# Module#const_added is triggered when an autoload is defined too. This
# callback is only for constants that are defined for real. In the case
# of inceptions we get a false nil, but this is covered in the loader by
# doing things in a certain order.
return if mod.autoload?(cname, false)

# I benchmarked this against using pairs [mod, cname] as keys, and
# strings won.
cpath = mod.equal?(Object) ? cname.name : "#{real_mod_name(mod)}::#{cname}"
Expand Down

0 comments on commit b5f2ade

Please sign in to comment.