Bug 28277

Summary: ftell slow with wide characters
Product: glibc Reporter: Ruud Harmsen <infor>
Component: stdioAssignee: Not yet assigned to anyone <unassigned>
Status: UNCONFIRMED ---    
Severity: minor    
Priority: P2    
Version: 2.34   
Target Milestone: ---   
Host: Target:
Build: Last reconfirmed:

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.