Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allows WaitIO() to reuse the []OpResult slice #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ import (
func echoServer(w *gaio.Watcher) {
for {
// loop wait for any IO events
results, err := w.WaitIO()
results, err := w.WaitIO(nil)
if err != nil {
log.Println(err)
return
Expand Down Expand Up @@ -124,6 +124,27 @@ func main() {

```

Notice that in the example above, ```Watcher.WaitIO()``` will allocate a slice object each time as the returned value. To eliminate these allocations, you can reuse the returned slice in this way:

```go
func server(w *gaio.Watcher) {
var reused []gaio.OpResult
for {
results, err := w.WaitIO(reused)
if err != nil {
log.Println(err)
return
}
for _, res := range results {
// do something...
}
if results != nil {
reused = results[:0]
}
}
}
```

### More examples

<details>
Expand Down Expand Up @@ -166,7 +187,7 @@ func main() {
// watcher.WaitIO goroutine
go func() {
for {
results, err := w.WaitIO()
results, err := w.WaitIO(nil)
if err != nil {
log.Println(err)
return
Expand Down
24 changes: 12 additions & 12 deletions aio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func echoServer(t testing.TB, bufsize int) net.Listener {
// ping-pong scheme echo server
go func() {
for {
results, err := w.WaitIO()
results, err := w.WaitIO(nil)
if err != nil {
log.Println(err)
return
Expand Down Expand Up @@ -206,7 +206,7 @@ func TestUnsupportedConn(t *testing.T) {
w.Read(nil, p2, make([]byte, 1))

for {
results, err := w.WaitIO()
results, err := w.WaitIO(nil)
if err != nil {
t.Log(err)
}
Expand Down Expand Up @@ -252,7 +252,7 @@ func testSingleDeadline(t *testing.T, w *Watcher) {

READTEST:
for {
results, err := w.WaitIO()
results, err := w.WaitIO(nil)
if err != nil {
t.Log(err)
break READTEST
Expand Down Expand Up @@ -283,7 +283,7 @@ READTEST:

WRITETEST:
for {
results, err := w.WaitIO()
results, err := w.WaitIO(nil)
if err != nil {
t.Log(err)
break WRITETEST
Expand Down Expand Up @@ -371,7 +371,7 @@ func testBidirectionWatcher(t *testing.T, w *Watcher) {
}()

for {
results, err := w.WaitIO()
results, err := w.WaitIO(nil)
if err != nil {
t.Log(err)
return
Expand Down Expand Up @@ -434,7 +434,7 @@ func TestReadFull(t *testing.T) {
}

for {
results, err := w.WaitIO()
results, err := w.WaitIO(nil)
if err != nil {
t.Log(err)
return
Expand Down Expand Up @@ -485,7 +485,7 @@ func TestSocketClose(t *testing.T) {
log.Fatal(err)
}
for {
results, err := w.WaitIO()
results, err := w.WaitIO(nil)
if err != nil {
t.Log(err)
return
Expand Down Expand Up @@ -531,7 +531,7 @@ func TestWriteOnClosedConn(t *testing.T) {
}()

for {
results, err := w.WaitIO()
results, err := w.WaitIO(nil)
if err != nil {
t.Log(err)
return
Expand Down Expand Up @@ -615,7 +615,7 @@ func testParallel(t *testing.T, par int, msgsize int) {
nbytes := 0
ntotal := msgsize * par
for {
results, err := w.WaitIO()
results, err := w.WaitIO(nil)
if err != nil {
t.Log(err)
return
Expand Down Expand Up @@ -689,7 +689,7 @@ func testParallelRandomInternal(t *testing.T, par int, msgsize int, allswap bool
nbytes := 0
ntotal := msgsize * par
for {
results, err := w.WaitIO()
results, err := w.WaitIO(nil)
if err != nil {
t.Log(err)
return
Expand Down Expand Up @@ -776,7 +776,7 @@ func testDeadline(t *testing.T, par int) {

nerrs := 0
for {
results, err := w.WaitIO()
results, err := w.WaitIO(nil)
if err != nil {
t.Log(err)
return
Expand Down Expand Up @@ -871,7 +871,7 @@ func benchmarkEcho(b *testing.B, bufsize int, numconn int) {
count := 0
target := bufsize * b.N * numconn
for {
results, err := w.WaitIO()
results, err := w.WaitIO(nil)
if err != nil {
b.Fatal("waitio:", err)
return
Expand Down
2 changes: 1 addition & 1 deletion examples/echo-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func echoServer(w *gaio.Watcher) {
for {
// loop wait for any IO events
results, err := w.WaitIO()
results, err := w.WaitIO(nil)
if err != nil {
log.Println(err)
return
Expand Down
2 changes: 1 addition & 1 deletion examples/push-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func main() {
// watcher.WaitIO goroutine
go func() {
for {
results, err := w.WaitIO()
results, err := w.WaitIO(nil)
if err != nil {
log.Println(err)
return
Expand Down
4 changes: 2 additions & 2 deletions watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ func (w *watcher) notifyPending() {
}

// WaitIO blocks until any read/write completion, or error.
// An internal 'buf' returned or 'r []OpResult' are safe to use BEFORE next call to WaitIO().
func (w *watcher) WaitIO() (r []OpResult, err error) {
// An internal 'buf' returned or '[]OpResult' are safe to use BEFORE next call to WaitIO().
func (w *watcher) WaitIO(r []OpResult) ([]OpResult, error) {
// recycle previous aiocb
for k := range w.recycles {
aiocbPool.Put(w.recycles[k])
Expand Down