I'm wondering if it's possible use sync.Pool
with an array or slice? For example, could sync.Pool
speed up the following when handling tens-of-thousands of requests per second? The example is simply understanding Go better.
// Handler that uses GenerateArray
func ok(w http.ResponseWriter, r *http.Request) {
var res [100000]uint64
fibonacci.GenerateArray(&res)
fmt.Fprintf(w, "OK")
}
func GenerateArray(data *[100000]uint64) {
var start uint16 = 1000
var counter uint32
for start >= 1 {
var num = 90
var n1, n2, temp uint64 = 0, 1, 0
for num >= 1 {
temp = n2
n1, n2 = temp, n1+n2
data[counter] = n2
counter++
num--
}
start--
}
}
EDIT: This is the slice version as suggested by Icza. Hopefully I did it right as I'm learning.
res := make([]uint64, 100000)
fibonacci.GenerateSlice(res)
// PopulateSlice does this...
func PopulateSlice(data []uint64) {
var start uint16 = 1000
var counter uint32
for start >= 1 {
var num = 90
var n1, n2, temp uint64 = 0, 1, 0
for num >= 1 {
temp = n2
n1, n2 = temp, n1+n2
data[counter] = n2
counter++
num--
}
start--
}
}
Returning it.
func GenerateSlice() []uint64 {
data := make([]uint64, 0, 100000)
var start uint16 = 1000
var counter uint32
for start >= 1 {
var num = 90
var n1, n2, temp uint64 = 0, 1, 0
for num >= 1 {
temp = n2
n1, n2 = temp, n1+n2
// data[counter] = n2
data = append(data, n2)
counter++
num--
}
start--
}
return data
}