5 EN_KCP Best Practice
Yuanchao Sun edited this page 2017-06-28 14:30:39 +08:00

BestPractice

Memory Management

KCP use malloc/free for memory management, user can use ikcp_allocator to use other solution. Remember to setup it at the first line of main():

ikcp_allocator(my_new_malloc, my_new_free);

Reduce Redundancy for FEC

As concrete transport is specified by application, which integrate KCP(For more information, read Usage). When FEC is used by application-level, it may contains some redundancy packets. User should feed KCP(by ikcp_input) by the packets without redundancy, that is, if received packets p0,p1,p0,p2, user should feed KCP p0,p1,p2, rather than p0,p1,p0,p2, the duplicated p0 should never feed to KCP, or there maybe more ACK packets.

For example, the FEC in application use triple redundancy:

Fn = (Pn, Pn-1, Pn-2)

P0 = (0, X, X)
P1 = (1, 0, X)
P2 = (2, 1, 0)
P3 = (3, 2, 1)

The recever can recover previous two packet from one packet. User should reduce the duplicated packets then feed to KCP.

Advance Update

For server-side application, for example linux application server, select or epoll is often used in non-blocking async architecture, to serve over 10k clients. For these use scenarios, there are usually huge connections(>3k) to serve, it may cause performance issue when use ikcp_update for each connection to upddate, user should better use ikcp_check to replace ikcp_update. Unlike ikcp_update to update KCP every N ms, ikcp_check tells us the exactly time to update the KCP(Only need to update when called ikcp_send or ikcp_input).

For example, user can use ikcp_udpate at the first time, then use ikcp_check to get the next time to call ikcp_update. It's reported that the CPU usage descreate form 60% to 15% when use ikcp_check.