diff --git a/bench.rb b/bench.rb index 442c8631..459db778 100755 --- a/bench.rb +++ b/bench.rb @@ -13,6 +13,6 @@ puts sample.bytesize Benchmark.ips do |x| - x.time = 20 + x.time = 5 x.report("Page") { Example::Page.new.call } end diff --git a/lib/phlex.rb b/lib/phlex.rb index 6bf158e1..8b32a751 100644 --- a/lib/phlex.rb +++ b/lib/phlex.rb @@ -24,7 +24,7 @@ module Phlex autoload :Unbuffered, "phlex/unbuffered" Escape = ERB::Escape - ATTRIBUTE_CACHE = FIFO.new(4_000_000) # 4MB + ATTRIBUTE_CACHE = FIFO.new SUPPORTS_FIBER_STORAGE = RUBY_ENGINE == "ruby" end diff --git a/lib/phlex/elements.rb b/lib/phlex/elements.rb index 2ff0ef17..1107bdf7 100644 --- a/lib/phlex/elements.rb +++ b/lib/phlex/elements.rb @@ -56,11 +56,11 @@ def #{method_name}(**attributes, &block) if attributes.length > 0 # with attributes if block # with content block - buffer << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[attributes.hash] ||= __attributes__(attributes)) << ">" + buffer << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[attributes] ||= __attributes__(attributes)) << ">" yield_content(&block) buffer << "" else # without content block - buffer << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[attributes.hash] ||= __attributes__(attributes)) << ">" + buffer << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[attributes] ||= __attributes__(attributes)) << ">" end else # without attributes if block # with content block @@ -113,7 +113,7 @@ def #{method_name}(**attributes) end if attributes.length > 0 # with attributes - buffer << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[attributes.hash] ||= __attributes__(attributes)) << ">" + buffer << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[attributes] ||= __attributes__(attributes)) << ">" else # without attributes buffer << "<#{tag}>" end diff --git a/lib/phlex/fifo.rb b/lib/phlex/fifo.rb index dd46c33d..2b8c8a4b 100644 --- a/lib/phlex/fifo.rb +++ b/lib/phlex/fifo.rb @@ -1,35 +1,21 @@ # frozen_string_literal: true class Phlex::FIFO - def initialize(max_bytesize) + def initialize(max_size = 1_000_000) @hash = {} - @mutex = Mutex.new - - @bytesize = 0 - @max_bytesize = max_bytesize + @max_size = max_size end - attr_reader :bytesize, :max_bytesize + attr_reader :size, :max_size def [](key) - @hash[key] + k, v = @hash[key.hash] + v if k == key end def []=(key, value) - @mutex.synchronize do - old_value = @hash.delete(key) - @hash[key] = value - - if old_value - @bytesize += value.bytesize - old_value.bytesize - else - @bytesize += value.bytesize - end - - while @bytesize > @max_bytesize - key, value = @hash.shift - @bytesize -= value.bytesize - end - end + hash = key.hash + @hash[hash] = [key, value] + @hash.shift while @hash.length > @max_size end end