现在的位置: 首页 > 综合 > 正文

1.2.2. Caching and Hash Tables 高速缓存和哈希表

2017年08月16日 ⁄ 综合 ⁄ 共 1792字 ⁄ 字号 评论关闭

1.2.2. Caching and Hash Tables
It is pretty common to use a cache to increase performance. In the networking code, there are caches for L3-to-L2 mappings (such as the ARP cache used by IPv4), for the routing table cache, etc.
我们经常会用缓存来提高处理速率。在网络编程中,L3-to-L2 对应,路由表等都用到了缓存。

Cache lookup routines often take an input parameter that says whether a cache miss should or should not create a new element and add it to the cache. Other lookup routines simply add missing elements all the time.
高速缓存查找例程经常使用一个输入参数来指示,当高速缓存中没有所要查找的元素时,是创建该元素并添加到高速缓存还是什么都不做。其他的一些查找例程总是简单的添加不存在的元素。

Caches are often implemented with hash tables . The kernel provides a set of data types, such as one-way and bidirectional lists, that can be used as building blocks for simple hash tables.
高速缓存一般用HASH表来管理,内核提供了多种数据结构来创建哈希表,例如单链表和双链表。

The standard way to handle inputs that hash to the same value is to put them in a list. Traversing this list takes substantially longer than using the hash key to do a lookup. Therefore, it is always important to minimize the number of inputs that hash to the same value.
处理hash到同一个元素的标准方法是将他们放进一个链表。遍历一个链表比使用hash关键字的查找要费时的多,所以减少hash到同一元素的输入数是非常重要的。

When the lookup time on a hash table (whether it uses a cache or not) is a critical parameter for the owner subsystem, it may implement a mechanism to increase the size of the hash table so that the average length of the collision lists goes down and the average lookup time improves. See the section "Dynamic resizing of per-netmask hash tables" in Chapter 34 for an example.
当hash表的查询时间在所属子系统中是一个严格限制时,应该采取某种机制增加hash表的长度,这样冲突链表的平均长度将会减小而平均查询时间将得到改善。参阅第三十四章中“per-netmask hash表动态变化尺寸”一节。

You may also find subsystems, such as the neighboring layer, that add a random component (regularly changed) to the key used to distribute elements in the cache's buckets. This is used to reduce the damage of Denial of Service (DoS) attacks aimed at concentrating the elements of a hash table into a single bucket. See the section "Caching" in Chapter 27 for an example.
在子系统中,如临层,经常会在分配元素的关键字中增加一个随机数。这是用来减少那些试图将大量元素hash入相同VAlue值的Dos(拒绝服务攻击)攻击的危害。

抱歉!评论已关闭.