Updated EN_KCP Best Practice (markdown)

winlin 2017-02-17 09:45:54 +08:00
parent 505b896a04
commit 772940e354

@ -1,16 +1,17 @@
# 最佳实践
#### 内存分配器
# BestPractice
默认KCP协议使用 malloc/free进行内存分配释放如果应用层接管了内存分配可以用ikcp_allocator来设置新的内存分配器注意要在一开始设置
#### 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
为了进一步提高传输速度下层协议也许会使用前向纠错技术。需要注意前向纠错会根据冗余信息解出原始数据包。相同的原始数据包不要两次input到KCP否则将会导致 kcp以为对方重发了这样会产生更多的ack占用额外带宽。
As concrete transport is specified by application, which integrate KCP(For more information, read [Usage](EN_KCP-Basic-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:
```cpp
Fn = (Pn, Pn-1, Pn-2)
@ -21,11 +22,10 @@ P2 = (2, 1, 0)
P3 = (3, 2, 1)
```
这样几个包发送出去接收方对于单个原始包都可能被解出3次来后面两个包任然会重复该包内容那么这里需要记录一下一个下层数据包只会input给kcp一次避免过多重复ack带来的浪费。
The recever can recover previous two packet from one packet. User should reduce the duplicated packets then feed to KCP.
#### 管理大规模连接
#### Efficient Update
如果需要同时管理大规模的 KCP连接比如大于3000个比如你正在实现一套类 epoll的机制那么为了避免每秒钟对每个连接调用大量的调用 ikcp_update我们可以使用 ikcp_check 来大大减少 ikcp_update调用的次数。 ikcp_check返回值会告诉你需要在什么时间点再次调用 ikcp_update如果中途没有 ikcp_send, ikcp_input的话否则中途调用了 ikcp_send, ikcp_input的话需要在下一次interval时调用 update
For server-side application, for example linux application server, `select` or `epool` is often used in `non-blocking` `asnc` 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`).
标准顺序是每次调用了 ikcp_update后使用 ikcp_check决定下次什么时间点再次调用 ikcp_update而如果中途发生了 ikcp_send, ikcp_input 的话,在下一轮 interval 立马调用 ikcp_update和 ikcp_check。 使用该方法原来在处理2000个 kcp连接且每
个连接每10ms调用一次update改为 check机制后cpu从 60%降低到 15%。
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`.