Compare commits

...

2 Commits

Author SHA1 Message Date
Hajime Hoshi
c8aea2df16 examples/blocks: bug fix: do not ignore error 2024-07-29 17:37:30 +09:00
Hajime Hoshi
0281ac7bd2 all: use bufio.NewWriter to improve writing speed 2024-07-29 16:29:57 +09:00
8 changed files with 66 additions and 14 deletions

View File

@ -17,6 +17,7 @@
package main package main
import ( import (
"bufio"
"fmt" "fmt"
"image" "image"
"image/color" "image/color"
@ -77,7 +78,11 @@ func run() error {
} }
defer f.Close() 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 return err
} }

View File

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

View File

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

View File

@ -20,6 +20,7 @@
package main package main
import ( import (
"bufio"
"errors" "errors"
"fmt" "fmt"
"os" "os"
@ -88,7 +89,15 @@ func generateHSLSFiles(source *shaderprecomp.ShaderSource, index int, tmpdir str
} }
defer psf.Close() 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 return "", "", err
} }

View File

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

View File

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

View File

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

View File

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