处理 poll 数组最有效的方法是什么?
实现定义。在您知道必须优化之前不要优化;这称为过早优化,这是浪费时间。
我是否应该定义一个大小为 10 的小数组以及是否有更多客户端
为大小为 20 或 50 的数组重新分配内存?
如果您的分析器确定您的pollfds 是您程序中的一个重大瓶颈,并且您的老板(或教授)说您的程序“不够快”,那就去吧。如果您的列表将获得的最大列表是 50,那么只需使用静态数组,将 fd 成员设置为 -1,当您从数组中删除时,不要费心转移东西......你指的是瓶颈对于这么小的数字来说,这将是微不足道的。
如果您在最坏的情况下尝试处理大量客户端,您可能会担心在处理较小数字时数组尾随未使用的空间......因此会选择可调整大小的 pollfd 数组。
或者我应该:(a) 释放 struct pollfd 类型的数组,并且 (b)
重新分配它的大小等于列表的大小(+1
监听套接字)=> 每次客户端关闭连接时(和
因此我必须从数组中删除一个元素(可能设置
socket 到 -1,因此 poll 忽略它)导致未使用的空间
数组)
我能想到的最简单的算法在调整大小时将空间加倍。与线性调整大小相比,加倍调整大小的最大好处可能是减少了对realloc 的调用;与其接受 1024 连接并调用 realloc 1024 次,我更愿意接受 1024 连接并调用 realloc 10 或 11 次。这对我来说似乎也更简单,因为不需要将数组容量单独存储到已用计数;您可以使用二进制数的属性来发挥自己的优势:(n - 1) & n == 0 n 是 2 的幂。
#define is_power_of_two(n) !(((n) - 1) & (n))
struct pollfd *pollfd_array_resize(struct pollfd *array, size_t size) {
const size_t max_size = SIZE_MAX / sizeof *array;
if (size == max_size) { return NULL; }
if (is_power_of_two(size)) {
array = realloc(array, (size > 0 ? size * 2 : 1) * sizeof *array);
}
return array;
}
编辑:我想我已经找到了一个使用 realloc 的解决方案,并在每次客户端断开连接以覆盖 fds 处的套接字时使用 memmove 进行转移
数组。
这似乎是一个相当不错的解决方案。它增加了缓存局部性,但代价是每次客户端断开连接时都必须调用 memmove 和 realloc。
在我的分析器指出poll 占用了太多处理器时间之前,我什至不会考虑对数组进行“碎片整理”。发生这种情况时,我会考虑将fd 成员设置为负值(就像您在编辑之前正在所做的那样)并将要删除的项目的索引放入recently_freed 堆栈中。插入时,如果可能,我会从该堆栈中选择项目。如果您必须进行碎片整理,我建议您根据堆栈的大小进行。
size_t pollfd_array_defrag(struct pollfd *array, size_t size) {
size_t new_size = 0;
for (size_t x = 0; x + 1 < size; x++) {
if (array[x].fd < 0) {
continue;
}
array[new_size++] = array[x];
}
return new_size;
}
int main(void) {
size_t size = 0, index;
struct pollfd *array = NULL;
struct index_stack *recently_freed = NULL;
/*----------------------------*
* ... snip for insertion ... */
if (recently_freed && recently_freed->size > 0) {
index = index_stack_pop(&recently_freed);
}
else {
struct pollfd *temp = pollfd_array_resize(array, size);
if (temp == NULL) {
/* TODO: Handle memory allocation errors */
}
array = temp;
index = size++;
}
array[index] = (struct pollfd) { .fd = your_fd,
.events = your_events,
.revents = your_revents };
/*--------------------------*
* ... snip for removal ... */
index_stack_push(&recently_freed, index);
array[index] = -1;
/*----------------------------------*
* ... snip for defragmentation ... */
if (is_power_of_two(recently_freed->size) && recently_freed->size * 2 + 1 > size) {
size = pollfd_array_defrag(array);
/* array will shrink in future insertions */
index_stack_destroy(&recently_freed);
recently_freed = NULL;
}
}