From e3798efbc21645ac1fb9db6f7a067bfd1b997894 Mon Sep 17 00:00:00 2001 From: Dmitri Shuralyov Date: Mon, 22 May 2023 12:56:28 -0400 Subject: [PATCH] cmd/dist: copy trailing text more directly in testJSONFilter.process MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Use io.Copy¹ that matches the comment more closely, avoids the possibility of needing a bigger array, and is slightly shorter. Its downside is that it takes two w.Write calls instead of one. ¹ Admittedly, it was temping to use io.CopyBuffer since the 'data' byte slice becomes a viable buffer after its contents are written. I resisted that temptation for two reasons. One, it would need the io.Reader returned by dec.Buffered() (currently a *bytes.Reader) to not implement the io.WriterTo interface for any chance of making a positive difference. This seems not very likely. Two, to avoid burdening anyone with determining that io.CopyBuffer won't panic without 'if len(data) == 0 && data != nil { data = nil }' because json.Marshal never returns an empty but non-nil byte slice. Change-Id: I33c53d9d990f6ee79cd3ab90f12e3b575b9ebe72 Reviewed-on: https://go-review.googlesource.com/c/go/+/497736 Reviewed-by: Austin Clements Auto-Submit: Dmitri Shuralyov TryBot-Bypass: Dmitri Shuralyov Run-TryBot: Dmitri Shuralyov --- src/cmd/dist/testjson.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/cmd/dist/testjson.go b/src/cmd/dist/testjson.go index 0f7e5be289..7408f95d12 100644 --- a/src/cmd/dist/testjson.go +++ b/src/cmd/dist/testjson.go @@ -100,11 +100,10 @@ func (f *testJSONFilter) process(line []byte) { // Should never happen. panic(fmt.Sprintf("failed to round-trip JSON %q: %s", string(line), err)) } + f.w.Write(data) // Copy any trailing text. We expect at most a "\n" here, but // there could be other text and we want to feed that through. - extra, _ := io.ReadAll(dec.Buffered()) - data = append(data, extra...) - f.w.Write(data) + io.Copy(f.w, dec.Buffered()) return } } -- 2.48.1