From c25c6e38e1bd834b868c01503e6e556bcd036d47 Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Tue, 29 Sep 2026 12:31:04 +0100 Subject: [PATCH] perf(sse): skip line splitting for single-line data events --- sse.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/sse.go b/sse.go index 6ef961d..38b70a5 100644 --- a/sse.go +++ b/sse.go @@ -315,12 +315,20 @@ func splitSSELines(b []byte) [][]byte { // "\r" of any kind ever reaches the wire from here, regardless of what // readSSE upstream of this call did or did not already split it on. func (s *sseWriter) writeData(b []byte) error { - lines := splitSSELines(b) - buf := make([]byte, 0, len(b)+len(lines)*len(sseDataPrefix)+1) - for _, line := range lines { + var buf []byte + if !bytes.ContainsAny(b, "\r\n") { + buf = make([]byte, 0, len(sseDataPrefix)+len(b)+2) buf = append(buf, sseDataPrefix...) - buf = append(buf, line...) + buf = append(buf, b...) buf = append(buf, '\n') + } else { + lines := splitSSELines(b) + buf = make([]byte, 0, len(b)+len(lines)*len(sseDataPrefix)+1) + for _, line := range lines { + buf = append(buf, sseDataPrefix...) + buf = append(buf, line...) + buf = append(buf, '\n') + } } buf = append(buf, '\n')