Bug 27777 - fclose does a linear search, takes ages when many FILE* are opened
Summary: fclose does a linear search, takes ages when many FILE* are opened
Status: UNCONFIRMED
Alias: None
Product: glibc
Classification: Unclassified
Component: stdio (show other bugs)
Version: 2.34
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-04-24 21:50 UTC by Alexandre Ferrieux
Modified: 2021-04-24 21:51 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Alexandre Ferrieux 2021-04-24 21:50:58 UTC
If one has *many* opened streams (which is possible if the max number of opened file descriptors per process has been tuned beyond the typical 1024), fclose() starts being *very* slow.

The root cause is the following linear search in genops.c/_IO_un_link:

d18ea0c5	68		for (f = &_IO_list_all->file._chain; *f; f = &(*f)->_chain)
9964a145	69		  if (*f == (FILE *) fp)
40a55d20	70		    {
cedb4109	71		      *f = fp->file._chain;
40a55d20        72		      break;
UD	        73		    }

Clearly a singly-linked list does not allow for O(1) removal.
Given what I understand of the design constraints of this list, the most natural fix would be to switch to a doubly-linked list.