From 38d3de7a2fca6085b7bb231fe1f1c907e360698a Mon Sep 17 00:00:00 2001 From: Florian Klampfer Date: Sun, 24 May 2020 14:14:42 +0700 Subject: [PATCH 1/8] Avoid unnecessary string building --- lib/jekyll-include-cache/tag.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/jekyll-include-cache/tag.rb b/lib/jekyll-include-cache/tag.rb index 418b19b..73730ab 100644 --- a/lib/jekyll-include-cache/tag.rb +++ b/lib/jekyll-include-cache/tag.rb @@ -39,7 +39,10 @@ def key(path, params) end def digest(path_hash, params_hash) - Digest::MD5.hexdigest("#{path_hash}#{params_hash}") + md5 = Digest::MD5.new + md5.update path_hash.to_s + md5.update params_hash.to_s + md5.hexdigest end end end From 52b108c7b203d57f003f9d9fab2c7980cb0dffc6 Mon Sep 17 00:00:00 2001 From: Florian Klampfer Date: Sun, 24 May 2020 14:15:03 +0700 Subject: [PATCH 2/8] Avoid expensive document hashing --- lib/jekyll-include-cache/tag.rb | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/jekyll-include-cache/tag.rb b/lib/jekyll-include-cache/tag.rb index 73730ab..761faae 100644 --- a/lib/jekyll-include-cache/tag.rb +++ b/lib/jekyll-include-cache/tag.rb @@ -33,11 +33,32 @@ def path(context) def key(path, params) path_hash = path.hash - params_hash = params.hash + params_hash = quick_hash(params) self.class.digest_cache[path_hash] ||= {} self.class.digest_cache[path_hash][params_hash] ||= digest(path_hash, params_hash) end + def quick_hash(params) + return params.hash unless params + + md5 = Digest::MD5.new + + params.each do |key, value| + # Using the fact that Jekyll documents don't change within a site build. + # Instead of calculating the hash of an entire document (expesive!) + # we just use the object id. + # This allows posts and pages to be passed to `include_cached` + # without a performance penality. + if value.is_a? Jekyll::Drops::Drop + md5.update value.object_id.to_s + else + md5.update value.hash.to_s + end + end + + md5.hexdigest + end + def digest(path_hash, params_hash) md5 = Digest::MD5.new md5.update path_hash.to_s From 76ee44a6758627889a0b27e979e61939ae28883b Mon Sep 17 00:00:00 2001 From: Florian Klampfer Date: Mon, 25 May 2020 12:51:50 +0700 Subject: [PATCH 3/8] Update comments --- lib/jekyll-include-cache/tag.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/jekyll-include-cache/tag.rb b/lib/jekyll-include-cache/tag.rb index 761faae..2b85eca 100644 --- a/lib/jekyll-include-cache/tag.rb +++ b/lib/jekyll-include-cache/tag.rb @@ -44,11 +44,9 @@ def quick_hash(params) md5 = Digest::MD5.new params.each do |key, value| - # Using the fact that Jekyll documents don't change within a site build. - # Instead of calculating the hash of an entire document (expesive!) - # we just use the object id. - # This allows posts and pages to be passed to `include_cached` - # without a performance penality. + # Using the fact that Jekyll documents don't change during a build. + # Instead of calculating the hash of an entire document (expensive!) + # we just use its object id. if value.is_a? Jekyll::Drops::Drop md5.update value.object_id.to_s else From 1a12142d14d4bf471e2fab976fc998f575f0458f Mon Sep 17 00:00:00 2001 From: Florian Klampfer Date: Mon, 25 May 2020 13:26:54 +0700 Subject: [PATCH 4/8] Fix code style --- lib/jekyll-include-cache/tag.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jekyll-include-cache/tag.rb b/lib/jekyll-include-cache/tag.rb index 2b85eca..6cac15f 100644 --- a/lib/jekyll-include-cache/tag.rb +++ b/lib/jekyll-include-cache/tag.rb @@ -43,7 +43,7 @@ def quick_hash(params) md5 = Digest::MD5.new - params.each do |key, value| + params.sort.each do |_, value| # Using the fact that Jekyll documents don't change during a build. # Instead of calculating the hash of an entire document (expensive!) # we just use its object id. From 0bdb8a074a02572c3e559c814bdcb272faeab2de Mon Sep 17 00:00:00 2001 From: Florian Klampfer Date: Mon, 25 May 2020 13:27:03 +0700 Subject: [PATCH 5/8] Update gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 831af70..016bf9e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,6 @@ spec/examples.txt *.gem Gemfile.lock /tmp +vendor/bundle +.bundle +.jekyll-cache From ae25b6082f730c35b2967541e59b3ae212018c01 Mon Sep 17 00:00:00 2001 From: Florian Klampfer Date: Tue, 26 May 2020 13:48:37 +0700 Subject: [PATCH 6/8] Fix tests --- spec/jekyll-include-tag/tag_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/jekyll-include-tag/tag_spec.rb b/spec/jekyll-include-tag/tag_spec.rb index 48a4924..c77e4db 100644 --- a/spec/jekyll-include-tag/tag_spec.rb +++ b/spec/jekyll-include-tag/tag_spec.rb @@ -29,20 +29,20 @@ it "builds the key" do key = subject.send(:key, "foo.html", "foo" => "bar", "foo2" => "bar2") expect(key).to eql( - subject.send(:digest, "foo.html".hash, { "foo" => "bar", "foo2" => "bar2" }.hash) + subject.send(:digest, "foo.html".hash, subject.send(:quick_hash, { "foo" => "bar", "foo2" => "bar2" })) ) end it "builds the key based on the path" do key = subject.send(:key, "foo2.html", "foo" => "bar", "foo2" => "bar2") expect(key).to eql( - subject.send(:digest, "foo2.html".hash, { "foo" => "bar", "foo2" => "bar2" }.hash) + subject.send(:digest, "foo2.html".hash, subject.send(:quick_hash, { "foo" => "bar", "foo2" => "bar2" })) ) end it "builds the key based on the params" do key = subject.send(:key, "foo2.html", "foo" => "bar") - expect(key).to eql(subject.send(:digest, "foo2.html".hash, { "foo" => "bar" }.hash)) + expect(key).to eql(subject.send(:digest, "foo2.html".hash, subject.send(:quick_hash, { "foo" => "bar" }))) end end From b2fa6d836d7dff1b5b86afe4d49f7aa3a4084419 Mon Sep 17 00:00:00 2001 From: Florian Klampfer Date: Tue, 26 May 2020 18:22:29 +0700 Subject: [PATCH 7/8] Fix test --- spec/spec_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b379b5a..5db79c8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -21,7 +21,7 @@ Jekyll.logger.adjust_verbosity(:quiet => true) -Jekyll::Cache.base_dir = File.expand_path("../tmp", __dir__) if defined? Jekyll::Cache +Jekyll::Cache.cache_dir = File.expand_path("../tmp", __dir__) if defined? Jekyll::Cache def fixture_path(fixture) File.expand_path "./fixtures/#{fixture}", File.dirname(__FILE__) From 1567ce6446e4186f106f7827ffdc944404b471bd Mon Sep 17 00:00:00 2001 From: Florian Klampfer Date: Tue, 26 May 2020 18:22:44 +0700 Subject: [PATCH 8/8] Fix code style --- spec/jekyll-include-tag/tag_spec.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/spec/jekyll-include-tag/tag_spec.rb b/spec/jekyll-include-tag/tag_spec.rb index c77e4db..21b7b3d 100644 --- a/spec/jekyll-include-tag/tag_spec.rb +++ b/spec/jekyll-include-tag/tag_spec.rb @@ -28,21 +28,24 @@ context "building the key" do it "builds the key" do key = subject.send(:key, "foo.html", "foo" => "bar", "foo2" => "bar2") + params = { "foo" => "bar", "foo2" => "bar2" } expect(key).to eql( - subject.send(:digest, "foo.html".hash, subject.send(:quick_hash, { "foo" => "bar", "foo2" => "bar2" })) + subject.send(:digest, "foo.html".hash, subject.send(:quick_hash, params)) ) end it "builds the key based on the path" do key = subject.send(:key, "foo2.html", "foo" => "bar", "foo2" => "bar2") + params = { "foo" => "bar", "foo2" => "bar2" } expect(key).to eql( - subject.send(:digest, "foo2.html".hash, subject.send(:quick_hash, { "foo" => "bar", "foo2" => "bar2" })) + subject.send(:digest, "foo2.html".hash, subject.send(:quick_hash, params)) ) end it "builds the key based on the params" do key = subject.send(:key, "foo2.html", "foo" => "bar") - expect(key).to eql(subject.send(:digest, "foo2.html".hash, subject.send(:quick_hash, { "foo" => "bar" }))) + params = { "foo" => "bar" } + expect(key).to eql(subject.send(:digest, "foo2.html".hash, subject.send(:quick_hash, params))) end end