Commit ca75a19c authored by jan.koester's avatar jan.koester
Browse files

speedup

parent 06c8740f
Loading
Loading
Loading
Loading
+26 −71
Changes for src/event/epoll.cpp: 26 added lines, 71 removed lines.
Original line number Diff line number Diff line
@@ -93,53 +93,8 @@ namespace netplus {
        int _Timeout;
    };

    // ------------------------------------------------------------
    // RAII Rearm (EPOLLONESHOT)
    // ------------------------------------------------------------
    class EpollArmGuard {
    public:
        EpollArmGuard(int fd, std::shared_ptr<con> c, pollapi* pptr)
            : _fd(fd), _con(std::move(c)), _pptr(pptr) {}

        ~EpollArmGuard() {
            if (!_armed) return;
            if (!_con || !_con->csock) return;

            int next = EPOLLRDHUP | EPOLLONESHOT;

            // during handshake => need IN, and OUT only if pending writes
            if (!_con->csock->getHandshakeDone()) {
                next |= EPOLLIN;
                if (_con->csock->hasPendingWrite())
                    next |= EPOLLOUT;
            } else {
                next |= EPOLLIN;
                if (_con->SendData.pos < _con->SendData.size() || _con->csock->hasPendingWrite())
                    next |= EPOLLOUT;
                // If the TLS layer still has buffered records from a previous
                // prime_recv, the kernel socket may be empty so EPOLLIN alone
                // won't fire.  Adding EPOLLOUT (almost always ready) forces
                // epoll_wait to return, giving us another chance to process
                // the buffered data.
                if (_con->csock->hasBufferedData())
                    next |= EPOLLOUT;
            }

            try {
                _pptr->setpollEventsFd(_fd, next);
            } catch (...) {
                // ignore
            }
        }

        void disarm() { _armed = false; }

    private:
        bool _armed = true;
        int _fd;
        std::shared_ptr<con> _con;
        pollapi* _pptr;
    };
    // Level-triggered epoll no longer requires re-arming
    // (EPOLLONESHOT eliminated for better performance)

    // ------------------------------------------------------------
    // poll implementation
@@ -269,18 +224,19 @@ namespace netplus {
                }

                epoll_event setevent{};
                setevent.events = EPOLLIN | EPOLLRDHUP | EPOLLONESHOT;
                setevent.events = EPOLLIN | EPOLLRDHUP;  // level-triggered
                setevent.data.fd = fd;

                epoll_ctl(_pollFD, EPOLL_CTL_ADD, fd, &setevent);

                evconnection->ConnectEvent(*ccon, tid, args);

                // If ConnectEvent queued data (e.g. HTTP/2 SETTINGS),
                // re-arm with EPOLLOUT so it gets flushed immediately.
                // With level-triggered, socket stays registered and will fire
                // on both IN and OUT as long as conditions persist.
                // Update events to include EPOLLOUT if data is pending.
                if (ccon->SendData.pos < ccon->SendData.size() || ccon->csock->hasPendingWrite()) {
                    epoll_event ev{};
                    ev.events = EPOLLIN | EPOLLOUT | EPOLLRDHUP | EPOLLONESHOT;
                    ev.events = EPOLLIN | EPOLLOUT | EPOLLRDHUP;  // level-triggered
                    ev.data.fd = fd;
                    epoll_ctl(_pollFD, EPOLL_CTL_MOD, fd, &ev);
                }
@@ -301,7 +257,6 @@ namespace netplus {
                return;
            }

            EpollArmGuard rearm(fd, c, this);
            bool needClose = false;
            // Pre-allocate buffer outside the inner loops to avoid
            // repeated heap allocation on every recv iteration.
@@ -340,8 +295,8 @@ namespace netplus {
                                    c->csock->flush_out();
                                } catch (NetException& e) {
                                    if (e.getErrorType() == NetException::Note) {
                                        rearm.disarm();
                                        setpollEventsFd(fd, EPOLLOUT | EPOLLRDHUP | EPOLLONESHOT);
                                        // Level-triggered: socket will re-fire when ready
                                        setpollEventsFd(fd, EPOLLOUT | EPOLLRDHUP);
                                        return;
                                    }
                                    throw;
@@ -360,14 +315,7 @@ namespace netplus {
                            }
                        }

                        // Re-arm if handshake still in progress
                        if (!c->csock->getHandshakeDone()) {
                            int ev = EPOLLIN | EPOLLRDHUP | EPOLLONESHOT;
                            if (c->csock->hasPendingWrite()) ev |= EPOLLOUT;
                            rearm.disarm();
                            setpollEventsFd(fd, ev);
                            return;
                        }
                        // No need to re-arm with level-triggered epoll

                        // Post-handshake: if the socket has a framing
                        // callback, invoke it with stream_id=0 to produce
@@ -450,15 +398,19 @@ namespace netplus {
                                    c->csock->flush_out();
                                } catch (NetException& e) {
                                    if (e.getErrorType() == NetException::Note) {
                                        rearm.disarm();
                                        setpollEventsFd(fd, EPOLLOUT | EPOLLRDHUP | EPOLLONESHOT);
                                        // Level-triggered: socket will re-fire when ready
                                        setpollEventsFd(fd, EPOLLOUT | EPOLLRDHUP);
                                        return;
                                    }
                                    throw;
                                }
                            }

                            if (c->SendData.pos < c->SendData.size()) {
                            // Send loop: drain all available data in one event
                            // This significantly improves throughput for large files by
                            // reducing epoll_ctl system call overhead
                            bool sendBlockedWouldBlock = false;
                            while (c->SendData.pos < c->SendData.size() && !sendBlockedWouldBlock && !needClose) {
                                size_t remaining = c->SendData.size() - c->SendData.pos;
                                size_t sendlen = std::min((size_t)BLOCKSIZE, remaining);
                                buffer out(c->SendData.data() + c->SendData.pos, sendlen);
@@ -470,9 +422,8 @@ namespace netplus {
                                        c->csock->flush_out();
                                } catch (NetException& e) {
                                    if (e.getErrorType() == NetException::Note) {
                                        rearm.disarm();
                                        setpollEventsFd(fd, EPOLLOUT | EPOLLRDHUP | EPOLLONESHOT);
                                        return;
                                        sendBlockedWouldBlock = true;
                                        break;
                                    }
                                    throw;
                                }
@@ -490,9 +441,15 @@ namespace netplus {
                                        c->SendData.erase(c->SendData.begin(), c->SendData.begin() + c->SendData.pos);
                                        c->SendData.pos = 0;
                                    }
                                } else {
                                    // No progress made, socket likely full
                                    break;
                                }
                            }

                            // With level-triggered, socket stays ready for EPOLLOUT
                            // No explicit re-arm needed

                            if (c->SendData.pos >= c->SendData.size() && !c->csock->hasPendingWrite()) {
                                c->SendData.clear();
                                c->SendData.pos = 0;
@@ -503,10 +460,9 @@ namespace netplus {

                } catch (NetException& e) {
                    if (e.getErrorType() == NetException::Note) {
                        int ev = EPOLLIN | EPOLLRDHUP | EPOLLONESHOT;
                        int ev = EPOLLIN | EPOLLRDHUP;
                        if (c->csock->hasPendingWrite() || c->SendData.pos < c->SendData.size())
                            ev |= EPOLLOUT;
                        rearm.disarm();
                        setpollEventsFd(fd, ev);
                        return;
                    }
@@ -517,7 +473,6 @@ namespace netplus {
            } // unlock event_mutex

            if (needClose) {
                rearm.disarm();
                CloseEventHandler(fd, tid, args);
            }
        }