]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/dist: copy trailing text more directly in testJSONFilter.process
authorDmitri Shuralyov <dmitshur@golang.org>
Mon, 22 May 2023 16:56:28 +0000 (12:56 -0400)
committerGopher Robot <gobot@golang.org>
Wed, 24 May 2023 17:40:19 +0000 (17:40 +0000)
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 <austin@google.com>
Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org>
TryBot-Bypass: Dmitri Shuralyov <dmitshur@golang.org>
Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org>

src/cmd/dist/testjson.go

index 0f7e5be289494b4214b8433b1db3ae3565357d4f..7408f95d12be472984b9e1bb005d7e1d2d8acd99 100644 (file)
@@ -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
                        }
                }