[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[screen-devel] [PATCH for master] Make closeallfiles() faster
From: |
Amadeusz Sławiński |
Subject: |
[screen-devel] [PATCH for master] Make closeallfiles() faster |
Date: |
Sat, 2 Nov 2019 23:21:54 +0100 |
Optimize startup time, making closeallfiles() faster, by doing less
system calls. Instead of calling close for each possible file, use
poll() to check if file exist at all. On linux with open file limit set
to 1048576, it should do 1024 poll() calls instead of 1048576 close().
Bug: 55618
Signed-off-by: Amadeusz Sławiński <address@hidden>
---
src/misc.c | 30 +++++++++++++++++++++++++-----
1 file changed, 25 insertions(+), 5 deletions(-)
diff --git a/src/misc.c b/src/misc.c
index ad49a844..7349e30f 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -30,6 +30,7 @@
#include "misc.h"
+#include <poll.h>
#include <sys/types.h>
#include <sys/stat.h> /* mkdir() declaration */
#include <signal.h>
@@ -151,11 +152,30 @@ void Kill(pid_t pid, int sig)
void closeallfiles(int except)
{
- int f;
- f = getdtablesize();
- while (--f > 2)
- if (f != except)
- close(f);
+ struct pollfd pfd[1024];
+ int maxfd, i, fd, ret, z;
+
+ i = 3; /* skip stdin, stdout and stderr */
+ maxfd = getdtablesize();
+
+ while (i < maxfd) {
+ memset(pfd, 0, sizeof(pfd));
+
+ z = 0;
+ for (fd = i; fd < maxfd && fd < i + 1024; fd++)
+ pfd[z++].fd = fd;
+
+ ret = poll(pfd, fd - i, 0);
+ if (ret < 0)
+ Panic(errno, "poll");
+
+ z = 0;
+ for (fd = i; fd < maxfd && fd < i + 1024; fd++)
+ if (!(pfd[z++].revents & POLLNVAL) && fd != except)
+ close(fd);
+
+ i = fd;
+ }
}
/*
--
2.23.0
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [screen-devel] [PATCH for master] Make closeallfiles() faster,
Amadeusz Sławiński <=