From f8e650d08b09400d1d2190eeb3ae37d3f9c94c89 Mon Sep 17 00:00:00 2001 From: passionSeven Date: Fri, 8 May 2020 10:00:00 -0400 Subject: [PATCH] [x/tour] pic: don't make a string copy when writing image Previously, the entire image was encoded into a buffer, then encoded to a base64 string, which was concatenated with another string, and written to stdout in one swoop. We can do this more efficiently by writing the PNG image to a base64 encoder directly, and using buffered I/O to batch writes to stdout into moderately-sized chunks. As an added bonus, though really the main motivation for doing this optimization now, this helps avoid triggering the golang/go#38751 issue on the Go Playground. Switch to the best compression level instead of the default, reducing encoded image size at the cost of extra computation. Playground snippets need to be transferred over the network and kept in storage, so it should be a favorable trade-off. This CL was based on CL 232177. For golang/go#38751. Change-Id: I565fe538aa15910caaff98be156ac64b0d35fff4 Co-authored-by: Alexander Rakoczy Reviewed-on: https://go-review.googlesource.com/c/tour/+/232867 Run-TryBot: Dmitri Shuralyov TryBot-Result: Gobot Gobot Reviewed-by: Alexander Rakoczy X-Tour-Commit: 0608babe047def227de553b6538ed3bd6277acff --- tour/pic/pic.go | 16 ++++++++++------ tour/pic/pic_test.go | 2 +- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/tour/pic/pic.go b/tour/pic/pic.go index 7f22edd4..f60ba0e7 100644 --- a/tour/pic/pic.go +++ b/tour/pic/pic.go @@ -7,11 +7,12 @@ package pic // import "golang.org/x/tour/pic" import ( - "bytes" + "bufio" "encoding/base64" - "fmt" "image" "image/png" + "io" + "os" ) // Show displays a picture defined by the function f @@ -46,11 +47,14 @@ func Show(f func(dx, dy int) [][]uint8) { // ShowImage displays the image m // when executed on the Go Playground. func ShowImage(m image.Image) { - var buf bytes.Buffer - err := png.Encode(&buf, m) + w := bufio.NewWriter(os.Stdout) + defer w.Flush() + io.WriteString(w, "IMAGE:") + b64 := base64.NewEncoder(base64.StdEncoding, w) + err := (&png.Encoder{CompressionLevel: png.BestCompression}).Encode(b64, m) if err != nil { panic(err) } - enc := base64.StdEncoding.EncodeToString(buf.Bytes()) - fmt.Println("IMAGE:" + enc) + b64.Close() + io.WriteString(w, "\n") } diff --git a/tour/pic/pic_test.go b/tour/pic/pic_test.go index 2453773b..0e8ccf52 100644 --- a/tour/pic/pic_test.go +++ b/tour/pic/pic_test.go @@ -22,5 +22,5 @@ func ExampleShow() { pic.Show(f) // Output: - // IMAGE:iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAIAAADTED8xAAADKElEQVR4nOzVoRHAMAzAQCfX/VduxjDQk8di+mb+mTlnyKB3vYBc9K4XkIs6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6ANM6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