aboutsummaryrefslogtreecommitdiff
path: root/libio/stdfiles.c
diff options
context:
space:
mode:
authorAlexandre Ferrieux <alexandre.ferrieux@orange.com>2024-05-16 05:54:30 -0700
committerH.J. Lu <hjl.tools@gmail.com>2024-05-17 14:13:25 -0700
commit2a99e2398d9d717c034e915f7846a49e623f5450 (patch)
treebbd97d056e8aaa0fbd8aece4f7a6c055c638fd3c /libio/stdfiles.c
parenta81cdde1cb9d514fc8f014ddf21771c96ff2c182 (diff)
downloadglibc-2a99e2398d9d717c034e915f7846a49e623f5450.zip
glibc-2a99e2398d9d717c034e915f7846a49e623f5450.tar.gz
glibc-2a99e2398d9d717c034e915f7846a49e623f5450.tar.bz2
Use a doubly-linked list for _IO_list_all (bug 27777)
This patch fixes BZ #27777 "fclose does a linear search, takes ages when many FILE* are opened". Simply put, the master list of opened (FILE*), namely _IO_list_all, is a singly-linked list. As a consequence, the removal of a single element is in O(N), which cripples the performance of fclose(). The patch switches to a doubly-linked list, yielding O(1) removal. The one padding field in struct _IO_FILE, __pad5, is renamed to _prevchain for a doubly-linked list. Since fields in struct _IO_FILE after the _lock field are internal to glibc and opaque to applications. We can change them as long as the size of struct _IO_FILE is unchanged, which is checked as the part of glibc ABI with sizes of _IO_2_1_stdin_, _IO_2_1_stdout_ and _IO_2_1_stderr_. NB: When _IO_vtable_offset (fp) == 0, copy relocation will cover the whole struct _IO_FILE. Otherwise, only fields up to the _lock field will be copied to applications at run-time. It is used to check if the _prevchain field can be safely accessed. After opening 2 million (FILE*), the fclose() of 100 of them takes quite a few seconds without the patch, and under 2 seconds with it on a loaded machine. No test is added since there are no functional changes. Co-Authored-By: H.J. Lu <hjl.tools@gmail.com> Signed-off-by: Alexandre Ferrieux <alexandre.ferrieux@orange.com> Signed-off-by: H.J. Lu <hjl.tools@gmail.com> Reviewed-by: Carlos O'Donell <carlos@redhat.com>
Diffstat (limited to 'libio/stdfiles.c')
-rw-r--r--libio/stdfiles.c15
1 files changed, 15 insertions, 0 deletions
diff --git a/libio/stdfiles.c b/libio/stdfiles.c
index cd8eca8..12db164 100644
--- a/libio/stdfiles.c
+++ b/libio/stdfiles.c
@@ -54,4 +54,19 @@ DEF_STDFILE(_IO_2_1_stdout_, 1, &_IO_2_1_stdin_, _IO_NO_READS);
DEF_STDFILE(_IO_2_1_stderr_, 2, &_IO_2_1_stdout_, _IO_NO_READS+_IO_UNBUFFERED);
struct _IO_FILE_plus *_IO_list_all = &_IO_2_1_stderr_;
+
+/* Finish double-linking stdin, stdout, and stderr in a constructor.
+ Static initialization cannot complete the _prevchain setup. */
+
+__THROW __attribute__ ((constructor))
+static void
+_IO_stdfiles_init (void)
+{
+ struct _IO_FILE **f;
+ for (f = (struct _IO_FILE **) &_IO_list_all;
+ *f != NULL;
+ f = &(*f)->_chain)
+ (*f)->_prevchain = f;
+}
+
libc_hidden_data_def (_IO_list_all)