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

Make OutputBuffer more performant for 3.2+ YJIT #723

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
57 changes: 51 additions & 6 deletions bridgetown-core/lib/bridgetown-core/converters/erb_templates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,71 @@
require "tilt/erubi"

module Bridgetown
class OutputBuffer < ActiveSupport::SafeBuffer
def initialize(*)
super
encode!
class OutputBuffer
def initialize(buffer = "")
@buffer = String.new(buffer)
@buffer.encode!
end

def initialize_copy(other)
@buffer = other.to_str
end

delegate(
:blank?,
:empty?,
:encode,
:encode!,
:encoding,
:force_encoding,
:length,
:lines,
:reverse,
:strip,
:valid_encoding?,
to: :@buffer
)

def <<(value)
return self if value.nil?

super(value.to_s)
value = value.to_s
value = CGI.escapeHTML(value) unless value.html_safe?

@buffer << value

self
end
alias_method :append=, :<<

def ==(other)
other.instance_of?(OutputBuffer) &&
@buffer == other.to_str
end

def html_safe?
true
end

def safe_concat(value)
@buffer << value
self
end
alias_method :safe_append=, :safe_concat

def safe_expr_append=(val)
return self if val.nil? # rubocop:disable Lint/ReturnInVoidContext

safe_concat val.to_s
end

alias_method :safe_append=, :safe_concat
def to_s
@buffer.html_safe
end

def to_str
@buffer.dup
end
end

class ERBEngine < Erubi::Engine
Expand Down
41 changes: 41 additions & 0 deletions bridgetown-core/test/bridgetown/test_output_buffer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

require "helper"

module Bridgetown
class OutputBufferTest < BridgetownUnitTest
def setup
super
@buffer = Bridgetown::OutputBuffer.new
end

should "be able to be duped" do
@buffer << "Hello"
copy = @buffer.dup
copy << " world!"

assert_equal "Hello", @buffer.to_s
assert_equal "Hello world!", copy.to_s
end

context "#<<" do
should "maintain HTML safety" do
@buffer << "<p>Nothing bad to see here.</p>"

assert_predicate @buffer, :html_safe?
assert_predicate @buffer.to_s, :html_safe?
assert_equal "&lt;p&gt;Nothing bad to see here.&lt;/p&gt;", @buffer.to_s
end
end

context "#safe_append=" do
should "bypass HTML safety" do
@buffer.safe_append = "<p>Nothing bad to see here.</p>"

assert_predicate @buffer, :html_safe?
assert_predicate @buffer.to_s, :html_safe?
assert_equal "<p>Nothing bad to see here.</p>", @buffer.to_s
end
end
end
end