Reduce WritePacket memory allocation - #341
Conversation
|
Have you got any benchmarks to prove allocations have been reduced? |
|
Any update on this? Any benchmarks? |
|
Tested in production, and I see no significant improvement in memory allocations. I have no idea how to benchmark this. |
It's because your benchmark code never calls |
|
Your benchmark only writes a single packet, which is why this PR doesn't provide any benefit. (1) Benchmark:func BenchmarkWritePacket(b *testing.B) {
b.ReportAllocs()
conn := &Conn{
ctx: context.Background(),
hdr: &packet.Header{},
proto: DefaultProtocol,
enc: packet.NewEncoder(io.Discard),
}
p := &packet.Login{}
for b.Loop() {
for i := 0; i < 5; i++ {
_ = conn.WritePacket(p)
}
_ = conn.Flush()
}
}PR: Parent PR (102aa92): (2) Benchmarkfunc BenchmarkWritePacket(b *testing.B) {
b.ReportAllocs()
conn := &Conn{
ctx: context.Background(),
hdr: &packet.Header{},
proto: DefaultProtocol,
enc: packet.NewEncoder(io.Discard),
}
p := &packet.Login{
ConnectionRequest: make([]byte, 1024*1024),
}
for b.Loop() {
for i := 0; i < 5; i++ {
_ = conn.WritePacket(p)
}
_ = conn.Flush()
}
}PR: Parent PR (102aa92):: In benchmark (2), as you can see, this PR reduces memory allocations (B/op) by ~50% compared to the parent PR. |
|
I see, it lwk took me a minute to understand how this optimization worked while reading it but that's pretty neat. |





No description provided.