Skip to content

Commit

Permalink
Avoid expensive document hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
qwtel committed May 24, 2020
1 parent 38d3de7 commit 52b108c
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion lib/jekyll-include-cache/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 52b108c

Please sign in to comment.