Eliminate quadratic behavior in SourceWatcher::poll_once!.

Both `Array#reject` and `Array#include?` have linear time
complexity. This results in quadratic time complexity when
`Array#include?` is called within `Array#reject`'s block. Using
`Array`'s difference operator gives the same result in linear time.

In a site with ~14,000 files, this drops the time taken by some calls to
`SourceWatcher::poll_once!` from ~15 seconds each to just a few
milliseconds.
This commit is contained in:
Mark Rowe 2016-08-11 00:24:42 -07:00
parent 7027b4933a
commit dcd36a4f99

View file

@ -199,7 +199,7 @@ module Middleman
Contract ArrayOf[Pathname] Contract ArrayOf[Pathname]
def poll_once! def poll_once!
updated = ::Middleman::Util.all_files_under(@directory.to_s, &method(:should_not_recurse?)) updated = ::Middleman::Util.all_files_under(@directory.to_s, &method(:should_not_recurse?))
removed = @files.keys.reject { |p| updated.include?(p) } removed = @files.keys - updated
result = update(updated, removed) result = update(updated, removed)