ice icon indicating copy to clipboard operation
ice copied to clipboard

Reduce `candidateBase.writeTo` CPU cost

Open adwpc opened this issue 5 years ago • 7 comments

Summary

image

Motivation

reduce candidateBase.writeTo cpu cost

Describe alternatives you've considered

Maybe we can use bufio in candidateBase.writeTo, like: https://github.com/grpc/grpc-go/pull/1544/commits/30d8f0749874c54017578cf0760e18c01d27a3a2

Additional context

https://github.com/pion/ice/blob/master/candidate_base.go#L152

adwpc avatar Nov 24 '19 10:11 adwpc

https://github.com/xtaci/kcp-go/blob/9f0f2bc5547c9ae7943d1a729f382494e4a2fd65/tx_linux.go#L25 Maybe good solution! https://github.com/golang/net/blob/master/ipv4/batch.go#L108 call https://github.com/golang/net/blob/master/internal/socket/sys_linux.go#L24:6 http://man7.org/linux/man-pages/man2/sendmmsg.2.html

adwpc avatar Feb 13 '20 09:02 adwpc

I have tested PacketConn.WriteBatch in pion/ice package, it reduce 30%~40% cpu in pion/ion https://godoc.org/golang.org/x/net/ipv4#PacketConn.WriteBatch

This conn.WriteTo conn is from here? https://github.com/pion/ice/blob/master/candidate_base.go#L152 https://github.com/pion/ice/blob/master/gather.go#L138 https://github.com/pion/transport/blob/master/vnet/net.go#L555

My question is where to finish this code? in the ice package or transport package? @Sean-Der

adwpc avatar Mar 03 '20 16:03 adwpc

:fire: that's amazing! Yes we need to do this @adwpc

I will take it. I also have benchmarks I am running will post results before/after.

Sean-Der avatar Mar 03 '20 19:03 Sean-Der

🔥 that's amazing! Yes we need to do this @adwpc

I will take it. I also have benchmarks I am running will post results before/after.

@Sean-Der I can make a PR to pion/ice if you need as a reference, should save your precious time :)

adwpc avatar Mar 04 '20 03:03 adwpc

I think it only work on linux

// WriteBatch writes a batch of messages.
//
// The provided flags is a set of platform-dependent flags, such as
// syscall.MSG_DONTROUTE.
//
// It returns the number of messages written on a successful write.
//
// On Linux, a batch write will be optimized.
// On other platforms, this method will write only a single message.

notedit avatar Mar 11 '20 08:03 notedit

How much does it increase latency?

jech avatar Sep 05 '20 12:09 jech

kernel context switches COST. a single sendmmsg call has nearly no measurable overhead difference between a sendmsg call for a single packet, but you can bundle up a lot of packets.

Also see ngtcp2 which has similar mechanisms in it that just landed for quic.

dtaht avatar Jul 06 '21 17:07 dtaht