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

[DEBUG] Increase score recording visibility #1858

Closed
wants to merge 5 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
15 changes: 15 additions & 0 deletions lib/liquid/block_body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ def render(context)
def render_to_output_buffer(context, output)
freeze unless frozen?

context.registers.static[:render_scores] ||= []
context.registers.static[:render_scores] << @nodelist.length

context.resource_limits.increment_render_score(@nodelist.length)

idx = 0
Expand All @@ -233,6 +236,18 @@ def render_to_output_buffer(context, output)
end
idx += 1

context.registers.static[:write_scores] ||= []

if (last_captured = context.resource_limits.last_capture_length)
captured = output.bytesize
increment = captured - last_captured
context.registers.static[:assign_scores] ||= []
context.registers.static[:assign_scores] << ['cap', increment]
else
context.registers.static[:write_scores] ||= []
context.registers.static[:write_scores] << (output.bytesize)
end

context.resource_limits.increment_write_score(output)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/liquid/resource_limits.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Liquid
class ResourceLimits
attr_accessor :render_length_limit, :render_score_limit, :assign_score_limit
attr_accessor :render_length_limit, :render_score_limit, :assign_score_limit, :last_capture_length
attr_reader :render_score, :assign_score

def initialize(limits)
Expand Down
12 changes: 9 additions & 3 deletions lib/liquid/tags/assign.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def initialize(tag_name, markup, parse_context)
def render_to_output_buffer(context, output)
val = @from.render(context)
context.scopes.last[@to] = val

context.registers.static[:assign_scores] ||= []
context.registers.static[:assign_scores] << [@to, assign_score_of(val)]

context.resource_limits.increment_assign_score(assign_score_of(val))
output
end
Expand All @@ -47,14 +51,16 @@ def blank?
private

def assign_score_of(val)
if val.instance_of?(String)
return 0 if val.nil?

if val.is_a?(String)
val.bytesize
elsif val.instance_of?(Array)
elsif val.is_a?(Array)
sum = 1
# Uses #each to avoid extra allocations.
val.each { |child| sum += assign_score_of(child) }
sum
elsif val.instance_of?(Hash)
elsif val.is_a?(Hash)
sum = 1
val.each do |key, entry_value|
sum += assign_score_of(key)
Expand Down
Loading