From b185421405583b74801f147f1f98e43b9108e8bf Mon Sep 17 00:00:00 2001 From: Youssef Makboul Date: Tue, 26 Nov 2024 14:09:27 -0500 Subject: [PATCH 1/5] store scores in regs --- lib/liquid/block_body.rb | 8 ++++++++ lib/liquid/tags/assign.rb | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/lib/liquid/block_body.rb b/lib/liquid/block_body.rb index e4ada7d16..80c334507 100644 --- a/lib/liquid/block_body.rb +++ b/lib/liquid/block_body.rb @@ -218,6 +218,9 @@ def render(context) def render_to_output_buffer(context, output) freeze unless frozen? + context.registers[:render_scores] ||= [] + context.registers[:render_scores] << @nodelist.length + context.resource_limits.increment_render_score(@nodelist.length) idx = 0 @@ -233,6 +236,11 @@ def render_to_output_buffer(context, output) end idx += 1 + context.registers[:write_scores] ||= [] + + last_captured = context.registers[:write_scores].any? ? context.registers[:write_scores].last : 0 + context.registers[:write_scores] << (output.bytesize - last_captured) + context.resource_limits.increment_write_score(output) end diff --git a/lib/liquid/tags/assign.rb b/lib/liquid/tags/assign.rb index 702831781..ad978e306 100644 --- a/lib/liquid/tags/assign.rb +++ b/lib/liquid/tags/assign.rb @@ -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[:assign_scores] ||= [] + context.registers[:assign_scores] << assign_score_of(val) + context.resource_limits.increment_assign_score(assign_score_of(val)) output end From 4d8bf848f14de94babeef0fcf333c0b92a6e4173 Mon Sep 17 00:00:00 2001 From: Youssef Makboul Date: Wed, 27 Nov 2024 12:24:48 -0500 Subject: [PATCH 2/5] use static regs --- lib/liquid/block_body.rb | 11 ++++++----- lib/liquid/tags/assign.rb | 4 ++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/liquid/block_body.rb b/lib/liquid/block_body.rb index 80c334507..1ed0c7f30 100644 --- a/lib/liquid/block_body.rb +++ b/lib/liquid/block_body.rb @@ -218,8 +218,8 @@ def render(context) def render_to_output_buffer(context, output) freeze unless frozen? - context.registers[:render_scores] ||= [] - context.registers[:render_scores] << @nodelist.length + context.registers.static[:render_scores] ||= [] + context.registers.static[:render_scores] << @nodelist.length context.resource_limits.increment_render_score(@nodelist.length) @@ -236,10 +236,11 @@ def render_to_output_buffer(context, output) end idx += 1 - context.registers[:write_scores] ||= [] + context.registers.static[:write_scores] ||= [] - last_captured = context.registers[:write_scores].any? ? context.registers[:write_scores].last : 0 - context.registers[:write_scores] << (output.bytesize - last_captured) + # THIS ISNT ACTUALLY A LIST OF NET OUTPUTS + last_captured = context.registers.static[:write_scores].any? ? context.registers.static[:write_scores].last : 0 + context.registers.static[:write_scores] << (output.bytesize - last_captured) context.resource_limits.increment_write_score(output) end diff --git a/lib/liquid/tags/assign.rb b/lib/liquid/tags/assign.rb index ad978e306..f754ad49a 100644 --- a/lib/liquid/tags/assign.rb +++ b/lib/liquid/tags/assign.rb @@ -37,8 +37,8 @@ def render_to_output_buffer(context, output) val = @from.render(context) context.scopes.last[@to] = val - context.registers[:assign_scores] ||= [] - context.registers[:assign_scores] << assign_score_of(val) + context.registers.static[:assign_scores] ||= [] + context.registers.static[:assign_scores] << assign_score_of(val) context.resource_limits.increment_assign_score(assign_score_of(val)) output From 7193798007ad5b779395630813e784b455d89773 Mon Sep 17 00:00:00 2001 From: Youssef Makboul Date: Wed, 27 Nov 2024 14:19:21 -0500 Subject: [PATCH 3/5] add key and val to assign score recs --- lib/liquid/tags/assign.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/liquid/tags/assign.rb b/lib/liquid/tags/assign.rb index f754ad49a..438533b0e 100644 --- a/lib/liquid/tags/assign.rb +++ b/lib/liquid/tags/assign.rb @@ -38,7 +38,7 @@ def render_to_output_buffer(context, output) context.scopes.last[@to] = val context.registers.static[:assign_scores] ||= [] - context.registers.static[:assign_scores] << assign_score_of(val) + context.registers.static[:assign_scores] << [@to, assign_score_of(val)] context.resource_limits.increment_assign_score(assign_score_of(val)) output From 0d1d77e551f6d44625937daab32c4c09ec48984f Mon Sep 17 00:00:00 2001 From: Youssef Makboul Date: Wed, 27 Nov 2024 16:00:48 -0500 Subject: [PATCH 4/5] use is_a instead of instance and ignore nils --- lib/liquid/tags/assign.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/liquid/tags/assign.rb b/lib/liquid/tags/assign.rb index 438533b0e..2acfddb40 100644 --- a/lib/liquid/tags/assign.rb +++ b/lib/liquid/tags/assign.rb @@ -51,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) From bc0dbeb20850bf41f4d9d1a81e0aca14cb1e0ab8 Mon Sep 17 00:00:00 2001 From: Youssef Makboul Date: Thu, 28 Nov 2024 17:46:50 -0500 Subject: [PATCH 5/5] clean up write recording --- lib/liquid/block_body.rb | 12 +++++++++--- lib/liquid/resource_limits.rb | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/liquid/block_body.rb b/lib/liquid/block_body.rb index 1ed0c7f30..f29c839ff 100644 --- a/lib/liquid/block_body.rb +++ b/lib/liquid/block_body.rb @@ -238,9 +238,15 @@ def render_to_output_buffer(context, output) context.registers.static[:write_scores] ||= [] - # THIS ISNT ACTUALLY A LIST OF NET OUTPUTS - last_captured = context.registers.static[:write_scores].any? ? context.registers.static[:write_scores].last : 0 - context.registers.static[:write_scores] << (output.bytesize - last_captured) + 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 diff --git a/lib/liquid/resource_limits.rb b/lib/liquid/resource_limits.rb index 70fac24be..418f18ac0 100644 --- a/lib/liquid/resource_limits.rb +++ b/lib/liquid/resource_limits.rb @@ -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)