Skip to content

Commit

Permalink
fix: Avoid scanning and building entries for silenced directories
Browse files Browse the repository at this point in the history
Building entries that will be ignored by the listener degrades the
initialization performance, and can greatly increase memory consumption.

Specially on projects with large .git or node_modules directories.
  • Loading branch information
ElMassimo authored and ColinDKelley committed Aug 14, 2021
1 parent 0f2662a commit 8b17254
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/listen/adapter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def configure
# TODO: separate config per directory (some day maybe)
change_config = Change::Config.new(config.queue, config.silencer)
config.directories.each do |dir|
record = Record.new(dir)
record = Record.new(dir, config.silencer)
snapshot = Change.new(change_config, record)
@snapshots[dir] = snapshot
end
Expand Down
5 changes: 4 additions & 1 deletion lib/listen/record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ class Record

attr_reader :root

def initialize(directory)
def initialize(directory, silencer)
@tree = _auto_hash
@root = directory.to_s
@silencer = silencer
end

def add_dir(rel_path)
Expand Down Expand Up @@ -98,6 +99,8 @@ def _fast_unset_path(dirname, basename)

def _fast_build_dir(remaining, symlink_detector)
entry = remaining.pop
return if @silencer.silenced?(entry.record_dir_key, :dir)

children = entry.children # NOTE: children() implicitly tests if dir
symlink_detector.verify_unwatched!(entry)
children.each { |child| remaining << child }
Expand Down
7 changes: 5 additions & 2 deletions spec/lib/listen/record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

RSpec.describe Listen::Record do
let(:dir) { instance_double(Pathname, to_s: '/dir') }
let(:record) { Listen::Record.new(dir) }
let(:silencer) { Listen::Silencer.new }
let(:record) { Listen::Record.new(dir, silencer) }

def dir_entries_for(hash)
hash.each do |dir, entries|
Expand Down Expand Up @@ -308,9 +309,11 @@ def record_tree(record)

context 'with subdir containing files' do
before do
real_directory('/dir' => %w[dir1 dir2])
real_directory('/dir' => %w[dir1 dir2 .git])
real_directory('/dir/.git' => %w[FETCH_HEAD])
real_directory('/dir/dir1' => %w[foo])
real_directory('/dir/dir1/foo' => %w[bar])
lstat(file('/dir/.git/FETCH_HEAD'))
lstat(file('/dir/dir1/foo/bar'))
real_directory('/dir/dir2' => [])
end
Expand Down

0 comments on commit 8b17254

Please sign in to comment.