Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jnodorp committed May 11, 2023
0 parents commit 063b470
Show file tree
Hide file tree
Showing 10 changed files with 375 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Julian Nodorp

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# infinite

Infinite provides an [`io.Reader`](https://pkg.go.dev/io#Reader) that never returns EOF. Instead, a pattern is repeated
forever.

I needed this for performance testing of a streaming JSON parser.

## Usage

```go
r := infinite.NewReader([]byte("Hello infinity!"))

buf := make([]byte, 16)

r.Read(buf)
fmt.Printf(string(buf) + "\n")

r.Read(buf)
fmt.Printf(string(buf) + "\n")

// Output:
// Hello infinity!H
// ello infinity!He
```

## Limitations

The buffer size must be a multiple of `sizeof(__uint128_t)`!
11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module github.com/jnodorp/infinite

go 1.20

require github.com/stretchr/testify v1.8.2

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
17 changes: 17 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
24 changes: 24 additions & 0 deletions read_cgo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//go:build cgo

package infinite

// #cgo CFLAGS: -march=native
// #include "reader.h"
import "C"
import (
"bytes"
"io"
"unsafe"
)

var _ io.Reader = (*C.reader_t)(nil)

// NewReader that will always return data. Once all data has been read, it will continue from the start.
func NewReader(data []byte) io.Reader {
data = bytes.Repeat(data, 16)
return C.new_reader(C.CBytes(data), C.ulong(len(data)))
}

func (r *C.reader_t) Read(buf []byte) (int, error) {
return int(C._read(r, unsafe.Pointer(&buf[0]), C.ulong(len(buf)))), nil
}
32 changes: 32 additions & 0 deletions read_fallback.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//go:build !cgo

package infinite

import "io"

var _ io.Reader = (*reader)(nil)

type reader struct {
Data []byte
pos int
}

// NewReader that will always return data. Once all data has been read, it will continue from the start.
func NewReader(data []byte) io.Reader {
return &reader{
Data: data,
}
}

func (r *reader) Read(buf []byte) (int, error) {
for i := range buf {
buf[i] = r.Data[r.pos]

r.pos++
if r.pos == len(r.Data) {
r.pos = 0
}
}

return len(buf), nil
}
72 changes: 72 additions & 0 deletions reader.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include <immintrin.h>
#include "reader.h"

/*
* Create a new reader.
*/
reader_t *new_reader(const void *src, size_t src_size)
{
reader_t *r = malloc(sizeof *r);
r->src = src;
r->src_size = src_size;
r->off = 0;
return r;
}

/*
* Copy a pattern into a buffer with an offset into the pattern.
*
* This can be used to repeatedly fill a buffer with a portion of data, creating a stream of the pattern.
*
* Pseudocode example:
*
* \code
* pat <- "foo"
* buf <- "__"
*
* off <- copy_with_offset(buf, pat, 2, 3, 0)
*
* off: 2
* buf: "fo"
*
* off <- copy_with_offset(buf, pat, 2, 3, off)
*
* off: 1
* buf: "of"
*
* off <- copy_with_offset(buf, pat, 2, 3, off)
*
* off: 0
* buf: "oo"
*
* off <- copy_with_offset(buf, pat, 2, 3, off)
*
* off: 2
* buf: "fo"
* \endcode
*
* @param r the reader_t holding any state
* @param dst pointer to the start of the buffer
* @param dst_size number of bytes in the buffer. Must be a multiple of sizeof(__uint128_t)!
*
* @return dst_size to resemble with Go's io.Reader
*/
size_t _read(reader_t *r, void *dst, size_t dst_size)
{
__uint128_t *d = dst;
const __uint128_t *s = r->src;

size_t maxOff = r->src_size / sizeof(__uint128_t);
for (size_t i = 0; i < dst_size / sizeof(__uint128_t); i++)
{
d[i] = s[r->off];

r->off++;
if (r->off == maxOff)
{
r->off = 0;
}
}

return dst_size;
}
11 changes: 11 additions & 0 deletions reader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <stddef.h>

typedef struct {
const void *src;
size_t src_size;
size_t off;
} reader_t;

reader_t* new_reader(const void *src, size_t src_size);

size_t _read(reader_t *r, void *dst, size_t dst_size);
138 changes: 138 additions & 0 deletions reader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package infinite_test

import (
"fmt"
"strings"
"testing"

. "github.com/jnodorp/infinite"
"github.com/stretchr/testify/assert"
)

func TestReadBufferSmallerThanData(t *testing.T) {
r := NewReader([]byte(string("0123456789ABCDEF-")))

// Buffer size must be a multiple of 256 bits (32 bytes).
buf := make([]byte, 32)

n, err := r.Read(buf)
assert.NoError(t, err)
assert.Equal(t, len(buf), n)
assert.Equal(t, "0123456789ABCDEF-0123456789ABCDE", string(buf))

n, err = r.Read(buf)
assert.NoError(t, err)
assert.Equal(t, len(buf), n)
assert.Equal(t, "F-0123456789ABCDEF-0123456789ABC", string(buf))

n, err = r.Read(buf)
assert.NoError(t, err)
assert.Equal(t, len(buf), n)
assert.Equal(t, "DEF-0123456789ABCDEF-0123456789A", string(buf))
}

func TestReadBufferLargerThanData(t *testing.T) {
r := NewReader([]byte("foobar"))

// Buffer size must be a multiple of 256 bits (32 bytes).
buf := make([]byte, 64)

n, err := r.Read(buf)
assert.NoError(t, err)
assert.Equal(t, len(buf), n)
assert.Equal(t, strings.Repeat("foobar", 12)[:64], string(buf))

n, err = r.Read(buf)
assert.NoError(t, err)
assert.Equal(t, len(buf), n)
assert.Equal(t, strings.Repeat("foobar", 12)[4:68], string(buf))

n, err = r.Read(buf)
assert.NoError(t, err)
assert.Equal(t, len(buf), n)
assert.Equal(t, strings.Repeat("foobar", 12)[2:66], string(buf))

n, err = r.Read(buf)
assert.NoError(t, err)
assert.Equal(t, len(buf), n)
assert.Equal(t, strings.Repeat("foobar", 12)[:64], string(buf))
}

func ExampleNewReader() {
r := NewReader([]byte("Hello infinity!"))

// Buffer size must be a multiple of 256 bits (32 bytes).
buf := make([]byte, 32)

r.Read(buf)
fmt.Printf(string(buf) + "\n")

r.Read(buf)
fmt.Printf(string(buf) + "\n")
// Output: Hello infinity!Hello infinity!He
// llo infinity!Hello infinity!Hell
}

// BenchmarkRead heavily depends on whether cgo is enabled!
//
// $ CGO_ENABLED=0 go test -benchmem -run=^$ -bench ^BenchmarkRead_4096$
// goos: darwin
// goarch: amd64
// pkg: github.com/jnodorp/infinite
// cpu: Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
// BenchmarkRead_4096-16 249250 4082 ns/op 1003.49 MB/s 0 B/op 0 allocs/op
// PASS
// ok github.com/jnodorp/infinite 1.343s
//
// $ CGO_ENABLED=1 go test -benchmem -run=^$ -bench ^BenchmarkRead_4096$
// goos: darwin
// goarch: amd64
// pkg: github.com/jnodorp/infinite
// cpu: Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
// BenchmarkRead_4096-16 4713326 250.4 ns/op 16355.63 MB/s 24 B/op 1 allocs/op
// PASS
// ok github.com/jnodorp/infinite 1.548s
func BenchmarkRead_4096(b *testing.B) {
r := NewReader([]byte("foobar"))

// Use a reasonably large buffer, to get useful results. Buffer size must be a multiple of 128 bits (16 bytes).
buf := make([]byte, 4096)

for i := 0; i < b.N; i++ {
n, err := r.Read(buf)
assert.NoError(b, err)
b.SetBytes(int64(n))
}
}

// BenchmarkRead heavily depends on whether cgo is enabled!
//
// $ CGO_ENABLED=0 go test -benchmem -run=^$ -bench ^BenchmarkRead_8192$
// goos: darwin
// goarch: amd64
// pkg: github.com/jnodorp/infinite
// cpu: Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
// BenchmarkRead_8192-16 141156 8320 ns/op 984.60 MB/s 0 B/op 0 allocs/op
// PASS
// ok github.com/jnodorp/infinite 1.372s
//
// $ CGO_ENABLED=1 go test -benchmem -run=^$ -bench ^BenchmarkRead_8192$
// goos: darwin
// goarch: amd64
// pkg: github.com/jnodorp/infinite
// cpu: Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
// BenchmarkRead_8192-16 3105466 377.6 ns/op 21692.77 MB/s 24 B/op 1 allocs/op
// PASS
// ok github.com/jnodorp/infinite 1.681s
func BenchmarkRead_8192(b *testing.B) {
r := NewReader([]byte("foobar"))

// Use a reasonably large buffer, to get useful results. Buffer size must be a multiple of 128 bits (16 bytes).
buf := make([]byte, 8192)

for i := 0; i < b.N; i++ {
n, err := r.Read(buf)
assert.NoError(b, err)
b.SetBytes(int64(n))
}
}

0 comments on commit 063b470

Please sign in to comment.