PhysicalSocketServer: remove heap-based epoll_event handling.

This change deletes heap-based handling of dynamic number of epoll
events handled per call to epoll, with the assumption
that PSS load is likely not dominated by the epoll syscalls. This
simplifies the logic in the code and removes a heap allocation.

Bug: webrtc:11567
Change-Id: I34fbe1fa8bf0a037bf849a4adac1a0a333c9dd86
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/175563
Commit-Queue: Markus Handell <handellm@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#31358}
diff --git a/rtc_base/physical_socket_server.cc b/rtc_base/physical_socket_server.cc
index cf65300..3a95331 100644
--- a/rtc_base/physical_socket_server.cc
+++ b/rtc_base/physical_socket_server.cc
@@ -1365,12 +1365,6 @@
 
 #if defined(WEBRTC_USE_EPOLL)
 
-// Initial number of events to process with one call to "epoll_wait".
-static const size_t kInitialEpollEvents = 128;
-
-// Maximum number of events to process with one call to "epoll_wait".
-static const size_t kMaxEpollEvents = 8192;
-
 void PhysicalSocketServer::AddEpoll(Dispatcher* pdispatcher) {
   RTC_DCHECK(epoll_fd_ != INVALID_SOCKET);
   int fd = pdispatcher->GetDescriptor();
@@ -1437,20 +1431,13 @@
     tvStop = TimeAfter(cmsWait);
   }
 
-  if (epoll_events_.empty()) {
-    // The initial space to receive events is created only if epoll is used.
-    epoll_events_.resize(kInitialEpollEvents);
-  }
-
   fWait_ = true;
-
   while (fWait_) {
     // Wait then call handlers as appropriate
     // < 0 means error
     // 0 means timeout
     // > 0 means count of descriptors ready
-    int n = epoll_wait(epoll_fd_, &epoll_events_[0],
-                       static_cast<int>(epoll_events_.size()),
+    int n = epoll_wait(epoll_fd_, epoll_events_.data(), epoll_events_.size(),
                        static_cast<int>(tvWait));
     if (n < 0) {
       if (errno != EINTR) {
@@ -1483,13 +1470,6 @@
       }
     }
 
-    if (static_cast<size_t>(n) == epoll_events_.size() &&
-        epoll_events_.size() < kMaxEpollEvents) {
-      // We used the complete space to receive events, increase size for future
-      // iterations.
-      epoll_events_.resize(std::max(epoll_events_.size() * 2, kMaxEpollEvents));
-    }
-
     if (cmsWait != kForever) {
       tvWait = TimeDiff(tvStop, TimeMillis());
       if (tvWait < 0) {
diff --git a/rtc_base/physical_socket_server.h b/rtc_base/physical_socket_server.h
index 2f53d4d..e21e53b 100644
--- a/rtc_base/physical_socket_server.h
+++ b/rtc_base/physical_socket_server.h
@@ -16,6 +16,7 @@
 #define WEBRTC_USE_EPOLL 1
 #endif
 
+#include <array>
 #include <memory>
 #include <set>
 #include <vector>
@@ -81,6 +82,9 @@
   void Update(Dispatcher* dispatcher);
 
  private:
+  // The number of events to process with one call to "epoll_wait".
+  static constexpr size_t kNumEpollEvents = 128;
+
   typedef std::set<Dispatcher*> DispatcherSet;
 
   void AddRemovePendingDispatchers() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
@@ -95,8 +99,12 @@
   bool WaitEpoll(int cms);
   bool WaitPoll(int cms, Dispatcher* dispatcher);
 
+  // This array is accessed in isolation by a thread calling into Wait().
+  // It's useless to use a SequenceChecker to guard it because a socket
+  // server can outlive the thread it's bound to, forcing the Wait call
+  // to have to reset the sequence checker on Wait calls.
+  std::array<epoll_event, kNumEpollEvents> epoll_events_;
   const int epoll_fd_ = INVALID_SOCKET;
-  std::vector<struct epoll_event> epoll_events_;
 #endif  // WEBRTC_USE_EPOLL
   DispatcherSet dispatchers_ RTC_GUARDED_BY(crit_);
   DispatcherSet pending_add_dispatchers_ RTC_GUARDED_BY(crit_);