Skip to content

Commit 4e80438

Browse files
authored
Improve performance of computing indexables (#1129)
* Avoid searching LOAD_PATH for entries that are related to the same pattern * Avoid using Pathname#relative_path_from when instantiating indexable paths
1 parent c2875f4 commit 4e80438

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

lib/ruby_indexer/lib/ruby_indexer/configuration.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,12 @@ def indexables
6767

6868
# Add user specified patterns
6969
indexables = @included_patterns.flat_map do |pattern|
70+
load_path_entry = T.let(nil, T.nilable(String))
71+
7072
Dir.glob(pattern, File::FNM_PATHNAME | File::FNM_EXTGLOB).map! do |path|
71-
load_path_entry = $LOAD_PATH.find { |load_path| path.start_with?(load_path) }
73+
# All entries for the same pattern match the same $LOAD_PATH entry. Since searching the $LOAD_PATH for every
74+
# entry is expensive, we memoize it for the entire pattern
75+
load_path_entry ||= $LOAD_PATH.find { |load_path| path.start_with?(load_path) }
7276
IndexablePath.new(load_path_entry, path)
7377
end
7478
end

lib/ruby_indexer/lib/ruby_indexer/indexable_path.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class IndexablePath
2121
def initialize(load_path_entry, full_path)
2222
@full_path = full_path
2323
@require_path = T.let(
24-
load_path_entry ? Pathname.new(full_path).relative_path_from(load_path_entry).to_s.delete_suffix(".rb") : nil,
24+
load_path_entry ? full_path.delete_prefix("#{load_path_entry}/").delete_suffix(".rb") : nil,
2525
T.nilable(String),
2626
)
2727
end

0 commit comments

Comments
 (0)