From 2d61236d3743a31ae0791da7eefd065f7a6f2a10 Mon Sep 17 00:00:00 2001 From: Matthew Keeler Date: Mon, 2 Mar 2026 10:07:03 -0500 Subject: [PATCH 1/2] fix: Use in-place append for buffer concatenation to fix O(n^2) performance The previous change (0e2e80dc) replaced `buffer << chunk` with `buffer += chunk` to handle frozen strings. However, `+=` creates a new string on every iteration, resulting in O(n^2) behavior for large payloads. The buffer is already explicitly unfrozen (`+"".b`), so `<<` is safe to use. This restores the in-place append while keeping the `.dup` on the incoming chunk to avoid mutating frozen caller strings. --- lib/ld-eventsource/impl/buffered_line_reader.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ld-eventsource/impl/buffered_line_reader.rb b/lib/ld-eventsource/impl/buffered_line_reader.rb index 7f37dd9..3689854 100644 --- a/lib/ld-eventsource/impl/buffered_line_reader.rb +++ b/lib/ld-eventsource/impl/buffered_line_reader.rb @@ -23,7 +23,7 @@ def self.lines_from(chunks) Enumerator.new do |gen| chunks.each do |chunk| chunk = chunk.dup.force_encoding("ASCII-8BIT") - buffer += chunk + buffer << chunk loop do # Search for a line break in any part of the buffer that we haven't yet seen. From 37d5f658c56b85a32763fd59a8d25c72cf8f42c1 Mon Sep 17 00:00:00 2001 From: Matthew Keeler Date: Mon, 2 Mar 2026 10:30:31 -0500 Subject: [PATCH 2/2] fix: Ensure buffer reset preserves mutability and binary encoding Replace `buffer = ""` with `buffer = +"".b` so the reset buffer remains unfrozen and binary-encoded, matching the initial assignment. Without this, `buffer << chunk` after the reset would raise a FrozenError in Ruby 3.4+ where string literals are frozen by default. Co-Authored-By: Claude Opus 4.6 (1M context) --- lib/ld-eventsource/impl/buffered_line_reader.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ld-eventsource/impl/buffered_line_reader.rb b/lib/ld-eventsource/impl/buffered_line_reader.rb index 3689854..c0c3a53 100644 --- a/lib/ld-eventsource/impl/buffered_line_reader.rb +++ b/lib/ld-eventsource/impl/buffered_line_reader.rb @@ -66,7 +66,7 @@ def self.lines_from(chunks) end end if i == buffer.length - buffer = "" + buffer = +"".b i = 0 end position = i