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

reset items from sync.pool #118

Open
komuw opened this issue Sep 15, 2018 · 3 comments · Fixed by #119
Open

reset items from sync.pool #118

komuw opened this issue Sep 15, 2018 · 3 comments · Fixed by #119

Comments

@komuw
Copy link
Owner

komuw commented Sep 15, 2018

we should reset whatever we get from sync.pool b4 use;

meli/image.go

Line 118 in 9e036d4

bufp := blackHolePool.Get().(*[]byte)

@komuw
Copy link
Owner Author

komuw commented Sep 15, 2018

if you run the following code:

func BenchmarkPoolReadFrom(b *testing.B) {
	r := strings.NewReader("hello")
	b.ResetTimer()
	for n := 0; n < 3; n++ {
		_, _ = poolReadFrom(r)
	}
}

and inside

meli/image.go

Line 118 in 9e036d4

bufp := blackHolePool.Get().(*[]byte)
you;

fmt.Printf("#%+v", bufp)

you get an output like;

#&[0 0 0 0 0 0 0 0]

#&[104 101 108 108 111 0 0 0]

#&[104 101 108 108 111 0 0 0]

#&[0 0 0 0 0 0 0 0]

#&[104 101 108 108 111 0 0 0]

So the first time we get a []byte from sync.Pool it is zeroed, but after that every other access may already have data in it.
also

fmt.Printf("#%+v", string(*bufp))

prints;

#
#hello
#hello
#

get 1 & 4 are okay(zeroed) but get 2 & 3 are not.

@komuw
Copy link
Owner Author

komuw commented Sep 19, 2018

package main

import (
	"bytes"
	"fmt"
	"io"
	"strings"
	"sync"
)

const max = 512

var blackHolePool = sync.Pool{
	New: func() interface{} {
		return make([]byte, max)
	},
}

func main() {
	x := []string{"hello", "cool", "three", "four"}
	for _, v := range x {
		src := strings.NewReader(v)
		bufp := blackHolePool.Get().([]byte)
		dst := bytes.NewBuffer(bufp)
		io.Copy(dst, src)

		blackHolePool.Put(bufp[:max])

		fmt.Println(dst)
	}

}

@komuw komuw reopened this Sep 19, 2018
@komuw
Copy link
Owner Author

komuw commented Sep 19, 2018

see: golang/go#27740

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant