Skip to content

Optimize Windows UDP recv(WSARecvFrom), and reduces EventPollers wake-ups (epoll_wait/wepoll)#285

Closed
PioLing wants to merge 19 commits intoZLMediaKit:masterfrom
PioLing:master
Closed

Optimize Windows UDP recv(WSARecvFrom), and reduces EventPollers wake-ups (epoll_wait/wepoll)#285
PioLing wants to merge 19 commits intoZLMediaKit:masterfrom
PioLing:master

Conversation

@PioLing
Copy link
Contributor

@PioLing PioLing commented Dec 19, 2025

No description provided.

return ret;
}

ret += nread;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ret这个值去掉了?建议不要修改api返回值逻辑

#endif
return std::make_shared<SocketRecvFromBuffer>(kPacketCount * kBufferCapacity);

return std::make_shared<SocketRecvFromBuffer>(kPacketCount * kBufferCapacity, is_udp ? 32 : 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

udp情况下,每个udp buffer 128KB, 总共32个,会造成内存浪费,应该参考SocketRecvmmsgBuffer方案,每个buffer 4K,32个共128KB

}

size_t checkIndex(size_t index) const {
return index < _batch_size ? index : 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个不用判断吧? 这个逻辑都是你自己写的,又不是外部传参进来的,这个逻辑我觉得不应该存在

std::static_pointer_cast<BufferRaw>(_buffer)->setSize(nread);
for (size_t i = 0; i < _batch_size; ++i) {
if (!_buffers[i]) {
allocBuffer(i);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个allocBuffer的操作应该放在recvfrom那个循环里面,没必要单独开个循环逻辑

nread = ::recvfrom(fd, _buffers[i]->data(), _buffers[i]->getCapacity() - 1, 0, (struct sockaddr *)&_addresses[i], &len);
if (nread > 0) {
_buffers[i]->data()[nread] = '\0';
std::static_pointer_cast<BufferRaw>(_buffers[i])->setSize(nread);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议_buffer直接声明为BufferRaw::Ptr 这样不需要static_pointer_cast

ssize_t recvFromSocket(int fd, ssize_t &count) override {
ssize_t totalread = 0;
int nread = 0, retry_count = 0;
static const int MAX_RETRIES = 5;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么需要重试5次? 好像没必要吧?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UV_EINTR ,这个水平触发的?

@PioLing
Copy link
Contributor Author

PioLing commented Jan 4, 2026

按上面的建议修改好了

@PioLing PioLing closed this Jan 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants