Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIFO cache #648

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ gem "rubocop"
gem "sus"
gem "benchmark-ips"
gem "yard"
gem "green_dots", github: "joeldrapper/green_dots"

group :test do
gem "i18n"
Expand Down
4 changes: 3 additions & 1 deletion lib/phlex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ module Phlex
autoload :Unbuffered, "phlex/unbuffered"
autoload :ConcurrentMap, "phlex/concurrent_map"
autoload :BlackHole, "phlex/black_hole"
autoload :LRU, "phlex/lru"
autoload :FIFO, "phlex/fifo"

# Included in all Phlex exceptions allowing you to match any Phlex error.
# @example Rescue any Phlex error:
Expand All @@ -37,7 +39,7 @@ class NameError < ::NameError
end

# @api private
ATTRIBUTE_CACHE = Phlex::ConcurrentMap.new
CACHE = Phlex::FIFO.new(20_000_000)
end

if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("3.0")
Expand Down
6 changes: 3 additions & 3 deletions lib/phlex/elements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ def #{method_name}(**attributes, &block)

if attributes.length > 0 # with attributes
if block # with content block
target << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[respond_to?(:process_attributes) ? (attributes.hash + self.class.hash) : attributes.hash] || __attributes__(**attributes)) << ">"
target << "<#{tag}" << (Phlex::CACHE[respond_to?(:process_attributes) ? (attributes.hash + self.class.hash) : attributes.hash] || __attributes__(**attributes)) << ">"
yield_content(&block)
target << "</#{tag}>"
else # without content block
target << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[respond_to?(:process_attributes) ? (attributes.hash + self.class.hash) : attributes.hash] || __attributes__(**attributes)) << "></#{tag}>"
target << "<#{tag}" << (Phlex::CACHE[respond_to?(:process_attributes) ? (attributes.hash + self.class.hash) : attributes.hash] || __attributes__(**attributes)) << "></#{tag}>"
end
else # without attributes
if block # with content block
Expand Down Expand Up @@ -77,7 +77,7 @@ def #{method_name}(**attributes)
target = @_context.target

if attributes.length > 0 # with attributes
target << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[respond_to?(:process_attributes) ? (attributes.hash + self.class.hash) : attributes.hash] || __attributes__(**attributes)) << ">"
target << "<#{tag}" << (Phlex::CACHE[respond_to?(:process_attributes) ? (attributes.hash + self.class.hash) : attributes.hash] || __attributes__(**attributes)) << ">"
else # without attributes
target << "<#{tag}>"
end
Expand Down
57 changes: 57 additions & 0 deletions lib/phlex/fifo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# frozen_string_literal: true

class Phlex::FIFO
def initialize(max_bytesize)
@hash = {}
@mutex = Mutex.new

@bytesize = 0
@max_bytesize = max_bytesize
end

attr_accessor :size
attr_accessor :max_bytesize

def [](key)
@hash[key]
end

def []=(key, value)
@mutex.synchronize do
old_value = @hash.delete(key)
@hash[key] = value

@bytesize += value.bytesize - (old_value ? old_value.bytesize : 0)

while @bytesize > @max_bytesize
key, value = @hash.shift
@bytesize -= value.bytesize
end
end
end

def delete(key)
@mutex.synchronize do
old_value = @hash.delete(key)
@bytesize -= old_value.bytesize if old_value
end
end

def include?(key)
@hash.include?(key)
end

def size
@hash.size
end

def empty?
@hash.empty?
end

def clear
@mutex.synchronize do
@hash.clear
end
end
end
22 changes: 1 addition & 21 deletions lib/phlex/sgml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ def new(*args, **kwargs, &block)
end
end

# @api private
def rendered_at_least_once!
alias_method :__attributes__, :__final_attributes__
alias_method :call, :__final_call__
end

# @api private
def element_method?(method_name)
return false unless instance_methods.include?(method_name)
Expand Down Expand Up @@ -94,15 +88,8 @@ def await(task)
end
end

# Renders the view and returns the buffer. The default buffer is a mutable String.
def call(buffer = +"", context: Phlex::Context.new, view_context: nil, parent: nil, &block)
__final_call__(buffer, context: context, view_context: view_context, parent: parent, &block).tap do
self.class.rendered_at_least_once!
end
end

# @api private
def __final_call__(buffer = +"", context: Phlex::Context.new, view_context: nil, parent: nil, &block)
def call(buffer = +"", context: Phlex::Context.new, view_context: nil, parent: nil, &block)
@_buffer = buffer
@_context = context
@_view_context = view_context
Expand Down Expand Up @@ -363,13 +350,6 @@ def __text__(content)

# @api private
def __attributes__(**attributes)
__final_attributes__(**attributes).tap do |buffer|
Phlex::ATTRIBUTE_CACHE[respond_to?(:process_attributes) ? (attributes.hash + self.class.hash) : attributes.hash] = buffer.freeze
end
end

# @api private
def __final_attributes__(**attributes)
if respond_to?(:process_attributes)
attributes = process_attributes(**attributes)
end
Expand Down
Loading