Compare commits

...
Author SHA1 Message Date
Youssef Makboul bc0dbeb208 clean up write recording 2024-11-28 17:46:50 -05:00
Youssef Makboul 0d1d77e551 use is_a instead of instance and ignore nils 2024-11-27 16:00:48 -05:00
Youssef Makboul 7193798007 add key and val to assign score recs 2024-11-27 14:19:21 -05:00
Youssef Makboul 4d8bf848f1 use static regs 2024-11-27 12:31:05 -05:00
Youssef Makboul b185421405 store scores in regs 2024-11-26 14:29:49 -05:00
3 changed files with 25 additions and 4 deletions
+15
View File
@@ -218,6 +218,9 @@ module Liquid
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
@@ -233,6 +236,18 @@ module Liquid
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
+1 -1
View File
@@ -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)
+9 -3
View File
@@ -36,6 +36,10 @@ module Liquid
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
@@ -47,14 +51,16 @@ module Liquid
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)