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

use sync.Pool to buffer queues #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

brendanashworth
Copy link

This dramatically reduces the amount of memory allocated, the
time spent on GC, and number of allocations per nearest
neighbor search. This should be a dramatic improvement for
workflows where repeated searches are common.

name      old time/op    new time/op    delta
Nearby-8    20.5µs ± 4%    15.1µs ± 7%  -26.37%  (p=0.010 n=6+4)

name      old alloc/op   new alloc/op   delta
Nearby-8    20.6kB ± 1%     3.5kB ± 0%  -82.86%  (p=0.016 n=5+4)

name      old allocs/op  new allocs/op  delta
Nearby-8      13.0 ± 0%       6.0 ± 0%  -53.85%  (p=0.000 n=6+4)

This dramatically reduces the amount of memory allocated, the
time spent on GC, and number of allocations per nearest
neighbor search. This should be a dramatic improvement for
workflows where repeated searches are common.

name      old time/op    new time/op    delta
Nearby-8    20.5µs ± 4%    15.1µs ± 7%  -26.37%  (p=0.010 n=6+4)

name      old alloc/op   new alloc/op   delta
Nearby-8    20.6kB ± 1%     3.5kB ± 0%  -82.86%  (p=0.016 n=5+4)

name      old allocs/op  new allocs/op  delta
Nearby-8      13.0 ± 0%       6.0 ± 0%  -53.85%  (p=0.000 n=6+4)
@brendanashworth
Copy link
Author

bump @tidwall

@tidwall
Copy link
Owner

tidwall commented Dec 7, 2020

I ran into a data race.

This block of code is not safe for use by multiple goroutines simultaneously.

	if index.queues == nil {
		index.queues = &sync.Pool{
			New: func() interface{} {
				// We need not do anything special, but the underlying
				// slice will retain its memory over time.
				return &queue{}
			},
		}
	}

What if you move the queues = &sync.Pool to the Wrap function?

To reproduce the data race add this function to the geoindex_test.go

func TestNearbyQueue(t *testing.T) {
	N := 10000 // number of points
	T := 64    // number of threads
	tr := &rbang.RTree{}
	rand.Seed(time.Now().UnixNano())
	points := make([][2]float64, N)
	for i := 0; i < N; i++ {
		points[i][0] = rand.Float64()*360 - 180
		points[i][1] = rand.Float64()*180 - 90
		tr.Insert(points[i], points[i], i)
	}
	start := time.Now()
	for time.Since(start) < time.Second {
		var count int32
		treeIndex := Wrap(tr)
		lotsa.Output = nil
		lotsa.Ops(N, T, func(i, _ int) {
			targetPoint := points[i]
			treeIndex.Nearby(func(min, max [2]float64, data interface{}, item bool) (dist float64) {
				return math.Sqrt(math.Pow(min[0]-targetPoint[0], 2.) + math.Pow(min[1]-targetPoint[1], 2.))
			}, func(min, max [2]float64, data interface{}, dist float64) bool {
				atomic.AddInt32(&count, 1)
				return false
			})
		})
		if int(count) != N {
			t.Fatalf("wrong count\n")
		}
	}
}

and run go test -race -run TestNearbyQueue

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 this pull request may close these issues.

2 participants