Setting up hashes

设置hashes

To quickly process static sets of data such as server names, map directive’s values, MIME types, names of request header strings, nginx uses hash tables. During the start and each re-configuration nginx selects the minimum possible sizes of hash tables such that the bucket size that stores keys with identical hash values does not exceed the configured parameter (hash bucket size). The size of a table is expressed in buckets. The adjustment is continued until the table size exceeds the hash max size parameter. Most hashes have the corresponding directives that allow changing these parameters, for example, for the server names hash they are server_names_hash_max_size and server_names_hash_bucket_size.

nginx使用哈希表,用于快速处理服务器名称、map指令值、MIME类型、请求头字符串名称等静态数据集。在启动和每次重新配置时,nginx都会选择最小可能的哈希表大小,以确保存储具有相同哈希键值的桶大小不超过配置(哈希桶大小)。表的大小用桶表示。持续调整到表的大小超过哈希最大参数值。大多数哈希值都有相应的指令,允许更改这些参数,例如服务器名称哈希值的指令是server_names_hash_max_sizeserver_names_hash_bucket_size

The hash bucket size parameter is aligned to the size that is a multiple of the processor’s cache line size. This speeds up key search in a hash on modern processors by reducing the number of memory accesses. If hash bucket size is equal to one processor’s cache line size then the number of memory accesses during the key search will be two in the worst case — first to compute the bucket address, and second during the key search inside the bucket. Therefore, if nginx emits the message requesting to increase either hash max size or hash bucket size then the first parameter should first be increased.

hash桶大小参数与处理器缓存行大小的成倍数关系。这样可以减少内存访问次数,从而加快hash键的搜索速度。如果散列桶的大小等于一个处理器的缓存行大小,那么在最糟糕的情况下,搜索键的过程中需要访问两次内存 - 第一次是计算桶地址,第二次是在桶内进行键搜索。因此,如果nginx发出请求增加哈希最大值或哈希桶大小的信息,则应首先增加第一个参数(hash max size)。