Bug 28277 - ftell slow with wide characters
Summary: ftell slow with wide characters
Status: UNCONFIRMED
Alias: None
Product: glibc
Classification: Unclassified
Component: stdio (show other bugs)
Version: 2.34
: P2 minor
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-08-27 03:57 UTC by Ruud Harmsen
Modified: 2021-08-27 03:57 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 Ruud Harmsen 2021-08-27 03:57:32 UTC
ftell slow with wide characters

In a wide character stream, ftell gets slower depending on where we are in a 4 kiB block: at the start of it, ftell is about as fast as with a non-wide stream, around 100 ns in my test. Towards the end of the 4 kiB, it gets some 30 to 40 times slower. This can be problematic in applications that handle lots of data.

Strangely, this is not affected by changing the stdio buffer size by calling setvbuf. The change from slow to fast always happens when (bytepos modulo 4096 + 1) goes from 4096 to 1.

Tested with glibc 2.31 as distributed with Linux Mint 20.1, and with a freshly compiled 2.34. FreeBSD 12.2 does not have this problem, ftell is always fast there, like 22 ns.

See also https://rudhar.com/sfreview/siworin/siworin09.htm . Demonstration program available in https://rudhar.com/sfreview/siworin/src/siworin09.c .

When debugging this, I actually see a loop happening, in iconv/loop.c, for example lines 309, 322 and 336. I don't see why ftell would need to involve any looping, or any character conversion: as I see it, all ftell does is return the current value of a stdio-internal counter, so it should be extremely fast.