On Fri, Jun 11, 2021 at 9:38 AM tapi...@gmail.com <tapir....@gmail.com> wrote: > > package allocate > > import "testing" > import "os" > import "fmt" > > func init() { > fmt.Println("OS page size:", os.Getpagesize()) > } > > var r1 []byte > > func BenchmarkCount1(b *testing.B) { > for i := 0; i < b.N; i++ { > r1 = make([]byte, 32768+1) > } > } > > var r2 []byte > > func BenchmarkCount2(b *testing.B) { > for i := 0; i < b.N; i++ { > r2 = make([]byte, 40) > } > } > > The output: > > OS page size: 32768 > BenchmarkCount1-4 166443 7312 ns/op 40960 B/op > 1 allocs/op > BenchmarkCount2-4 31465389 36.88 ns/op 48 B/op > 1 allocs/op > > In fact, a memory block with size 36864 is enough to carry the elements of a > byte slice with size 32768+1. Why to allocate one more page for them?
Memory blocks larger than 32768 bytes flip over to the "large allocation" model. See _MaxSmallSize in runtime/sizeclasses.go (a generated file). This requires an extra span. I haven't tried to work it out in detail, but it probably has something to do with that. Ian -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAOyqgcVix6HiyhNGwaxj0L4%3Dmavsj5YheAbgQsOFNPaN_iD78g%40mail.gmail.com.