From db7338fd7518b068269aa102e821ac388f519cd9 Mon Sep 17 00:00:00 2001 From: normal Date: Wed, 28 Nov 2018 01:10:40 +0000 Subject: io.c (rb_update_max_fd): use F_GETFL if possible On 64-bit Linux, fstat() needs to fill out a 144 byte struct while F_GETFL only needs to return 8 bytes. Fwiw, F_GETFD requires an additional rcu_read_lock and bitmap check; so it's obviously more expensive than F_GETFL on Linux. Reduce stack usage of rb_update_max_fd from 184 to 24 bytes. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66060 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- io.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'io.c') diff --git a/io.c b/io.c index 28dfd56a43..d59bde93cf 100644 --- a/io.c +++ b/io.c @@ -193,14 +193,22 @@ static rb_atomic_t max_file_descriptor = NOFILE; void rb_update_max_fd(int fd) { - struct stat buf; rb_atomic_t afd = (rb_atomic_t)fd; rb_atomic_t max_fd = max_file_descriptor; + int err; if (afd <= max_fd) return; - if (fstat(fd, &buf) != 0 && errno == EBADF) { +#if defined(HAVE_FCNTL) && defined(F_GETFL) + err = fcntl(fd, F_GETFL) == -1; +#else + { + struct stat buf; + err = fstat(fd, &buf) != 0; + } +#endif + if (err && errno == EBADF) { rb_bug("rb_update_max_fd: invalid fd (%d) given.", fd); } -- cgit v1.2.3