From c9d151ba667aa84ec08c14508a01f0071826eccb Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Wed, 22 May 2013 17:37:43 -0700 Subject: [PATCH] The file watcher knows which files exist, so don't use slow File.exists? Possible solution for #903 --- .../lib/middleman-core/core_extensions/file_watcher.rb | 7 +++++++ .../lib/middleman-core/core_extensions/front_matter.rb | 8 ++++---- .../lib/middleman-core/core_extensions/rendering.rb | 2 +- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/middleman-core/lib/middleman-core/core_extensions/file_watcher.rb b/middleman-core/lib/middleman-core/core_extensions/file_watcher.rb index 7bfd2ea2..b21fa123 100644 --- a/middleman-core/lib/middleman-core/core_extensions/file_watcher.rb +++ b/middleman-core/lib/middleman-core/core_extensions/file_watcher.rb @@ -56,6 +56,7 @@ module Middleman class API attr_reader :app + attr_reader :known_paths delegate :logger, :to => :app # Initialize api and internal path cache @@ -142,6 +143,12 @@ module Middleman reload_path(path, true) end + def exists?(path) + p = Pathname(path) + p = p.relative_path_from(Pathname(@app.root)) if !p.relative? + @known_paths.include?(p) + end + protected # Whether this path is ignored # @param [Pathname] path diff --git a/middleman-core/lib/middleman-core/core_extensions/front_matter.rb b/middleman-core/lib/middleman-core/core_extensions/front_matter.rb index 06514acd..e6d5e68e 100644 --- a/middleman-core/lib/middleman-core/core_extensions/front_matter.rb +++ b/middleman-core/lib/middleman-core/core_extensions/front_matter.rb @@ -59,9 +59,9 @@ module Middleman::CoreExtensions @cache[p] ||= begin in_file = frontmatter_and_content(p) + return in_file unless @app.files.exists?("#{path}.frontmatter") + external_file = frontmatter_and_content("#{p}.frontmatter") - - return in_file if external_file.nil? [ external_file[0].deep_merge(in_file[0]), @@ -145,8 +145,8 @@ module Middleman::CoreExtensions path end - return nil unless File.exists?(full_path) - + return nil unless @app.files.exists?(full_path) + data = {} content = nil diff --git a/middleman-core/lib/middleman-core/core_extensions/rendering.rb b/middleman-core/lib/middleman-core/core_extensions/rendering.rb index a5eb64f2..3275611b 100644 --- a/middleman-core/lib/middleman-core/core_extensions/rendering.rb +++ b/middleman-core/lib/middleman-core/core_extensions/rendering.rb @@ -476,7 +476,7 @@ module Middleman end # If we found one, return it and the found engine - if found_path || (File.exists?(on_disk_path) && !File.directory?(on_disk_path)) + if found_path || files.exists?(on_disk_path) engine = found_path ? File.extname(found_path)[1..-1].to_sym : nil [ found_path || on_disk_path, engine ] else