﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-xiaoxiaoling</title><link>http://www.cppblog.com/xiaoxiaoling/</link><description /><language>zh-cn</language><lastBuildDate>Wed, 08 Apr 2026 08:09:07 GMT</lastBuildDate><pubDate>Wed, 08 Apr 2026 08:09:07 GMT</pubDate><ttl>60</ttl><item><title>将博客搬至CSDN</title><link>http://www.cppblog.com/xiaoxiaoling/archive/2019/01/29/216224.html</link><dc:creator>clcl</dc:creator><author>clcl</author><pubDate>Tue, 29 Jan 2019 02:39:00 GMT</pubDate><guid>http://www.cppblog.com/xiaoxiaoling/archive/2019/01/29/216224.html</guid><wfw:comment>http://www.cppblog.com/xiaoxiaoling/comments/216224.html</wfw:comment><comments>http://www.cppblog.com/xiaoxiaoling/archive/2019/01/29/216224.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/xiaoxiaoling/comments/commentRss/216224.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/xiaoxiaoling/services/trackbacks/216224.html</trackback:ping><description><![CDATA[<span style="color: #c92027; font-family: &quot;SF Pro Display&quot;, Roboto, Noto, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, sans-serif; font-size: 12px; background-color: #ffffff;">将博客搬至CSDN</span><img src ="http://www.cppblog.com/xiaoxiaoling/aggbug/216224.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/xiaoxiaoling/" target="_blank">clcl</a> 2019-01-29 10:39 <a href="http://www.cppblog.com/xiaoxiaoling/archive/2019/01/29/216224.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>epoll那些事儿</title><link>http://www.cppblog.com/xiaoxiaoling/archive/2018/07/14/215782.html</link><dc:creator>clcl</dc:creator><author>clcl</author><pubDate>Sat, 14 Jul 2018 03:17:00 GMT</pubDate><guid>http://www.cppblog.com/xiaoxiaoling/archive/2018/07/14/215782.html</guid><wfw:comment>http://www.cppblog.com/xiaoxiaoling/comments/215782.html</wfw:comment><comments>http://www.cppblog.com/xiaoxiaoling/archive/2018/07/14/215782.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/xiaoxiaoling/comments/commentRss/215782.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/xiaoxiaoling/services/trackbacks/215782.html</trackback:ping><description><![CDATA[<p><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;">ET和LT: &nbsp;&nbsp;</span><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;"><br /></span><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;">&nbsp; &nbsp;LT一般用在单线程。</span><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;"><br /></span><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;">&nbsp; &nbsp;ET和EPOLLONESHOT配合用在多线程共享一个epoll环境下，EPOLLONESHOT标记触发过的事件从epoll中移除，下次必须重新注册，用来防止多线程同时取到同一个socket的事件产生冲突。</span><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;"><br /></span><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;"><br /></span><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;">epoll_wait 第三个参数 取事件数量：</span><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;"><br /></span><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;">&nbsp; &nbsp;单线程模型当然尽可能一次多取一些效率高，多线程为了防止一个线程把所有事件取完其他线程饥饿，ACE实现是只取1个。</span><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;"><br /></span><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;"><br /></span><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;">错误处理：</span><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;"><br /></span><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;">&nbsp; &nbsp;EAGIN | EINTR |&nbsp;EWOULDBLOCK 重试。</span><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;"><br /></span><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;">&nbsp;&nbsp;&nbsp;EPOLLERR | EPOLLHUP | EPOLLRDHUP 断开连接。</span><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;"><br /></span><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;"><br /></span><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;">惊群：</span><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;"><br /></span><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;">&nbsp; &nbsp;默认系统都会有这问题，据说新系统有修复不过还是处理一下比较好，一般解决方案是同时只有一个线程等待accept，可以单独线程accept，将连接在分给其他工作线程。nginx是多进程模型，使用了基于共享内存的互斥锁，使得同时只有一个工作进程的epoll含有accept的socket，通过这种方式实现连接数上的负载均衡（连接数少的工作进程得到accept锁的概率高）。<br /><br /></span>&nbsp; &nbsp;<span style="font-size: 12pt;">为了避免大数据量io时，et模式下只处理一个fd,其他fd被饿死的情况发生。linux建议可以在fd联系到的结构（一般都会自己封装一个包含fd和读写缓冲的结构体）中增加ready位，然后epoll_wait触发事件之后仅将其置位为ready模式，然后在下边轮询ready fd列表。</span></p><p><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;"><br /></span><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;">epoll实现： &nbsp;</span><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;"><br /></span><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;">&nbsp; &nbsp;</span><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;">epoll内部用了一个红黑树记录添加的socket，用了一个双向链表接收内核触发的事件。</span></p><p><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;">&nbsp; &nbsp;注册的事件挂载在红黑树中(红黑树的插入时间效率是logN，其中n为树的高度)。</span></p><p><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;">&nbsp; &nbsp;挂载的事件会与设备(网卡)驱动建立回调关系，也就是说，当相应的事件发生时会调用这个回调方法。这个回调方法在内核中叫ep_poll_callback,它会将发生的事件添加到rdlist双链表中。</span></p><p><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;">&nbsp; &nbsp;使用mmap映射内存，减少内核态和用户态的不同内存地址空间拷贝开销。</span><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;">每次注册新的事件到epoll中时，会把fd拷贝进内核，通过内核于用户空间mmap同一块内存</span><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;">保证了只会拷贝一次。（返回的时候不需要拷贝，select要）</span></p><p><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;">&nbsp; &nbsp;执行epoll_ctl时，除了把socket放到红黑树上，还会给内核中断处理程序注册一个回调函数，告诉内核，如果这个句柄的中断到了，就把它放到准备就绪list链表里。所以，当一个socket上有数据到了，内核在把网卡上的数据copy到内核中后就把socket插入到准备就绪链表里了。链表又是通过mmap映射的空间，所以在传递给用户程序的时候不需要复制（这也是为什么比select效率高的原因，epoll_wait返回的只是就绪队列，不需要轮询 不需要复制完成的事件列表，select，poll实现需要自己不断轮询所有fd集合，直到设备就绪）。</span></p><p><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;">&nbsp; &nbsp;epoll_wait最后会检查socket，如果是 LT，并且这些socket上确实有未处理的事件时，又把该句柄放回到刚刚清空的准备就绪链表了（LT比ET低效的原因）。</span></p><p>&nbsp;</p><p><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;">可见，如果没有大量的空闲，无效连接，epoll效率不比select高。</span><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;"><br /></span><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;"><br /></span><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;">测试数据（仅是刚接触go的时候好奇做的参考意义的测试）： &nbsp;</span><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;"><br /></span><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;"><br /></span><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;">&nbsp; &nbsp;同样的环境，echo服务器测并发io，单线程epoll qps:45000左右，每连接/协程 go： 50000多，多线程epoll（开6个epoll，每个epoll开8线程，一共48线程）：qps 70000多。</span></p><p>&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; line-height: 1.5; font-size: 13px; font-family: Verdana, Arial, Helvetica, sans-serif; background-color: #fefef2;"><span style="font-size: 12pt;"></span><br /></p><img src ="http://www.cppblog.com/xiaoxiaoling/aggbug/215782.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/xiaoxiaoling/" target="_blank">clcl</a> 2018-07-14 11:17 <a href="http://www.cppblog.com/xiaoxiaoling/archive/2018/07/14/215782.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>tcp/udp，socket那些事儿</title><link>http://www.cppblog.com/xiaoxiaoling/archive/2018/07/13/215781.html</link><dc:creator>clcl</dc:creator><author>clcl</author><pubDate>Fri, 13 Jul 2018 07:49:00 GMT</pubDate><guid>http://www.cppblog.com/xiaoxiaoling/archive/2018/07/13/215781.html</guid><wfw:comment>http://www.cppblog.com/xiaoxiaoling/comments/215781.html</wfw:comment><comments>http://www.cppblog.com/xiaoxiaoling/archive/2018/07/13/215781.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/xiaoxiaoling/comments/commentRss/215781.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/xiaoxiaoling/services/trackbacks/215781.html</trackback:ping><description><![CDATA[<div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 12pt;"><strong>概念</strong> ：  <br /></span>&nbsp;&nbsp;&nbsp;<span style="font-size: 12pt;">tcp和udp，连接和无连接都是协议，是共享物理介质的传输数据的应用程序之间的约定。面向连接的协议维护了segment的状态和次序。<br /><br /></span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 12pt;"><strong>故障</strong> :</span><br /><span style="font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-size: 12pt;">默认</span><span style="font-size: 12pt;">无kee</span><span style="font-size: 12pt;">p alive：</span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; text-indent: 28px; line-height: 1.75;"><span style="font-size: 12pt;">拔网线或路</span><span style="font-size: 12pt;">由器崩溃：发送端超时（重传12次大约9分钟）后放弃，接收端读errorno ETIMEOUT，如果没有读则要等到下一次写失败sigpipe。如果中间路由器无法转发则向源端发送 ICMP 目标主机不可达。</span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; text-indent: 28px; line-height: 1.75;"><span style="font-size: 12pt;">程序退出（包括崩溃）： 程序退出和正常调用 close无法区分，都会返回FIN表示退出，如果一端退出，</span><br /><span style="font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;另一端： 1.第一次写合法（接收到fin后还是能继续发送数据）第二次写的时候发现连接不存在，得到 RST  RESET错误  2.读的时候得到  conn reset错误，继续写则被SIGPIPE信号中止，程序退出。</span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; text-indent: 28px; line-height: 1.75;"></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; text-indent: 28px; line-height: 1.75;"><span style="font-size: 12pt;">主机宕机： 宕机后无法通过FIN通知对方，对方会继续重传直到timeout。如果超时前宕机的主机重启了，此时收到重传的主机没有连接记录，向源返回rst，发送端得到ECONNRESET错误，如果发送端在读得到  conn reset错误，继续写则被SIGPIPE信号中止，程序退出。</span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; text-indent: 28px; line-height: 1.75;"></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 12pt;">开启keep alive情况下：</span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; text-indent: 28px; line-height: 1.75;"><span style="font-size: 12pt;">如果程序崩溃返回fin ， 如果主机可达但程序不存在（主机重启），则响应RSt，源端得到ECONNRESET 错误。</span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; text-indent: 28px; line-height: 1.75;"><span style="font-size: 12pt;">如果对方没有对keep alive响应ACK 或者RST，源端TIMEOU</span><span style="font-size: 12pt;">T（</span><span style="font-size: 12pt;">重试9次，每次间隔75秒，定时器2小时超时后的11.25分钟）</span><span style="font-size: 12pt;"> 所</span><span style="font-size: 12pt;">以程序自己做心跳还是必要的。</span><br /><br /><span style="font-size: 12pt;"><strong>细节</strong>：<br /></span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 12pt;">tcp写操作：</span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; text-indent: 28px; line-height: 1.75;"><span style="font-size: 12pt;">用户态拷贝到内核态写缓冲区后返回，只返回明显错误：socket无效或缓冲区无效。影响的因素有：发送窗口，拥塞窗口，写缓冲区大小，Nagle。</span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; text-indent: 28px; line-height: 1.75;"><span style="font-size: 12pt;">tcp是提高带宽利用率的协议，每次发送倾向mss大小，同时不能大于对端指定的大小（发送窗口），tcp只考虑了网络中路由器缓冲区耗尽情况（也是tcp的限制）所以有拥塞窗口和慢启动：为了防止网络拥塞（自律的协议）每次发送的不能大于对方指定的（发送窗口）和自己给自己限制的（拥塞窗口）。<br /><br /><img src="http://www.cppblog.com/images/cppblog_com/xiaoxiaoling/微信图片_20180713155119.png" border="0" alt="" /><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;</div><p style="margin-bottom: 10.5pt; margin-left: 0pt; text-indent: 21pt; padding: 0pt;"><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;">慢启动：一开始指数级的增加拥塞窗口，到一个门阀值后变成线性的， 之后每次超时都把门阀值降低到原来一半（并且rto翻倍，</span><span style="font-size: 12pt;">TCP超时计算是RTOx2，这样连续丢三次包就变成RTOx8了，十分恐怖</span><span style="font-family: Verdana; font-size: 12pt; letter-spacing: 0pt; text-indent: 21pt;">），拥塞窗口设置为1重新开始慢启动（指数级增加）。一切都是为了让路由器有时间处理积压的缓冲。（所以不适用于频繁断开连接的移动网络，这也是为什么以前的下载工具开多条tcp传输速度更快的原因）。</span></p><p style="margin-bottom: 10.5pt; margin-left: 0pt; text-indent: 21pt; padding: 0pt;"><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;">Nagle算法：第一次（此时没有等待ack确认，空闲连接）发送小包成功，第二次继续发送 ：哪怕发送窗口，拥塞窗口都很大，之前的包没有ack确认依旧不让发直到收到之前的 ack确认。</span></p><p style="margin-bottom: 10.5pt; margin-left: 0pt; text-indent: 21pt; padding: 0pt;"><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;">shutdown和close的区别:</span></p><p style="margin-bottom: 10.5pt; margin-left: 0pt; text-indent: 21pt; padding: 0pt;"><span style="font-family: Verdana; letter-spacing: 0pt; font-size: 12pt;">close只是递减引用计数， shutdown的半关闭会影响所有的进程。</span></p><div yne-bulb-block="paragraph" style="white-space: pre-wrap; text-indent: 28px; line-height: 1.75;"><span style="font-size: 12pt;"></span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; text-indent: 28px; line-height: 1.75;"><span style="font-size: 12pt;">shutdown how=0  关闭读，读会返回eof</span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; text-indent: 28px; line-height: 1.75;"><span style="font-size: 12pt;">shutdown how=1  关闭写，任何写会出错，将缓冲区的发送完后会发送fin表示没有数据了，</span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; text-indent: 28px; line-height: 1.75;"></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; text-indent: 28px; line-height: 1.75;"><span style="font-size: 12pt;">收到对方发送的fin，recv会返回0。</span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; text-indent: 28px; line-height: 1.75;"></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; text-indent: 28px; line-height: 1.75;"><span style="font-size: 12pt;">listen 的第二个参数制定的是全连接队列大小（accept）<br /><br /></span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; text-indent: 28px; line-height: 1.75;"></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 12pt;">time_wait和close_wait</span><span style="text-indent: 28px;"> </span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; text-indent: 28px; line-height: 1.75;"><span style="font-size: 12pt;">time_wait  出现在主动close方，为了防止新连接收到旧链接的数据包， </span><span style="font-size: 12pt;">数量高</span><span style="font-size: 12pt;">可以通过设置内核参数缩短时间降低数值（2msl）。可以通过 SO_LINGER关闭。</span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; text-indent: 28px; line-height: 1.75;"><span style="font-size: 12pt;">close_wait 出现在被动close方，数量高一般因为性能或者bug在收到fin后没有调用close。<br /><br /></span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 12pt;">SO_REUSEADDR</span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; text-indent: 28px; line-height: 1.75;"><span style="font-size: 12pt;">用于程序崩溃后重启，解决地址被占用问题（time_wait），另一个用途是开两个服务，第一个制定地址，第二个指定INADDR_ANY 通配地址，如果客户端连接的是制定地址一样会到第一个socket上。<br /></span><span style="font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;对于绑定于同一地址端口组合上的UDP socket，kernel尝试在它们之间平均分配收到的数据包；对于绑定于同一地址端口组合上的TCP监听socket，kernel尝试在它们之间平均分配收到的连接请求（调用accept()方法所得到的请求）。</span><span style="font-size: 12pt;"><br /></span><span style="font-size: 12pt;"><br /></span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; text-indent: 28px; line-height: 1.75;"></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 12pt;">Nagle和延迟ack的问题：</span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; text-indent: 28px; line-height: 1.75;"><span style="font-size: 12pt;">发送端数据未发送完，被nagle禁止发送（必须等待接收端的ack，也就是没有未确认的包才允许发送），此时接收端ack被延迟发送（希望将ack和数据一起发送提高带宽利用率）而发送方数据未发送完导致接收端无法回复，双方死锁。</span></div><div yne-bulb-block="image" style="float: none;"><img data-media-type="image" src="C:/Users/Administrator/AppData/Local/YNote/data/xiaoxiaoling20@126.com/3473ea24da284e39b937ce3e866c349f/clipboard.png" alt="" data-attr-org-src-id="20FCAEE06EFA4023A0C4E0A0CC8CA880" /></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; text-indent: 28px; line-height: 1.75;"><img src="http://www.cppblog.com/images/cppblog_com/xiaoxiaoling/clipboard.png" border="0" alt="" /></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; text-indent: 28px; line-height: 1.75;"></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; line-height: 1.75;"><span style="font-size: 12pt;">已连接的UDP：</span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; text-indent: 28px; line-height: 1.75;"><span style="font-size: 12pt;">tcp的connect是开始三次握手，将远程的ip端口绑定到socket，而bind是绑定本地的ip端口。</span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; text-indent: 28px; line-height: 1.75;"><span style="font-size: 12pt;">udp没有三次握手，connect纯粹是本地行为，将远程的ip端口绑定到socket上。</span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; text-indent: 28px; line-height: 1.75;"><span style="font-size: 12pt;">已连接的udp可以不用sendto，sendto会先将socket和目的地址连接，然后发送数据后再断开，而已连接的已经绑定地址可以用write，提高效率（资料显示暂时连接断开所用的时间是传输udp数据的三分之一，还是很可观的）。异步错误的接收，使用sendto发送完系统就没有记录了，应用程序无法接收之后icmp返回的错误。</span></div><div yne-bulb-block="paragraph" style="white-space: pre-wrap; text-indent: 28px; line-height: 1.75;"><span style="font-size: 12pt;">接收端已连接udp的好处： 可以标识哪个socket是哪个用户，另外还可以独占连接，再另一个地方调用recvfrom指定相同地址会返回ECONNREFUSED，因为此连接已经被绑定。</span></div><img src ="http://www.cppblog.com/xiaoxiaoling/aggbug/215781.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/xiaoxiaoling/" target="_blank">clcl</a> 2018-07-13 15:49 <a href="http://www.cppblog.com/xiaoxiaoling/archive/2018/07/13/215781.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>初识cdn</title><link>http://www.cppblog.com/xiaoxiaoling/archive/2018/05/09/215630.html</link><dc:creator>clcl</dc:creator><author>clcl</author><pubDate>Wed, 09 May 2018 15:57:00 GMT</pubDate><guid>http://www.cppblog.com/xiaoxiaoling/archive/2018/05/09/215630.html</guid><wfw:comment>http://www.cppblog.com/xiaoxiaoling/comments/215630.html</wfw:comment><comments>http://www.cppblog.com/xiaoxiaoling/archive/2018/05/09/215630.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/xiaoxiaoling/comments/commentRss/215630.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/xiaoxiaoling/services/trackbacks/215630.html</trackback:ping><description><![CDATA[<span style="font-size: 12pt;">&nbsp;&nbsp;&nbsp;工作上虽然和cdn没关系但是技术方向还是很多相似之处的，例如：负载均衡，缓存，分布式，路由等。</span><br /><span style="font-size: 12pt;">&nbsp;&nbsp;&nbsp;cdn简单的说就是个复杂的大缓存，由于目标用户（包括源和端）广泛直接导致了其复杂性，遍布广节点多则需要分流负载甚至自组织，应用繁杂则需要分流路由，提速则需要缓存，稳定则需要监控调度，为了透明则需要各种映射。</span><br /><span style="font-size: 12pt;">&nbsp; &nbsp;由于接入面广和网络的复杂性，不可能让客户端直接面对源，于是就有了专门接入客户端的边缘服务器/组，这些边缘服务器和后端的调度，监控，源服务器通讯。既然是缓存就</span><span style="font-size: 12pt;">涉及到数据一致性的问题，</span><span style="font-size: 12pt;">最简单的就是各接入端的边缘服务器在需要的时候到后台拉取，或者更智能的他们之间可以相互拉取，甚至后台调度提前推送。</span><br /><span style="font-size: 12pt;">&nbsp; &nbsp;边缘接收到请求后首当其冲的问题就是对请求的内容 &#8220;去哪找</span><span style="font-size: 12pt;">&#8221;和&#8220;怎么去</span><span style="font-size: 12pt;">&#8221;，想要知道内容的位置，一般缓存的实现不外乎就是统一到一个目录服务器找，或者广播所有自己知道的节点问一圈，更高效的方法是将请求url哈希后直接找到目的地址，当然只要是缓存都会过期也就有TTL的概念。&#8220;怎么去&#8221;的方式多种多样，由于互联网web服务居多基于DNS的路由使用最广泛，缺点是客户端和中继点会缓存，更新需要一定时间。HTTP重定向，URL改写，也有直接在网络设备路由器上做，直接在路由表中保持路径。这些方法在cdn这个庞杂的系统中根据需要使用，例如边缘服务器为了接收到客户端的请求可以使用dns重定向，然后再用哈希或者url改写转发到后端的源。&nbsp;<br /></span><span style="font-size: 12pt;">&nbsp; &nbsp;现如今的网络内容有许多是动态生成的，对这些无法提前缓存的内容可以直接略过只存静态内容（分段缓存），组装的时候发送回客户端或者直接赋予边缘服务器生成动态内容的能力（边缘计算）当然这对网页的制作有规范。</span><br /><span style="font-size: 12pt;">&nbsp; &nbsp;面向大众的服务都有潮汐效应，在热点时段的访问量是平时的几十上百倍（根据二八理论，80%的问题都是在20%的地方出现的，热点访问量也是大多访问小部分数据，如果能提前将热点缓存于各边缘服务器最直接有效），如果有自适应的动态调整功能整个服务会健壮很多。边缘服务器是离用户最近的，可以将每个节点看成组，组长监控负载自适应的添加删除组员（缓存服务器）以及更新dns，不允许组员跨组拉数据。当然如果客户端之间可以使用P2P相互取数据也是一个办法。&nbsp;&nbsp;&nbsp;</span><br /><span style="font-size: 12pt;">&nbsp; &nbsp;当前出现了基于流媒体的cdn，视频内容分发在后端以文件的形式传输（适合传输的格式更高效），到边缘服务器再以流的形式和客户端传输（不需要全部传完即可开始播放）。同时也要综合考虑时段需求，视频编码策略也很重要。</span><img src ="http://www.cppblog.com/xiaoxiaoling/aggbug/215630.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/xiaoxiaoling/" target="_blank">clcl</a> 2018-05-09 23:57 <a href="http://www.cppblog.com/xiaoxiaoling/archive/2018/05/09/215630.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于dpdk 的思考</title><link>http://www.cppblog.com/xiaoxiaoling/archive/2017/02/05/214651.html</link><dc:creator>clcl</dc:creator><author>clcl</author><pubDate>Sun, 05 Feb 2017 06:08:00 GMT</pubDate><guid>http://www.cppblog.com/xiaoxiaoling/archive/2017/02/05/214651.html</guid><wfw:comment>http://www.cppblog.com/xiaoxiaoling/comments/214651.html</wfw:comment><comments>http://www.cppblog.com/xiaoxiaoling/archive/2017/02/05/214651.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/xiaoxiaoling/comments/commentRss/214651.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/xiaoxiaoling/services/trackbacks/214651.html</trackback:ping><description><![CDATA[<p><br /></p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">dpdk是通过许多不同的纬度来加速包处理的，其中主要包括：</span></p><p>&nbsp;</p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">hugepage大页内存(进程使用的是虚拟地址，一般页表(4k)能映射的虚拟地址空间有限，使用大页能减少换页次数提高cache命中，通过mmap把大页映射到用户态的虚拟地址空间有用过mmap的都知道这是实现共享内存的手段，所以dpdk还支持多进程共享内存)</span></p><p>&nbsp;</p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">cache预取 (每次预读当前数据相邻前后的数据)，批量操作数据，cache line对齐(通过浪费一点内存将要操作的数据对齐)</span></p><p>&nbsp;</p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">接管了网卡用户态驱动使用轮询而不是网卡中断</span></p><p>&nbsp;</p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">将网卡rx tx队列映射到用户态空间实现真正的零拷贝（传统堆栈至少也得一次拷贝，因为队列空间在内核而内核和用户态使用不同的地址空间）(传统堆栈为了支持通用性，例如ipx等其他网络，将包处理过程分了很多层次，层之间的接口标准统一数据结构就需要转换，无形中带来了巨大的成本，如osi七层模型而实用的就是tcp/ip四层模型</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">)</span></p><p>&nbsp;</p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">线程绑定cpu</span></p><p>&nbsp;</p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">支持NUMA，不同的core属于不同的node，每个node有自己的mempool减少冲突</span></p><p>&nbsp;</p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">无锁环形队列(冲突发生时也是一次cas的开销)</span></p><p>&nbsp;</p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">dpdk通过tools/dpdk-setup.sh的脚本，通过编译、挂载内核模块， 绑定网卡（先把网卡ifconfig down），设置hugepage后就可以使用了。</span></p><p>&nbsp;</p><p>&nbsp;</p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">在内核模块igb加载时，会注册pci设备驱动</span></p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">static struct pci_driver igbuio_pci_driver = {</span></p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">	</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">.name = "igb_uio",</span></p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">	</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">.id_table = NULL,</span></p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">	</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">.probe = igbuio_pci_probe,</span></p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">	</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">.remove = igbuio_pci_remove,</span></p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">};</span></p><p>&nbsp;</p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">在绑定网卡时，会调用igbuio_pci_probe，使用用户态驱动uio接管网卡(中断处理、mmap映射设备内存到用户空间)</span></p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">系统启动时，bios会将设备总线地址信息记录在/sys/bus/pci/devices，dpdk程序启动时会去这里扫描pci设备，根据不同类型的NIC有对应的初始化流程。在后面配置队列的时候会把用户态的队列内存地址通过硬件指令交给NIC，从而实现零拷贝。</span></p><p><img src="http://www.cppblog.com/images/cppblog_com/xiaoxiaoling/图片1.png" width="790" height="772" alt="" /><br /></p><p>&nbsp;</p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">如果</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">NIC收到包，会做标记，轮询的时候通过标记取数据包</span></p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">while (nb_rx &lt; nb_pkts) {</span></p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">	</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">	</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">/*</span></p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">	</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">	</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">&nbsp;* The order of operations here is important as the DD status</span></p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">	</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">	</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">&nbsp;* bit must not be read after any other descriptor fields.</span></p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">	</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">	</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">&nbsp;* rx_ring and rxdp are pointing to volatile data so the order</span></p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">	</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">	</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">&nbsp;* of accesses cannot be reordered by the compiler. If they were</span></p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">	</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">	</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">&nbsp;* not volatile, they could be reordered which could lead to</span></p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">	</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">	</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">&nbsp;* using invalid descriptor fields when read from rxd.</span></p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">	</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">	</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">&nbsp;*/</span></p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">	</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">	</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">rxdp = &amp;rx_ring[rx_id];</span></p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">	</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">	</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">staterr = rxdp-&gt;wb.upper.status_error;</span></p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">	</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">	</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">if (! (staterr &amp; rte_cpu_to_le_32(E1000_RXD_STAT_DD)))</span></p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">	</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">	</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">	</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">break;</span></p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">	</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">	</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">rxd = *rxdp;</span></p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">发包的轮询就是轮询发包结束的硬件标志位，硬件发包完成会写回标志位，驱动发现后再释放对应的描述符和缓冲块。</span></p><p>&nbsp;</p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">KNI</span></p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">通过创建一个虚拟网卡，将收到的包丢给协议栈</span></p><p><span style="mso-spacerun:'yes';font-family:Calibri;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">&nbsp;/* 发送skb到协议栈 */</span></p><p><span style="mso-spacerun:'yes';font-family:Calibri;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* Call </span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">netif</span><span style="mso-spacerun:'yes';font-family:Calibri;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">&nbsp;interface */</span></p><p><span style="mso-spacerun:'yes';font-family:Calibri;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">netif</span><span style="mso-spacerun:'yes';font-family:Calibri;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">_receive_skb(skb);</span></p><p>&nbsp;</p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">POWER </span></p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">在负载小的时候没有必要使用轮询模式，这时可以打开网卡中断 使用eventfd &nbsp;epoll通知用户层</span></p><p>&nbsp;</p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">Ring</span></p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">无锁环形队列的核心就是操作头尾索引，先将头尾索引赋给临时变量，再把尾索引往后跳n个位置，利用cas判断头如果还是在原来的位置就指向尾否则就重复这个过程，然后在操作中间跳过的n个元素就是安全的了，此时头尾索引应该指向同一个位置，如果不同应该是有别的线程也在操作，重复等待即可。(这里有个细节，索引是只加不减的，因为是环形队列索引又是unsigned 32bits，所以每次取数据前把索引模队列长度-1， uint32_t mask; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/**&lt; Mask (size-1) of ring. */即可</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">)</span></p><p>&nbsp;</p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">Windows下使用vmware虚拟机的时候出现EAL: Error</span><span style="mso-spacerun:'yes';font-family:Calibri;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">&nbsp;reading&nbsp;from&nbsp;file&nbsp;descriptor</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">，根据网上的说法打了patch还是不行，后来尝试挂载内核模块的时候不加载vfio模块就可以了</span></p><p>&nbsp;</p><img src ="http://www.cppblog.com/xiaoxiaoling/aggbug/214651.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/xiaoxiaoling/" target="_blank">clcl</a> 2017-02-05 14:08 <a href="http://www.cppblog.com/xiaoxiaoling/archive/2017/02/05/214651.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Redis解析</title><link>http://www.cppblog.com/xiaoxiaoling/archive/2017/01/24/214634.html</link><dc:creator>clcl</dc:creator><author>clcl</author><pubDate>Tue, 24 Jan 2017 10:13:00 GMT</pubDate><guid>http://www.cppblog.com/xiaoxiaoling/archive/2017/01/24/214634.html</guid><wfw:comment>http://www.cppblog.com/xiaoxiaoling/comments/214634.html</wfw:comment><comments>http://www.cppblog.com/xiaoxiaoling/archive/2017/01/24/214634.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/xiaoxiaoling/comments/commentRss/214634.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/xiaoxiaoling/services/trackbacks/214634.html</trackback:ping><description><![CDATA[<p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;"></span></p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">Redis是工作中很常用的，这里将比较普遍使用的结构研究了下做个备忘。</span></p><p>&nbsp;</p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">hash</span></p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">实现和dnspod的dataset半斤八两，本质上是个二维数组，通过将key哈希作为一维的下表，第二维的数组存相同哈希的元素，查找使用遍历的方式，所以这里redis做了优化，当满足条件的时候(数组数量太大)会进行rehash，动态扩大桶的数量来减少最后一维遍历的次数.</span></p><table style="border-collapse:collapse;width:292.3500pt;margin-left:5.4000pt;"><tbody><tr><td width="140" valign="top" style="width:105.7000pt;padding:0.0000pt 0.0000pt 0.0000pt 0.0000pt ;border-left:31.8750pt none #ffffff;border-right:31.8750pt none #ffffff;border-top:31.8750pt none #ffffff;border-bottom:31.8750pt none #ffffff;"><p style="margin-top:0.0000pt;margin-right:0.0000pt;margin-bottom:16.5000pt;margin-left:0.0000pt;vertical-align:baseline;line-height:19.5000pt;"><span style="font-family: 'Courier New'; color: #666666; letter-spacing: 0pt; font-size: 11pt;">函数名称</span></p></td><td width="204" valign="top" style="width:153.5500pt;padding:0.0000pt 0.0000pt 0.0000pt 0.0000pt ;border-left:none;;;border-right:31.8750pt none #ffffff;border-top:31.8750pt none #ffffff;border-bottom:31.8750pt none #ffffff;"><p style="margin-top:0.0000pt;margin-right:0.0000pt;margin-bottom:16.5000pt;margin-left:0.0000pt;vertical-align:baseline;line-height:19.5000pt;"><span style="font-family: 'Courier New'; color: #666666; letter-spacing: 0pt; font-size: 11pt;">作用</span></p></td><td width="44" valign="top" style="width:33.1000pt;padding:0.0000pt 0.0000pt 0.0000pt 0.0000pt ;border-left:none;;;border-right:31.8750pt none #ffffff;border-top:31.8750pt none #ffffff;border-bottom:31.8750pt none #ffffff;"><p style="margin-top:0.0000pt;margin-right:0.0000pt;margin-bottom:16.5000pt;margin-left:0.0000pt;vertical-align:baseline;line-height:19.5000pt;"><span style="font-family: 'Courier New'; color: #666666; letter-spacing: 0pt; font-size: 11pt;">复杂度</span></p></td></tr><tr><td width="140" valign="top" style="width:105.7000pt;padding:0.0000pt 0.0000pt 0.0000pt 0.0000pt ;border-left:31.8750pt none #ffffff;border-right:31.8750pt none #ffffff;border-top:none;;border-bottom:31.8750pt none #ffffff;"><p style="margin-top:0.0000pt;margin-right:0.0000pt;margin-bottom:16.5000pt;margin-left:0.0000pt;vertical-align:baseline;line-height:19.5000pt;"><span style="font-family: 'Courier New'; color: #666666; letter-spacing: 0pt; font-size: 11pt;">dictCreate</span></p></td><td width="204" valign="top" style="width:153.5500pt;padding:0.0000pt 0.0000pt 0.0000pt 0.0000pt ;border-left:none;;;border-right:31.8750pt none #ffffff;border-top:none;;border-bottom:31.8750pt none #ffffff;"><p style="margin-top:0.0000pt;margin-right:0.0000pt;margin-bottom:16.5000pt;margin-left:0.0000pt;vertical-align:baseline;line-height:19.5000pt;"><span style="font-family: 'Courier New'; color: #666666; letter-spacing: 0pt; font-size: 11pt;">创建一个新字典</span></p></td><td width="44" valign="top" style="width:33.1000pt;padding:0.0000pt 0.0000pt 0.0000pt 0.0000pt ;border-left:none;;;border-right:31.8750pt none #ffffff;border-top:none;;border-bottom:31.8750pt none #ffffff;"><p style="margin-top:0.0000pt;margin-right:0.0000pt;margin-bottom:16.5000pt;margin-left:0.0000pt;vertical-align:baseline;line-height:19.5000pt;"><span style="font-family: 'Courier New'; color: #666666; letter-spacing: 0pt; font-size: 11pt;">O(1)</span></p></td></tr><tr><td width="140" valign="top" style="width:105.7000pt;padding:0.0000pt 0.0000pt 0.0000pt 0.0000pt ;border-left:31.8750pt none #ffffff;border-right:31.8750pt none #ffffff;border-top:none;;border-bottom:31.8750pt none #ffffff;"><p style="margin-top:0.0000pt;margin-right:0.0000pt;margin-bottom:16.5000pt;margin-left:0.0000pt;vertical-align:baseline;line-height:19.5000pt;"><span style="font-family: 'Courier New'; color: #666666; letter-spacing: 0pt; font-size: 11pt;">dictResize</span></p></td><td width="204" valign="top" style="width:153.5500pt;padding:0.0000pt 0.0000pt 0.0000pt 0.0000pt ;border-left:none;;;border-right:31.8750pt none #ffffff;border-top:none;;border-bottom:31.8750pt none #ffffff;"><p style="margin-top:0.0000pt;margin-right:0.0000pt;margin-bottom:16.5000pt;margin-left:0.0000pt;vertical-align:baseline;line-height:19.5000pt;"><span style="font-family: 'Courier New'; color: #666666; letter-spacing: 0pt; font-size: 11pt;">重新规划字典的大小</span></p></td><td width="44" valign="top" style="width:33.1000pt;padding:0.0000pt 0.0000pt 0.0000pt 0.0000pt ;border-left:none;;;border-right:31.8750pt none #ffffff;border-top:none;;border-bottom:31.8750pt none #ffffff;"><p style="margin-top:0.0000pt;margin-right:0.0000pt;margin-bottom:16.5000pt;margin-left:0.0000pt;vertical-align:baseline;line-height:19.5000pt;"><span style="font-family: 'Courier New'; color: #666666; letter-spacing: 0pt; font-size: 11pt;">O(1)</span></p></td></tr><tr><td width="140" valign="top" style="width:105.7000pt;padding:0.0000pt 0.0000pt 0.0000pt 0.0000pt ;border-left:31.8750pt none #ffffff;border-right:31.8750pt none #ffffff;border-top:none;;border-bottom:31.8750pt none #ffffff;"><p style="margin-top:0.0000pt;margin-right:0.0000pt;margin-bottom:16.5000pt;margin-left:0.0000pt;vertical-align:baseline;line-height:19.5000pt;"><span style="font-family: 'Courier New'; color: #666666; letter-spacing: 0pt; font-size: 11pt;">dictExpand</span></p></td><td width="204" valign="top" style="width:153.5500pt;padding:0.0000pt 0.0000pt 0.0000pt 0.0000pt ;border-left:none;;;border-right:31.8750pt none #ffffff;border-top:none;;border-bottom:31.8750pt none #ffffff;"><p style="margin-top:0.0000pt;margin-right:0.0000pt;margin-bottom:16.5000pt;margin-left:0.0000pt;vertical-align:baseline;line-height:19.5000pt;"><span style="font-family: 'Courier New'; color: #666666; letter-spacing: 0pt; font-size: 11pt;">扩展字典</span></p></td><td width="44" valign="top" style="width:33.1000pt;padding:0.0000pt 0.0000pt 0.0000pt 0.0000pt ;border-left:none;;;border-right:31.8750pt none #ffffff;border-top:none;;border-bottom:31.8750pt none #ffffff;"><p style="margin-top:0.0000pt;margin-right:0.0000pt;margin-bottom:16.5000pt;margin-left:0.0000pt;vertical-align:baseline;line-height:19.5000pt;"><span style="font-family: 'Courier New'; color: #666666; letter-spacing: 0pt; font-size: 11pt;">O(1)</span></p></td></tr><tr><td width="140" valign="top" style="width:105.7000pt;padding:0.0000pt 0.0000pt 0.0000pt 0.0000pt ;border-left:31.8750pt none #ffffff;border-right:31.8750pt none #ffffff;border-top:none;;border-bottom:31.8750pt none #ffffff;"><p style="margin-top:0.0000pt;margin-right:0.0000pt;margin-bottom:16.5000pt;margin-left:0.0000pt;vertical-align:baseline;line-height:19.5000pt;"><span style="font-family: 'Courier New'; color: #666666; letter-spacing: 0pt; font-size: 11pt;">dictRehash</span></p></td><td width="204" valign="top" style="width:153.5500pt;padding:0.0000pt 0.0000pt 0.0000pt 0.0000pt ;border-left:none;;;border-right:31.8750pt none #ffffff;border-top:none;;border-bottom:31.8750pt none #ffffff;"><p style="margin-top:0.0000pt;margin-right:0.0000pt;margin-bottom:16.5000pt;margin-left:0.0000pt;vertical-align:baseline;line-height:19.5000pt;"><span style="font-family: 'Courier New'; color: #666666; letter-spacing: 0pt; font-size: 11pt;">对字典进行N步渐进式Rehash</span></p></td><td width="44" valign="top" style="width:33.1000pt;padding:0.0000pt 0.0000pt 0.0000pt 0.0000pt ;border-left:none;;;border-right:31.8750pt none #ffffff;border-top:none;;border-bottom:31.8750pt none #ffffff;"><p style="margin-top:0.0000pt;margin-right:0.0000pt;margin-bottom:16.5000pt;margin-left:0.0000pt;vertical-align:baseline;line-height:19.5000pt;"><span style="font-family: 'Courier New'; color: #666666; letter-spacing: 0pt; font-size: 11pt;">O(N)</span></p></td></tr><tr><td width="140" valign="top" style="width:105.7000pt;padding:0.0000pt 0.0000pt 0.0000pt 0.0000pt ;border-left:31.8750pt none #ffffff;border-right:31.8750pt none #ffffff;border-top:none;;border-bottom:31.8750pt none #ffffff;"><p style="margin-top:0.0000pt;margin-right:0.0000pt;margin-bottom:16.5000pt;margin-left:0.0000pt;vertical-align:baseline;line-height:19.5000pt;"><span style="font-family: 'Courier New'; color: #666666; letter-spacing: 0pt; font-size: 11pt;">_dictRehashStep</span></p></td><td width="204" valign="top" style="width:153.5500pt;padding:0.0000pt 0.0000pt 0.0000pt 0.0000pt ;border-left:none;;;border-right:31.8750pt none #ffffff;border-top:none;;border-bottom:31.8750pt none #ffffff;"><p style="margin-top:0.0000pt;margin-right:0.0000pt;margin-bottom:16.5000pt;margin-left:0.0000pt;vertical-align:baseline;line-height:19.5000pt;"><span style="font-family: 'Courier New'; color: #666666; letter-spacing: 0pt; font-size: 11pt;">对字典进行1步尝试Rehash</span></p></td><td width="44" valign="top" style="width:33.1000pt;padding:0.0000pt 0.0000pt 0.0000pt 0.0000pt ;border-left:none;;;border-right:31.8750pt none #ffffff;border-top:none;;border-bottom:31.8750pt none #ffffff;"><p style="margin-top:0.0000pt;margin-right:0.0000pt;margin-bottom:16.5000pt;margin-left:0.0000pt;vertical-align:baseline;line-height:19.5000pt;"><span style="font-family: 'Courier New'; color: #666666; letter-spacing: 0pt; font-size: 11pt;">O(N)</span></p></td></tr><tr><td width="140" valign="top" style="width:105.7000pt;padding:0.0000pt 0.0000pt 0.0000pt 0.0000pt ;border-left:31.8750pt none #ffffff;border-right:31.8750pt none #ffffff;border-top:none;;border-bottom:31.8750pt none #ffffff;"><p style="margin-top:0.0000pt;margin-right:0.0000pt;margin-bottom:16.5000pt;margin-left:0.0000pt;vertical-align:baseline;line-height:19.5000pt;"><span style="font-family: 'Courier New'; color: #666666; letter-spacing: 0pt; font-size: 11pt;">dictAdd</span></p></td><td width="204" valign="top" style="width:153.5500pt;padding:0.0000pt 0.0000pt 0.0000pt 0.0000pt ;border-left:none;;;border-right:31.8750pt none #ffffff;border-top:none;;border-bottom:31.8750pt none #ffffff;"><p style="margin-top:0.0000pt;margin-right:0.0000pt;margin-bottom:16.5000pt;margin-left:0.0000pt;vertical-align:baseline;line-height:19.5000pt;"><span style="font-family: 'Courier New'; color: #666666; letter-spacing: 0pt; font-size: 11pt;">添加一个元素</span></p></td><td width="44" valign="top" style="width:33.1000pt;padding:0.0000pt 0.0000pt 0.0000pt 0.0000pt ;border-left:none;;;border-right:31.8750pt none #ffffff;border-top:none;;border-bottom:31.8750pt none #ffffff;"><p style="margin-top:0.0000pt;margin-right:0.0000pt;margin-bottom:16.5000pt;margin-left:0.0000pt;vertical-align:baseline;line-height:19.5000pt;"><span style="font-family: 'Courier New'; color: #666666; letter-spacing: 0pt; font-size: 11pt;">O(1)</span></p></td></tr><tr><td width="140" valign="top" style="width:105.7000pt;padding:0.0000pt 0.0000pt 0.0000pt 0.0000pt ;border-left:31.8750pt none #ffffff;border-right:31.8750pt none #ffffff;border-top:none;;border-bottom:31.8750pt none #ffffff;"><p style="margin-top:0.0000pt;margin-right:0.0000pt;margin-bottom:16.5000pt;margin-left:0.0000pt;vertical-align:baseline;line-height:19.5000pt;"><span style="font-family: 'Courier New'; color: #666666; letter-spacing: 0pt; font-size: 11pt;">dictReplace</span></p></td><td width="204" valign="top" style="width:153.5500pt;padding:0.0000pt 0.0000pt 0.0000pt 0.0000pt ;border-left:none;;;border-right:31.8750pt none #ffffff;border-top:none;;border-bottom:31.8750pt none #ffffff;"><p style="margin-top:0.0000pt;margin-right:0.0000pt;margin-bottom:16.5000pt;margin-left:0.0000pt;vertical-align:baseline;line-height:19.5000pt;"><span style="font-family: 'Courier New'; color: #666666; letter-spacing: 0pt; font-size: 11pt;">替换给定key的value值</span></p></td><td width="44" valign="top" style="width:33.1000pt;padding:0.0000pt 0.0000pt 0.0000pt 0.0000pt ;border-left:none;;;border-right:31.8750pt none #ffffff;border-top:none;;border-bottom:31.8750pt none #ffffff;"><p style="margin-top:0.0000pt;margin-right:0.0000pt;margin-bottom:16.5000pt;margin-left:0.0000pt;vertical-align:baseline;line-height:19.5000pt;"><span style="font-family: 'Courier New'; color: #666666; letter-spacing: 0pt; font-size: 11pt;">O(1)</span></p></td></tr><tr><td width="140" valign="top" style="width:105.7000pt;padding:0.0000pt 0.0000pt 0.0000pt 0.0000pt ;border-left:31.8750pt none #ffffff;border-right:31.8750pt none #ffffff;border-top:none;;border-bottom:31.8750pt none #ffffff;"><p style="margin-top:0.0000pt;margin-right:0.0000pt;margin-bottom:16.5000pt;margin-left:0.0000pt;vertical-align:baseline;line-height:19.5000pt;"><span style="font-family: 'Courier New'; color: #666666; letter-spacing: 0pt; font-size: 11pt;">dictDelete</span></p></td><td width="204" valign="top" style="width:153.5500pt;padding:0.0000pt 0.0000pt 0.0000pt 0.0000pt ;border-left:none;;;border-right:31.8750pt none #ffffff;border-top:none;;border-bottom:31.8750pt none #ffffff;"><p style="margin-top:0.0000pt;margin-right:0.0000pt;margin-bottom:16.5000pt;margin-left:0.0000pt;vertical-align:baseline;line-height:19.5000pt;"><span style="font-family: 'Courier New'; color: #666666; letter-spacing: 0pt; font-size: 11pt;">删除一个元素</span></p></td><td width="44" valign="top" style="width:33.1000pt;padding:0.0000pt 0.0000pt 0.0000pt 0.0000pt ;border-left:none;;;border-right:31.8750pt none #ffffff;border-top:none;;border-bottom:31.8750pt none #ffffff;"><p style="margin-top:0.0000pt;margin-right:0.0000pt;margin-bottom:16.5000pt;margin-left:0.0000pt;vertical-align:baseline;line-height:19.5000pt;"><span style="font-family: 'Courier New'; color: #666666; letter-spacing: 0pt; font-size: 11pt;">O(N)</span></p></td></tr><tr><td width="140" valign="top" style="width:105.7000pt;padding:0.0000pt 0.0000pt 0.0000pt 0.0000pt ;border-left:31.8750pt none #ffffff;border-right:31.8750pt none #ffffff;border-top:none;;border-bottom:31.8750pt none #ffffff;"><p style="margin-top:0.0000pt;margin-right:0.0000pt;margin-bottom:16.5000pt;margin-left:0.0000pt;vertical-align:baseline;line-height:19.5000pt;"><span style="font-family: 'Courier New'; color: #666666; letter-spacing: 0pt; font-size: 11pt;">dictRelease</span></p></td><td width="204" valign="top" style="width:153.5500pt;padding:0.0000pt 0.0000pt 0.0000pt 0.0000pt ;border-left:none;;;border-right:31.8750pt none #ffffff;border-top:none;;border-bottom:31.8750pt none #ffffff;"><p style="margin-top:0.0000pt;margin-right:0.0000pt;margin-bottom:16.5000pt;margin-left:0.0000pt;vertical-align:baseline;line-height:19.5000pt;"><span style="font-family: 'Courier New'; color: #666666; letter-spacing: 0pt; font-size: 11pt;">释放字典</span></p></td><td width="44" valign="top" style="width:33.1000pt;padding:0.0000pt 0.0000pt 0.0000pt 0.0000pt ;border-left:none;;;border-right:31.8750pt none #ffffff;border-top:none;;border-bottom:31.8750pt none #ffffff;"><p style="margin-top:0.0000pt;margin-right:0.0000pt;margin-bottom:16.5000pt;margin-left:0.0000pt;vertical-align:baseline;line-height:19.5000pt;"><span style="font-family: 'Courier New'; color: #666666; letter-spacing: 0pt; font-size: 11pt;">O(1)</span></p></td></tr><tr><td width="140" valign="top" style="width:105.7000pt;padding:0.0000pt 0.0000pt 0.0000pt 0.0000pt ;border-left:31.8750pt none #ffffff;border-right:31.8750pt none #ffffff;border-top:none;;border-bottom:31.8750pt none #ffffff;"><p style="margin-top:0.0000pt;margin-right:0.0000pt;margin-bottom:16.5000pt;margin-left:0.0000pt;vertical-align:baseline;line-height:19.5000pt;"><span style="font-family: 'Courier New'; color: #666666; letter-spacing: 0pt; font-size: 11pt;">dictFind</span></p></td><td width="204" valign="top" style="width:153.5500pt;padding:0.0000pt 0.0000pt 0.0000pt 0.0000pt ;border-left:none;;;border-right:31.8750pt none #ffffff;border-top:none;;border-bottom:31.8750pt none #ffffff;"><p style="margin-top:0.0000pt;margin-right:0.0000pt;margin-bottom:16.5000pt;margin-left:0.0000pt;vertical-align:baseline;line-height:19.5000pt;"><span style="font-family: 'Courier New'; color: #666666; letter-spacing: 0pt; font-size: 11pt;">查找一个元素</span></p></td><td width="44" valign="top" style="width:33.1000pt;padding:0.0000pt 0.0000pt 0.0000pt 0.0000pt ;border-left:none;;;border-right:31.8750pt none #ffffff;border-top:none;;border-bottom:31.8750pt none #ffffff;"><p style="margin-top:0.0000pt;margin-right:0.0000pt;margin-bottom:16.5000pt;margin-left:0.0000pt;vertical-align:baseline;line-height:19.5000pt;"><span style="font-family: 'Courier New'; color: #666666; letter-spacing: 0pt; font-size: 11pt;">O(N)</span></p></td></tr><tr><td width="140" valign="top" style="width:105.7000pt;padding:0.0000pt 0.0000pt 0.0000pt 0.0000pt ;border-left:31.8750pt none #ffffff;border-right:31.8750pt none #ffffff;border-top:none;;border-bottom:31.8750pt none #ffffff;"><p style="margin-top:0.0000pt;margin-right:0.0000pt;margin-bottom:16.5000pt;margin-left:0.0000pt;vertical-align:baseline;line-height:19.5000pt;"><span style="font-family: 'Courier New'; color: #666666; letter-spacing: 0pt; font-size: 11pt;">dictFetchValue</span></p></td><td width="204" valign="top" style="width:153.5500pt;padding:0.0000pt 0.0000pt 0.0000pt 0.0000pt ;border-left:none;;;border-right:31.8750pt none #ffffff;border-top:none;;border-bottom:31.8750pt none #ffffff;"><p style="margin-top:0.0000pt;margin-right:0.0000pt;margin-bottom:16.5000pt;margin-left:0.0000pt;vertical-align:baseline;line-height:19.5000pt;"><span style="font-family: 'Courier New'; color: #666666; letter-spacing: 0pt; font-size: 11pt;">通过key查找value</span></p></td><td width="44" valign="top" style="width:33.1000pt;padding:0.0000pt 0.0000pt 0.0000pt 0.0000pt ;border-left:none;;;border-right:31.8750pt none #ffffff;border-top:none;;border-bottom:31.8750pt none #ffffff;"><p style="margin-top:0.0000pt;margin-right:0.0000pt;margin-bottom:16.5000pt;margin-left:0.0000pt;vertical-align:baseline;line-height:19.5000pt;"><span style="font-family: 'Courier New'; color: #666666; letter-spacing: 0pt; font-size: 11pt;">O(N)</span></p></td></tr><tr><td width="140" valign="top" style="width:105.7000pt;padding:0.0000pt 0.0000pt 0.0000pt 0.0000pt ;border-left:31.8750pt none #ffffff;border-right:31.8750pt none #ffffff;border-top:none;;border-bottom:31.8750pt none #ffffff;"><p style="margin-top:0.0000pt;margin-right:0.0000pt;margin-bottom:16.5000pt;margin-left:0.0000pt;vertical-align:baseline;line-height:19.5000pt;"><span style="font-family: 'Courier New'; color: #666666; letter-spacing: 0pt; font-size: 11pt;">dictGetRandomKey</span></p></td><td width="204" valign="top" style="width:153.5500pt;padding:0.0000pt 0.0000pt 0.0000pt 0.0000pt ;border-left:none;;;border-right:31.8750pt none #ffffff;border-top:none;;border-bottom:31.8750pt none #ffffff;"><p style="margin-top:0.0000pt;margin-right:0.0000pt;margin-bottom:16.5000pt;margin-left:0.0000pt;vertical-align:baseline;line-height:19.5000pt;"><span style="font-family: 'Courier New'; color: #666666; letter-spacing: 0pt; font-size: 11pt;">随机返回字典中一个元素</span></p></td><td width="44" valign="top" style="width:33.1000pt;padding:0.0000pt 0.0000pt 0.0000pt 0.0000pt ;border-left:none;;;border-right:31.8750pt none #ffffff;border-top:none;;border-bottom:31.8750pt none #ffffff;"><p style="margin-top:0.0000pt;margin-right:0.0000pt;margin-bottom:16.5000pt;margin-left:0.0000pt;vertical-align:baseline;line-height:19.5000pt;"><span style="font-family: 'Courier New'; color: #666666; letter-spacing: 0pt; font-size: 11pt;">O(1)</span></p></td></tr></tbody></table><p>&nbsp;</p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">字典结构</span></p><p><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">typedef</span>&nbsp;<span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">struct</span>&nbsp;<span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">dict</span><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;{</span></p><p><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#008000;font-size:9.5000pt;">// 类型特定函数</span></p><p><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">dictType</span><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;*</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#000080;font-size:9.5000pt;">type</span><span style="font-family: 新宋体; font-size: 9.5pt;">;</span></p><p><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#008000;font-size:9.5000pt;">// 私有数据</span></p><p><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">void</span><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;*</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#000080;font-size:9.5000pt;">privdata</span><span style="font-family: 新宋体; font-size: 9.5pt;">;</span></p><p><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#008000;font-size:9.5000pt;">// 哈希表</span></p><p><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">dictht</span>&nbsp;<span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#000080;font-size:9.5000pt;">ht</span><span style="font-family: 新宋体; font-size: 9.5pt;">[2];</span></p><p><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#008000;font-size:9.5000pt;">// rehash 索引</span></p><p><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#008000;font-size:9.5000pt;">// 当 rehash 不在进行时，值为 -1</span></p><p><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">int</span>&nbsp;<span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#000080;font-size:9.5000pt;">rehashidx</span><span style="font-family: 新宋体; font-size: 9.5pt;">; </span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#008000;font-size:9.5000pt;">/* rehashing not in progress if rehashidx == -1 */</span></p><p><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#008000;font-size:9.5000pt;">// 目前正在运行的安全迭代器的数量</span></p><p><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">int</span>&nbsp;<span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#000080;font-size:9.5000pt;">iterators</span><span style="font-family: 新宋体; font-size: 9.5pt;">; </span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#008000;font-size:9.5000pt;">/* number of iterators currently running */</span></p><p><span style="font-family: 新宋体; font-size: 9.5pt;">} </span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">dict</span><span style="font-family: 新宋体; font-size: 9.5pt;">;</span></p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">这里哈希表有两个，一般都用ht[0]，当需要rehash的时候会创建一个比ht[0]大的 2 的 N 次方的ht[1]，然后渐进式的将数据dictEntry移过去(除了定时的rehash，在每次操作哈希表时都会_dictRehashStep)，完成后将ht[1]替换ht[0]</span></p><p>&nbsp;</p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">zset</span></p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">zset本质就是list,只不过每个元素都有若干个指向后继span</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">长的指针，这样简单的设计大大提高了效率，使得可以比拟平衡二叉树，查找、删除、插入等操作都可以在对数期望时间内完成，对比平衡树，跳跃表的实现要简单直观很多。</span></p><p>&nbsp;</p><p>&nbsp;</p><p><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#008000;font-size:9.5000pt;">/* ZSETs use a specialized version of Skiplists */</span></p><p><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#008000;font-size:9.5000pt;">/*</span></p><p><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#008000;font-size:9.5000pt;">&nbsp;* 跳跃表节点</span></p><p><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#008000;font-size:9.5000pt;">&nbsp;*/</span></p><p><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">typedef</span>&nbsp;<span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">struct</span>&nbsp;<span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">zskiplistNode</span><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;{</span></p><p><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#008000;font-size:9.5000pt;">// 成员对象</span></p><p><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">robj</span><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;*</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#000080;font-size:9.5000pt;">obj</span><span style="font-family: 新宋体; font-size: 9.5pt;">;</span></p><p><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#008000;font-size:9.5000pt;">// 分值</span></p><p><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">double</span>&nbsp;<span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#000080;font-size:9.5000pt;">score</span><span style="font-family: 新宋体; font-size: 9.5pt;">;</span></p><p><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#008000;font-size:9.5000pt;">// 后退指针</span></p><p><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">struct</span>&nbsp;<span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">zskiplistNode</span><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;*</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#000080;font-size:9.5000pt;">backward</span><span style="font-family: 新宋体; font-size: 9.5pt;">;</span></p><p><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#008000;font-size:9.5000pt;">// 层</span></p><p><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">struct</span>&nbsp;<span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">zskiplistLevel</span><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;{</span></p><p><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#008000;font-size:9.5000pt;">// 前进指针</span></p><p><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">struct</span>&nbsp;<span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">zskiplistNode</span><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;*</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#000080;font-size:9.5000pt;">forward</span><span style="font-family: 新宋体; font-size: 9.5pt;">;</span></p><p><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#008000;font-size:9.5000pt;">// 跨度</span></p><p><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">unsigned</span>&nbsp;<span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">int</span>&nbsp;<span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#000080;font-size:9.5000pt;">span</span><span style="font-family: 新宋体; font-size: 9.5pt;">;</span></p><p><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;} </span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#000080;font-size:9.5000pt;">level</span><span style="font-family: 新宋体; font-size: 9.5pt;">[];</span></p><p><span style="font-family: 新宋体; font-size: 9.5pt;">} </span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">zskiplistNode</span><span style="font-family: 新宋体; font-size: 9.5pt;">;</span></p><p>&nbsp;</p><p><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#008000;font-size:9.5000pt;">/*</span></p><p><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#008000;font-size:9.5000pt;">&nbsp;* 跳跃表</span></p><p><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#008000;font-size:9.5000pt;">&nbsp;*/</span></p><p><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">typedef</span>&nbsp;<span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">struct</span>&nbsp;<span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">zskiplist</span><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;{</span></p><p><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#008000;font-size:9.5000pt;">// 表头节点和表尾节点</span></p><p><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">struct</span>&nbsp;<span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">zskiplistNode</span><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;*</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#000080;font-size:9.5000pt;">header</span><span style="font-family: 新宋体; font-size: 9.5pt;">, *</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#000080;font-size:9.5000pt;">tail</span><span style="font-family: 新宋体; font-size: 9.5pt;">;</span></p><p><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#008000;font-size:9.5000pt;">// 表中节点的数量</span></p><p><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">unsigned</span>&nbsp;<span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">long</span>&nbsp;<span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#000080;font-size:9.5000pt;">length</span><span style="font-family: 新宋体; font-size: 9.5pt;">;</span></p><p><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#008000;font-size:9.5000pt;">// 表中层数最大的节点的层数</span></p><p><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">int</span>&nbsp;<span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#000080;font-size:9.5000pt;">level</span><span style="font-family: 新宋体; font-size: 9.5pt;">;</span></p><p><span style="font-family: 新宋体; font-size: 9.5pt;">} </span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">zskiplist</span><span style="font-family: 新宋体; font-size: 9.5pt;">;</span></p><p>&nbsp;</p><p><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#008000;font-size:9.5000pt;">/*</span></p><p><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#008000;font-size:9.5000pt;">&nbsp;* 有序集合</span></p><p><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#008000;font-size:9.5000pt;">&nbsp;*/</span></p><p><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">typedef</span>&nbsp;<span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">struct</span>&nbsp;<span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">zset</span><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;{</span></p><p>&nbsp;</p><p><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#008000;font-size:9.5000pt;">// 字典，键为成员，值为分值</span></p><p><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#008000;font-size:9.5000pt;">// 用于支持 O(1) 复杂度的按成员取分值操作</span></p><p><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#000080;font-size:9.5000pt;">dict</span><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;*</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#000080;font-size:9.5000pt;">dict</span><span style="font-family: 新宋体; font-size: 9.5pt;">;</span></p><p><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#008000;font-size:9.5000pt;">// 跳跃表，按分值排序成员</span></p><p><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#008000;font-size:9.5000pt;">// 用于支持平均复杂度为 O(log N) 的按分值定位成员操作</span></p><p><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#008000;font-size:9.5000pt;">// 以及范围操作</span></p><p><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">zskiplist</span><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;*</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#000080;font-size:9.5000pt;">zsl</span><span style="font-family: 新宋体; font-size: 9.5pt;">;</span></p><p>&nbsp;</p><p><span style="font-family: 新宋体; font-size: 9.5pt;">} </span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">zset</span><span style="font-family: 新宋体; font-size: 9.5pt;">;</span></p><p>&nbsp;</p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">虽然这种方式排序查找很快，但是修改的话就得多做些工作了</span></p><p>&nbsp;</p><p><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#008000;font-size:9.5000pt;">/* Delete an element with matching score/object from the skiplist. </span></p><p><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#008000;font-size:9.5000pt;">&nbsp;*</span></p><p><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#008000;font-size:9.5000pt;">&nbsp;* 从跳跃表 zsl 中删除包含给定节点 score 并且带有指定对象 obj 的节点。</span></p><p><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#008000;font-size:9.5000pt;">&nbsp;*</span></p><p><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#008000;font-size:9.5000pt;">&nbsp;* T_wrost = O(N^2), T_avg = O(N log N)</span></p><p><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#008000;font-size:9.5000pt;">&nbsp;*/</span></p><p><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">int</span>&nbsp;<span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#880000;font-size:9.5000pt;">zslDelete</span><span style="font-family: 新宋体; font-size: 9.5pt;">(</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">zskiplist</span><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;*</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#000080;font-size:9.5000pt;">zsl</span><span style="font-family: 新宋体; font-size: 9.5pt;">, </span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">double</span>&nbsp;<span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#000080;font-size:9.5000pt;">score</span><span style="font-family: 新宋体; font-size: 9.5pt;">, </span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#0000ff;font-size:9.5000pt;">robj</span><span style="font-family: 新宋体; font-size: 9.5pt;">&nbsp;*</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#000080;font-size:9.5000pt;">obj</span><span style="font-family: 新宋体; font-size: 9.5pt;">) </span></p><p>&nbsp;</p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">intset</span></p><p style="margin: 0pt 0pt 0pt 4.6pt; border-left-width: 2.25pt; border-left-style: solid; border-left-color: #6ce26c; padding: 0pt 0pt 0pt 7pt; vertical-align: baseline; line-height: 13.5pt; background: #ffffff;"><strong><span style="font-family: 微软雅黑; color: #006699; letter-spacing: 0pt; font-size: 9pt; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">typedef</span></strong>&nbsp;<strong><span style="font-family: 微软雅黑; color: #006699; letter-spacing: 0pt; font-size: 9pt; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">struct</span></strong><span style="font-family: 微软雅黑; letter-spacing: 0pt; font-size: 9pt; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">&nbsp;intset&nbsp;{&nbsp;&nbsp;</span></p><p style="margin: 0pt 0pt 0pt 4.6pt; text-indent: 20.95pt; border-left-width: 2.25pt; border-left-style: solid; border-left-color: #6ce26c; padding: 0pt 0pt 0pt 7pt; vertical-align: baseline; line-height: 13.5pt; background: #f8f8f8;"><span style="font-family: 微软雅黑; letter-spacing: 0pt; font-size: 9pt; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">uint32_t&nbsp;encoding;&nbsp;</span><span style="font-family: 微软雅黑; color: #008200; letter-spacing: 0pt; font-size: 9pt; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">//所使用类型的长度，4\8\16</span><span style="font-family: 微软雅黑; letter-spacing: 0pt; font-size: 9pt; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">&nbsp;&nbsp;</span></p><p style="margin: 0pt 0pt 0pt 4.6pt; text-indent: 20.95pt; border-left-width: 2.25pt; border-left-style: solid; border-left-color: #6ce26c; padding: 0pt 0pt 0pt 7pt; vertical-align: baseline; line-height: 13.5pt; background: #ffffff;"><span style="font-family: 微软雅黑; letter-spacing: 0pt; font-size: 9pt; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">uint32_t&nbsp;length;&nbsp;</span><span style="font-family: 微软雅黑; color: #008200; letter-spacing: 0pt; font-size: 9pt; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">//元素个数</span><span style="font-family: 微软雅黑; letter-spacing: 0pt; font-size: 9pt; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">&nbsp;&nbsp;</span></p><p style="margin: 0pt 0pt 0pt 4.6pt; text-indent: 20.95pt; border-left-width: 2.25pt; border-left-style: solid; border-left-color: #6ce26c; padding: 0pt 0pt 0pt 7pt; vertical-align: baseline; line-height: 13.5pt; background: #f8f8f8;"><span style="font-family: 微软雅黑; letter-spacing: 0pt; font-size: 9pt; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">int8_t&nbsp;contents[];&nbsp;</span><span style="font-family: 微软雅黑; color: #008200; letter-spacing: 0pt; font-size: 9pt; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">//保存元素的数组</span><span style="font-family: 微软雅黑; letter-spacing: 0pt; font-size: 9pt; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">&nbsp;&nbsp;</span></p><p style="margin-top: 0pt; margin-right: 0pt; margin-bottom: 0pt; border-left-width: 2.25pt; border-left-style: solid; border-left-color: #6ce26c; padding: 0pt 0pt 0pt 7pt; vertical-align: baseline; line-height: 13.5pt; background: #ffffff;"><span style="font-family: 微软雅黑; letter-spacing: 0pt; font-size: 9pt; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">}&nbsp;intset;&nbsp;&nbsp;</span></p><p>&nbsp;</p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">intset其实就是数组，有序、无重复地保存多个整数值，查找用的是二分查找 * T = O(log N)，添加的话在找到对应的数组中应该存在的位子后使用memmove向后移出空位填补(当然需要先realloc预分配空间)，同理删除也是用memmove向前移动</span></p><p>&nbsp;</p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">set</span></p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">当使用整数时，使用intset，否则使用哈希表</span></p><p>&nbsp;</p><p>&nbsp;</p><p><span style="font-family: 宋体; font-size: 10.5pt;">其他的关于网络事件处理，epoll，回调，拆包都和正常使用差不多，关于错误处理EINTR(系统调用期间发生中断)和EAGAIN 继续重试而如果是EPOLLHUP或EPOLLERR则让io该读读该写写，有错处理就是了。</span></p><p>&nbsp;</p><p>&nbsp;</p><img src ="http://www.cppblog.com/xiaoxiaoling/aggbug/214634.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/xiaoxiaoling/" target="_blank">clcl</a> 2017-01-24 18:13 <a href="http://www.cppblog.com/xiaoxiaoling/archive/2017/01/24/214634.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>dnspod解析</title><link>http://www.cppblog.com/xiaoxiaoling/archive/2017/01/23/214630.html</link><dc:creator>clcl</dc:creator><author>clcl</author><pubDate>Mon, 23 Jan 2017 07:14:00 GMT</pubDate><guid>http://www.cppblog.com/xiaoxiaoling/archive/2017/01/23/214630.html</guid><wfw:comment>http://www.cppblog.com/xiaoxiaoling/comments/214630.html</wfw:comment><comments>http://www.cppblog.com/xiaoxiaoling/archive/2017/01/23/214630.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/xiaoxiaoling/comments/commentRss/214630.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/xiaoxiaoling/services/trackbacks/214630.html</trackback:ping><description><![CDATA[<p>&nbsp;</p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;"></span></p><p>&nbsp;</p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">dns的递归解析过程还是挺繁琐的，要知道一个域名可能有cname、ns 而请求的cname、ns可能还有cname、ns，如果按照线性的处理每个请求那逻辑就变成毛线团了</span></p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">dnspod的处理还是挺巧妙的，通过一个公共的数据集dataset将所有域名对应的a、cname、ns等类型的数据作为单独的条目存入，当有需要某个域名的信息时先去dataset找，找不到在加入qlist请求根，有专门的线程不间断的将qlist轮询dataset找（这里只要次数允许，没得到想要的结果就轮询所有qlist到dataset找虽然可以简化逻辑分离的彻底但是会是个性能瓶颈，后面有方案）当根返回以后只是简单的将记录(通常是一个域名的cname、ns或者a)存入dataset(而不是继续流程，因为根据这个返回是cname还是ns或者a处理不同逻辑复杂，而这样处理对于用到相同域名的请求还有优化作用)，剩下的工作交给那边不间断轮询的线程</span></p><p>&nbsp;</p><p><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">Dnspod主要由3个run（若干个线程）组成</span></p><p>&nbsp;</p><p><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#880000;font-size:9.5000pt;">run_sentinel</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">&nbsp;&nbsp;监听53端口接收客户端请求，将请求放到队列中</span></p><p><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#880000;font-size:9.5000pt;">run_fetcher</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">&nbsp;&nbsp;&nbsp;从队列中取出请求，根据qname取得最后一级cname，查看本地dataset 是否有记录，如果有则返回，没有则将该请求放入qlist中</span></p><p>&nbsp;</p><p><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#880000;font-size:9.5000pt;">run_quizzer</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#880000;font-size:9.5000pt;">&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p><span style="font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#880000;font-size:9.5000pt;">1.</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#880000;font-size:9.5000pt;">不间断的遍历qlist，只要状态为</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#a000a0;font-size:9.5000pt;">PROCESS_QUERY</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#a000a0;font-size:9.5000pt;">且</span><span style="mso-spacerun:'yes';font-family:新宋体;mso-bidi-font-family:'Times New Roman';color:#a000a0;font-size:9.5000pt;">dataset中没有的就向对应的根发送请求。</span></p><p><span style="font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">2.</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">通过epoll等待根返回，解析返回的数据加入 dataset</span></p><p><span style="font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">3.</span><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">检查记录的ttl，在将记录加入dataset时还会将这些记录以红黑树的形式组织起来，取得ttl最早到期的，将其放入qlist中等待刷新，注意这里不是删除，如果收不到不返回则该记录一直存在</span></p><p align="justify" style="text-align:justify;text-justify:inter-ideograph;">&nbsp;</p><p align="justify" style="text-align:justify;text-justify:inter-ideograph;"><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">关于dataset的实现</span></p><p align="justify" style="text-align:justify;text-justify:inter-ideograph;"><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">dataset是使用哈希表实现的，本质上是个二维数组，将域名哈希成一个值，模上数组的数量作为下标，找到对应的数组接着遍历查找，根据需要可以扩大数组的数量提升性能。</span></p><p align="justify" style="text-align:justify;text-justify:inter-ideograph;">&nbsp;</p><p align="justify" style="text-align:justify;text-justify:inter-ideograph;"><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">我们的优化手段</span></p><p align="justify" style="text-align:justify;text-justify:inter-ideograph;"><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">之前提到dnspod的qlist会不间断轮询，属于主动查询，对性能有不小的影响，这里我们采取的做法是被动(类似回调的方式)，我们将请求的域名和类型分类，相同的放在一组，当dataset找不到向根发出请求后我们并不每次主动轮询，而是在等到应答后，触发该域名和类型的请求组，让他们根据自己的逻辑走下一步（一般是先找该域名的最后一级cname，根据这个cname查是否存在他的对应请求类型的记录，一般是a或者ns，如果没有，则找这个cname的ns）</span></p><p align="justify" style="text-align:justify;text-justify:inter-ideograph;">&nbsp;</p><p align="justify" style="text-align:justify;text-justify:inter-ideograph;"><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;">以上可以看出dataset很重要，负载也不小，还经常需要并发访问，这里我们每次接收到根的回复后，除了将记录的答案加进dataset，还创建一个临时的dataset，只存该次回复的信息，在后面的流程会优先到这里去找，没有的再找dataset。</span></p><p align="justify" style="text-align:justify;text-justify:inter-ideograph;"><span style="mso-spacerun:'yes';font-family:宋体;mso-bidi-font-family:'Times New Roman';font-size:10.5000pt;"></span></p><img src ="http://www.cppblog.com/xiaoxiaoling/aggbug/214630.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/xiaoxiaoling/" target="_blank">clcl</a> 2017-01-23 15:14 <a href="http://www.cppblog.com/xiaoxiaoling/archive/2017/01/23/214630.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Network Stack Specialization for Performance</title><link>http://www.cppblog.com/xiaoxiaoling/archive/2017/01/22/214625.html</link><dc:creator>clcl</dc:creator><author>clcl</author><pubDate>Sun, 22 Jan 2017 10:01:00 GMT</pubDate><guid>http://www.cppblog.com/xiaoxiaoling/archive/2017/01/22/214625.html</guid><wfw:comment>http://www.cppblog.com/xiaoxiaoling/comments/214625.html</wfw:comment><comments>http://www.cppblog.com/xiaoxiaoling/archive/2017/01/22/214625.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/xiaoxiaoling/comments/commentRss/214625.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/xiaoxiaoling/services/trackbacks/214625.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 最近在研究DPDK，这是sigcomm 2014的论文，纪录在此备忘Ps: &nbsp;文中关键词的概念：segment : 对应于tcp的PDU(协议传输单元)，这里应该指tcp层的包，如果一个包太大tcp负责将它拆分成多个segment（这个概念对理解后文有帮助）根据unix网络编程卷1 第8页注解2：packet是IP层传递给链路层并且由链路层打包好封装在帧中的数据（不包括帧头）而IP层的包...&nbsp;&nbsp;<a href='http://www.cppblog.com/xiaoxiaoling/archive/2017/01/22/214625.html'>阅读全文</a><img src ="http://www.cppblog.com/xiaoxiaoling/aggbug/214625.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/xiaoxiaoling/" target="_blank">clcl</a> 2017-01-22 18:01 <a href="http://www.cppblog.com/xiaoxiaoling/archive/2017/01/22/214625.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>cegui 0.7 渲染改动</title><link>http://www.cppblog.com/xiaoxiaoling/archive/2010/04/27/113741.html</link><dc:creator>clcl</dc:creator><author>clcl</author><pubDate>Tue, 27 Apr 2010 12:56:00 GMT</pubDate><guid>http://www.cppblog.com/xiaoxiaoling/archive/2010/04/27/113741.html</guid><wfw:comment>http://www.cppblog.com/xiaoxiaoling/comments/113741.html</wfw:comment><comments>http://www.cppblog.com/xiaoxiaoling/archive/2010/04/27/113741.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/xiaoxiaoling/comments/commentRss/113741.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/xiaoxiaoling/services/trackbacks/113741.html</trackback:ping><description><![CDATA[<p>一直想用cegui，但是没机会用，只是抽空看其代码</p>
<p>最近听朋友说0.7 debug下帧数提高100多，挺惊讶的，重新到久违了的官网上下了0.7.1</p>
<p>看了下渲染的实现(GL)</p>
<p>首先，添加了GeometryBuffer玩意，使得每个window保存了属于自己的顶点和纹理信息</p>
<p>然后在RenderingSurface中有GeometryBuffer队列，使得每个拥有AutoRenderingSurface属性的window有属于自己的队列(默认只有FrameWindow才有)</p>
<p>而在drawself中执行的则是先通过looknfeel，把需要渲染的信息丢到每个部件自己的GeometryBuffer里，然后把GeometryBuffer丢到RenderingSurface的队列中(一般为</p>
<p>FrameWindow的GeometryBuffer队列，每个面板就有自己的渲染队列了)</p>
<p>要知道以往都是只有一个队列的，要渲染啥直接往里塞。 。 。<br>这样一改就不必每个小部件有更改都要全部重新清空渲染了</p>
<p><br>再往后就是把每个窗口队列里的GeometryBuffer渲染到各自的RenderingSurface表面上，这里要注意的是并不是渲染到屏幕上而是表面上，cegui在这里使用了渲染到纹理，GL</p>
<p>用的是fbo实现的。</p>
<p>注意RenderingSurface只有两个来源，一是通过设置AutoRenderingSurface属性，另一个就是RenderingRoot了，RenderingRoot只有一个，在render中，通过第一个来源的使</p>
<p>用的是fbo的渲染，而第二个来源则直接渲染到屏幕了。</p>
<p>所有的这些执行完后就可以渲染到屏幕了，通过RenderingRoot执行，注意这里的RenderingRoot中的RenderTarget和之前的不一样，这里用的是OpenGLViewportTarget而不是</p>
<p>OpenGLFBOTextureTarget。<br></p>
<img src ="http://www.cppblog.com/xiaoxiaoling/aggbug/113741.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/xiaoxiaoling/" target="_blank">clcl</a> 2010-04-27 20:56 <a href="http://www.cppblog.com/xiaoxiaoling/archive/2010/04/27/113741.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[转]理解批次</title><link>http://www.cppblog.com/xiaoxiaoling/archive/2009/10/25/99425.html</link><dc:creator>clcl</dc:creator><author>clcl</author><pubDate>Sun, 25 Oct 2009 09:15:00 GMT</pubDate><guid>http://www.cppblog.com/xiaoxiaoling/archive/2009/10/25/99425.html</guid><wfw:comment>http://www.cppblog.com/xiaoxiaoling/comments/99425.html</wfw:comment><comments>http://www.cppblog.com/xiaoxiaoling/archive/2009/10/25/99425.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/xiaoxiaoling/comments/commentRss/99425.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/xiaoxiaoling/services/trackbacks/99425.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 这个看代码里面batch相关的。[Direct3D] 实现批次渲染、硬件 T&amp;L 的渲染器和 D3DPipeline在是否从 D3DRender 提供顶点缓存区操作给流水线时做了一些权衡，最后决定暂时使用 IDirect3DDevice9::DrawPrimitiveUP 来渲染，因为它更容易书写，而且开销是一次顶点拷贝，流水线也不用操心对缓存的使用。(DrawPrimitive的...&nbsp;&nbsp;<a href='http://www.cppblog.com/xiaoxiaoling/archive/2009/10/25/99425.html'>阅读全文</a><img src ="http://www.cppblog.com/xiaoxiaoling/aggbug/99425.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/xiaoxiaoling/" target="_blank">clcl</a> 2009-10-25 17:15 <a href="http://www.cppblog.com/xiaoxiaoling/archive/2009/10/25/99425.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>