From ed3c6bca0a833c06b169a057eb9b7d9c8df84084 Mon Sep 17 00:00:00 2001 From: stephann <3025661+stephannv@users.noreply.github.com> Date: Sun, 8 Sep 2024 17:06:53 -0300 Subject: [PATCH] perf: Use String::Builder to improve render times (#51) --- src/blueprint/html.cr | 2 +- src/blueprint/html/content_capture.cr | 4 ++-- src/blueprint/html/renderer.cr | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/blueprint/html.cr b/src/blueprint/html.cr index 8d348f5..a42de75 100644 --- a/src/blueprint/html.cr +++ b/src/blueprint/html.cr @@ -10,7 +10,7 @@ require "./html/svg" require "./html/utils" module Blueprint::HTML - @buffer = IO::Memory.new + @buffer = String::Builder.new def to_html : String return "" unless render? diff --git a/src/blueprint/html/content_capture.cr b/src/blueprint/html/content_capture.cr index f109d63..f1106b4 100644 --- a/src/blueprint/html/content_capture.cr +++ b/src/blueprint/html/content_capture.cr @@ -1,8 +1,8 @@ module Blueprint::HTML private def capture_content(&) : Nil - buffer_size_before_block_evaluation = @buffer.size + buffer_size_before_block_evaluation = @buffer.bytesize content = with self yield - if buffer_size_before_block_evaluation == @buffer.size + if buffer_size_before_block_evaluation == @buffer.bytesize ::HTML.escape(content.to_s, @buffer) end end diff --git a/src/blueprint/html/renderer.cr b/src/blueprint/html/renderer.cr index c55f51d..ad0a409 100644 --- a/src/blueprint/html/renderer.cr +++ b/src/blueprint/html/renderer.cr @@ -10,14 +10,14 @@ module Blueprint::HTML end end - protected def render_to(buffer : IO::Memory) : Nil + protected def render_to(buffer : String::Builder) : Nil return unless render? @buffer = buffer blueprint end - protected def render_to(buffer : IO::Memory, &) : Nil + protected def render_to(buffer : String::Builder, &) : Nil return unless render? @buffer = buffer