all: use bufio.NewWriter to improve writing speed

This commit is contained in:
Hajime Hoshi 2024-07-29 16:29:50 +09:00
parent ef5ac4175d
commit 0281ac7bd2
8 changed files with 62 additions and 14 deletions

View File

@ -17,6 +17,7 @@
package main
import (
"bufio"
"fmt"
"image"
"image/color"
@ -77,7 +78,11 @@ func run() error {
}
defer f.Close()
if err := png.Encode(f, dst); err != nil {
w := bufio.NewWriter(f)
if err := png.Encode(w, dst); err != nil {
return err
}
if err := w.Flush(); err != nil {
return err
}

View File

@ -15,6 +15,7 @@
package main
import (
"bufio"
"flag"
"log"
"os"
@ -33,9 +34,11 @@ func main() {
if err != nil {
log.Fatal(err)
}
if err := pprof.StartCPUProfile(f); err != nil {
w := bufio.NewWriter(f)
if err := pprof.StartCPUProfile(w); err != nil {
log.Fatal(err)
}
defer w.Flush()
defer pprof.StopCPUProfile()
}

View File

@ -17,6 +17,7 @@
package main
import (
"bufio"
"bytes"
"image"
"image/png"
@ -220,7 +221,11 @@ func outputKeyboardImage() (map[ebiten.Key]image.Rectangle, error) {
}
defer out.Close()
if err := png.Encode(out, img); err != nil {
w := bufio.NewWriter(out)
if err := png.Encode(w, img); err != nil {
return nil, err
}
if err := w.Flush(); err != nil {
return nil, err
}
@ -278,10 +283,17 @@ func outputKeyRectsGo(k map[ebiten.Key]image.Rectangle) error {
if err != nil {
return err
}
return tmpl.Execute(f, map[string]any{
w := bufio.NewWriter(f)
if err := tmpl.Execute(w, map[string]any{
"License": license,
"KeyRectsMap": k,
})
}); err != nil {
return err
}
if err := w.Flush(); err != nil {
return err
}
return nil
}
type game struct {

View File

@ -20,6 +20,7 @@
package main
import (
"bufio"
"errors"
"fmt"
"os"
@ -88,7 +89,15 @@ func generateHSLSFiles(source *shaderprecomp.ShaderSource, index int, tmpdir str
}
defer psf.Close()
if err := shaderprecomp.CompileToHLSL(vsf, psf, source); err != nil {
vsfw := bufio.NewWriter(vsf)
psfw := bufio.NewWriter(psf)
if err := shaderprecomp.CompileToHLSL(vsfw, psfw, source); err != nil {
return "", "", err
}
if err := vsfw.Flush(); err != nil {
return "", "", err
}
if err := psfw.Flush(); err != nil {
return "", "", err
}

View File

@ -20,6 +20,7 @@
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
@ -68,10 +69,11 @@ func compile(source *shaderprecomp.ShaderSource, index int, tmpdir string) error
}
defer f.Close()
if err := shaderprecomp.CompileToMSL(f, source); err != nil {
w := bufio.NewWriter(f)
if err := shaderprecomp.CompileToMSL(w, source); err != nil {
return err
}
if err := f.Sync(); err != nil {
if err := w.Flush(); err != nil {
return err
}

View File

@ -19,6 +19,7 @@
package main
import (
"bufio"
"log"
"os"
"path/filepath"
@ -809,7 +810,8 @@ func main() {
buildConstraints = "//go:build !android && !ios && !js && !nintendosdk && !playstation5"
}
// NOTE: According to godoc, maps are automatically sorted by key.
if err := tmpl.Execute(f, struct {
w := bufio.NewWriter(f)
if err := tmpl.Execute(w, struct {
License string
DoNotEdit string
BuildConstraints string
@ -840,5 +842,9 @@ func main() {
}); err != nil {
log.Fatal(err)
}
if err := w.Flush(); err != nil {
log.Fatal(err)
}
}
}

View File

@ -155,7 +155,8 @@ func run() error {
}
defer f.Close()
if err := tmpl.Execute(f, struct {
w := bufio.NewWriter(f)
if err := tmpl.Execute(w, struct {
License string
DoNotEdit string
BuildConstraints string
@ -170,6 +171,9 @@ func run() error {
}); err != nil {
return err
}
if err := w.Flush(); err != nil {
return err
}
}
return nil

View File

@ -17,6 +17,7 @@
package main
import (
"bufio"
"fmt"
"go/ast"
"go/format"
@ -93,6 +94,8 @@ func run() error {
}
defer out.Close()
w := bufio.NewWriter(out)
// TODO: Remove call of RegisterDecoder
data, err := os.ReadFile(filepath.Join(dir, f))
@ -129,14 +132,14 @@ func run() error {
return true
}, nil)
fmt.Fprintln(out, "// Code generated by gen.go. DO NOT EDIT.")
fmt.Fprintln(out)
format.Node(out, fset, tree)
fmt.Fprintln(w, "// Code generated by gen.go. DO NOT EDIT.")
fmt.Fprintln(w)
format.Node(w, fset, tree)
if f == "reader.go" {
// The min function was removed as of Go 1.22, but this is needed for old Go.
// TODO: Remove this when Go 1.21 is the minimum supported version.
fmt.Fprintln(out, `
fmt.Fprintln(w, `
func min(a, b int) int {
if a < b {
return a
@ -144,6 +147,10 @@ func min(a, b int) int {
return b
}`)
}
if err := w.Flush(); err != nil {
return err
}
}
return nil
}