Skip to content

Commit

Permalink
Validate cached attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldrapper committed Aug 10, 2024
1 parent 63cde9c commit 6ac15fd
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 27 deletions.
2 changes: 1 addition & 1 deletion bench.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/phlex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions lib/phlex/elements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 << "</#{tag}>"
else # without content block
buffer << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[attributes.hash] ||= __attributes__(attributes)) << "></#{tag}>"
buffer << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[attributes] ||= __attributes__(attributes)) << "></#{tag}>"
end
else # without attributes
if block # with content block
Expand Down Expand Up @@ -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
Expand Down
30 changes: 8 additions & 22 deletions lib/phlex/fifo.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6ac15fd

Please sign in to comment.