From 70ab198a33ee34bba7c1ce8f1152918a81316569 Mon Sep 17 00:00:00 2001 From: Leeingnyo Date: Thu, 16 Sep 2021 00:16:26 +0900 Subject: [PATCH] Add config 'forcely_yield_count' the default value 1000 would make a fiber yield on each 4ms on SSD Apply yield counter in Dir.contents_signauture Use contents_signature cache in Title.new --- src/config.cr | 1 + src/library/library.cr | 3 ++- src/library/title.cr | 6 +++--- src/util/signature.cr | 11 +++++++---- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/config.cr b/src/config.cr index 99fb5b9..bc7aa18 100644 --- a/src/config.cr +++ b/src/config.cr @@ -23,6 +23,7 @@ class Config property cache_enabled = false property cache_size_mbs = 50 property cache_log_enabled = true + property forcely_yield_count = 1000 property disable_login = false property default_username = "" property auth_proxy_header_name = "" diff --git a/src/library/library.cr b/src/library/library.cr index ea2a5e3..de8d7b4 100644 --- a/src/library/library.cr +++ b/src/library/library.cr @@ -133,8 +133,9 @@ class Library storage = Storage.new auto_close: false + count = Config.current.forcely_yield_count examine_context : ExamineContext = { - file_counter: (YieldCounter.new 1000), + file_counter: (YieldCounter.new count), cached_contents_signature: {} of String => String, deleted_title_ids: [] of String, deleted_entry_ids: [] of String, diff --git a/src/library/title.cr b/src/library/title.cr index ee1edd0..f57024c 100644 --- a/src/library/title.cr +++ b/src/library/title.cr @@ -19,7 +19,7 @@ class Title @[YAML::Field(ignore: true)] @cached_cover_url : String? - def initialize(@dir : String, @parent_id, cache : Hash(String, String)? = nil) + def initialize(@dir : String, @parent_id, cache = {} of String => String) storage = Storage.default @signature = Dir.signature dir id = storage.get_title_id dir, signature @@ -43,7 +43,7 @@ class Title next if fn.starts_with? "." path = File.join dir, fn if File.directory? path - title = Title.new path, @id + title = Title.new path, @id, cache next if title.entries.size == 0 && title.titles.size == 0 Library.default.title_hash[title.id] = title @title_ids << title.id @@ -73,7 +73,7 @@ class Title def examine(context : ExamineContext) : Bool return false unless Dir.exists? @dir # No title, Remove this contents_signature = Dir.contents_signature @dir, - context["cached_contents_signature"] + context["cached_contents_signature"], context["file_counter"] # Not changed. Reuse this return true if @contents_signature == contents_signature diff --git a/src/util/signature.cr b/src/util/signature.cr index d0a55a6..59a8311 100644 --- a/src/util/signature.cr +++ b/src/util/signature.cr @@ -54,9 +54,11 @@ class Dir # Rescan conditions: # - When a file added, moved, removed, renamed (including which in nested # directories) - def self.contents_signature(dirname, cache = {} of String => String) : String - return cache[dirname] if !cache.nil? && cache[dirname]? - Fiber.yield # Yield first + def self.contents_signature(dirname, + cache = {} of String => String, + counter : YieldCounter? = nil) : String + return cache[dirname] if cache[dirname]? + counter.count_and_yield unless counter.nil? signatures = [] of String self.open dirname do |dir| dir.entries.sort.each do |fn| @@ -69,10 +71,11 @@ class Dir # supported file signatures << fn if is_supported_file fn end + counter.count_and_yield unless counter.nil? end end hash = Digest::SHA1.hexdigest(signatures.join) - cache[dirname] = hash unless cache.nil? + cache[dirname] = hash hash end end