Skip to content

Commit

Permalink
Migrate to 1.17 and iostream (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
kelindar authored Dec 12, 2021
1 parent a21ffc1 commit 24b670a
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 76 deletions.
9 changes: 8 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
15 changes: 9 additions & 6 deletions grid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
133 changes: 66 additions & 67 deletions store.go
Original file line number Diff line number Diff line change
@@ -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
}
7 changes: 5 additions & 2 deletions store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down

0 comments on commit 24b670a

Please sign in to comment.