From 24b670a5a9cd46292f7281fd1adfe1b1ee8d0b65 Mon Sep 17 00:00:00 2001 From: Roman Atachiants Date: Sun, 12 Dec 2021 20:44:56 +0400 Subject: [PATCH] Migrate to 1.17 and iostream (#2) --- go.mod | 9 +++- go.sum | 2 + grid_test.go | 15 +++--- store.go | 133 +++++++++++++++++++++++++------------------------- store_test.go | 7 ++- 5 files changed, 90 insertions(+), 76 deletions(-) diff --git a/go.mod b/go.mod index 0e4a216..d409ec9 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,12 @@ module github.com/kelindar/tile -go 1.16 +go 1.17 require github.com/stretchr/testify v1.7.0 + +require ( + github.com/davecgh/go-spew v1.1.0 // indirect + github.com/kelindar/iostream v1.2.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect +) diff --git a/go.sum b/go.sum index acb88a4..b407d69 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/kelindar/iostream v1.2.0 h1:UaFLsj/0quGMZxI/rpHmI8u7vNKY9xSyw4mQ9YCTPWA= +github.com/kelindar/iostream v1.2.0/go.mod h1:MkjMuVb6zGdPQVdwLnFRO0xOTOdDvBWTztFmjRDQkXk= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/grid_test.go b/grid_test.go index 65f48c8..e67447b 100644 --- a/grid_test.go +++ b/grid_test.go @@ -10,12 +10,15 @@ import ( "github.com/stretchr/testify/assert" ) -// BenchmarkGrid/each-8 524 2272672 ns/op 0 B/op 0 allocs/op -// BenchmarkGrid/neighbors-8 17840230 69.34 ns/op 0 B/op 0 allocs/op -// BenchmarkGrid/within-8 18727 64443 ns/op 0 B/op 0 allocs/op -// BenchmarkGrid/at-8 61221052 18.31 ns/op 0 B/op 0 allocs/op -// BenchmarkGrid/write-8 59267746 18.01 ns/op 0 B/op 0 allocs/op -// BenchmarkGrid/merge-8 49427276 23.88 ns/op 0 B/op 0 allocs/op +/* +cpu: Intel(R) Core(TM) i7-9700K CPU @ 3.60GHz +BenchmarkGrid/each-8 698 1694002 ns/op 0 B/op 0 allocs/op +BenchmarkGrid/neighbors-8 19652220 66.16 ns/op 0 B/op 0 allocs/op +BenchmarkGrid/within-8 26200 47040 ns/op 0 B/op 0 allocs/op +BenchmarkGrid/at-8 59190276 16.96 ns/op 0 B/op 0 allocs/op +BenchmarkGrid/write-8 100000000 15.40 ns/op 0 B/op 0 allocs/op +BenchmarkGrid/merge-8 52110926 23.06 ns/op 0 B/op 0 allocs/op +*/ func BenchmarkGrid(b *testing.B) { var d [6]byte var p Point diff --git a/store.go b/store.go index 47b45f4..46a519a 100644 --- a/store.go +++ b/store.go @@ -1,67 +1,66 @@ -// Copyright (c) Roman Atachiants and contributors. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for details. - -package tile - -import ( - "bufio" - "encoding/binary" - "io" -) - -const tileDataSize = 54 - -// WriteTo writes the grid to a specific writer. -func (m *Grid) WriteTo(dst io.Writer) (n int64, err error) { - var c int - p1 := At(0, 0) - p2 := At(m.Size.X-1, m.Size.Y-1) - - // Write the viewport size - header := make([]byte, 8) - binary.BigEndian.PutUint16(header[0:2], uint16(p1.X)) - binary.BigEndian.PutUint16(header[2:4], uint16(p1.Y)) - binary.BigEndian.PutUint16(header[4:6], uint16(p2.X)) - binary.BigEndian.PutUint16(header[6:8], uint16(p2.Y)) - if c, err = dst.Write(header); err != nil { - return - } - n += int64(c) - - // Write the grid data - m.pagesWithin(p1, p2, func(page *page) { - if c, err = dst.Write(page.Data()); err != nil { - return - } - n += int64(c) - }) - return -} - -// ReadFrom reads the grid from the reader. -func ReadFrom(rdr io.Reader) (grid *Grid, err error) { - reader := bufio.NewReader(rdr) - header := make([]byte, 8) - if _, err = io.ReadFull(reader, header); err != nil { - return - } - - // Read the size - var view Rect - view.Min.X = int16(binary.BigEndian.Uint16(header[0:2])) - view.Min.Y = int16(binary.BigEndian.Uint16(header[2:4])) - view.Max.X = int16(binary.BigEndian.Uint16(header[4:6])) - view.Max.Y = int16(binary.BigEndian.Uint16(header[6:8])) - - // Allocate a new grid - grid = NewGrid(view.Max.X+1, view.Max.Y+1) - buf := make([]byte, tileDataSize) - grid.pagesWithin(view.Min, view.Max, func(page *page) { - if _, err = io.ReadFull(reader, buf); err != nil { - return - } - - copy(page.Data(), buf) - }) - return -} +// Copyright (c) Roman Atachiants and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for details. + +package tile + +import ( + "encoding/binary" + "io" + + "github.com/kelindar/iostream" +) + +const tileDataSize = 54 + +// WriteTo writes the grid to a specific writer. +func (m *Grid) WriteTo(dst io.Writer) (n int64, err error) { + p1 := At(0, 0) + p2 := At(m.Size.X-1, m.Size.Y-1) + + // Write the viewport size + w := iostream.NewWriter(dst) + header := make([]byte, 8) + binary.BigEndian.PutUint16(header[0:2], uint16(p1.X)) + binary.BigEndian.PutUint16(header[2:4], uint16(p1.Y)) + binary.BigEndian.PutUint16(header[4:6], uint16(p2.X)) + binary.BigEndian.PutUint16(header[6:8], uint16(p2.Y)) + if _, err := w.Write(header); err != nil { + return w.Offset(), err + } + + // Write the grid data + m.pagesWithin(p1, p2, func(page *page) { + if _, err := w.Write(page.Data()); err != nil { + return + } + }) + return w.Offset(), nil +} + +// ReadFrom reads the grid from the reader. +func ReadFrom(src io.Reader) (grid *Grid, err error) { + r := iostream.NewReader(src) + header := make([]byte, 8) + if _, err := io.ReadFull(r, header); err != nil { + return nil, err + } + + // Read the size + var view Rect + view.Min.X = int16(binary.BigEndian.Uint16(header[0:2])) + view.Min.Y = int16(binary.BigEndian.Uint16(header[2:4])) + view.Max.X = int16(binary.BigEndian.Uint16(header[4:6])) + view.Max.Y = int16(binary.BigEndian.Uint16(header[6:8])) + + // Allocate a new grid + grid = NewGrid(view.Max.X+1, view.Max.Y+1) + buf := make([]byte, tileDataSize) + grid.pagesWithin(view.Min, view.Max, func(page *page) { + if _, err := io.ReadFull(r, buf); err != nil { + return + } + + copy(page.Data(), buf) + }) + return +} diff --git a/store_test.go b/store_test.go index b103e2f..7da3ce6 100644 --- a/store_test.go +++ b/store_test.go @@ -11,8 +11,11 @@ import ( "github.com/stretchr/testify/assert" ) -// BenchmarkStore/save-8 6327 188285 ns/op 8 B/op 1 allocs/op -// BenchmarkStore/read-8 2380 471609 ns/op 651594 B/op 8 allocs/op +/* +cpu: Intel(R) Core(TM) i7-9700K CPU @ 3.60GHz +BenchmarkStore/save-8 9068 129974 ns/op 8 B/op 1 allocs/op +BenchmarkStore/read-8 2967 379663 ns/op 647465 B/op 8 allocs/op +*/ func BenchmarkStore(b *testing.B) { m := mapFrom("300x300.png")