Bug 45628 - std::fstream::tellg invalidates I/O buffer
std::fstream::tellg invalidates I/O buffer
Status: RESOLVED FIXED
Product: gcc
Classification: Unclassified
Component: libstdc++
4.5.0
: P3 normal
: 4.6.0
Assigned To: Not yet assigned to anyone
:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2010-09-10 06:58 UTC by David Krauss
Modified: 2010-09-25 11:09 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2010-09-10 12:11:41


Attachments
simple testcase (552 bytes, text/plain)
2010-09-10 07:09 UTC, David Krauss
Details
Draft (1.04 KB, patch)
2010-09-10 19:01 UTC, Paolo Carlini
Details | Diff
alternative approach. untested (1.44 KB, patch)
2010-09-10 19:26 UTC, David Krauss
Details | Diff
Tested x86_64-linux, mainline (2.83 KB, patch)
2010-09-17 12:42 UTC, Paolo Carlini
Details | Diff
Works with codecvt. Tested Tested x86_64-darwin, mainline (2.46 KB, patch)
2010-09-17 17:51 UTC, David Krauss
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description David Krauss 2010-09-10 06:58:17 UTC
Querying the position in a file uses filebuf::seekoff( 0, ios::cur ). Although the Standard specifies that this combination of parameters does not flush putback or the output sequence, it does anyway, which results in a performance hit. (§27.8.1.4/11)

Regression: http://gcc.gnu.org/viewcvs/trunk/libstdc%2B%2B-v3/include/bits/fstream.tcc?r1=68163&r2=68420 (@@ -473,41 +486,26 @@)

This has been broken a long time!
Comment 1 David Krauss 2010-09-10 07:09:10 UTC
Created attachment 21762 [details]
simple testcase

This little program attempts to read three characters from its own source, checking the position each time. Reading three bytes from buffered file should only underflow once, right?
Comment 2 Paolo Carlini 2010-09-10 10:55:01 UTC
_M_terminate_output, correctly, does nothing in this case, cannot be the problem, and there is nothing wrong wrt the standard mandated behavior. The "problem" is that in our implementation, similarly to traditional C stdio impls, reading and writing are completely separate operations, and the user switches between the two with seeks, essentially. Any seek puts back the internal status to "uncommitted" (_M_reading = false, _M_writing = false) and afterwards the user can start *either* reading or writing, irrespective of the previous history, and the seek logic doesn't know what will actually happen in the future, of course. The user should not perform redundant seeks, because they have a cost, they do something more than just "seeking". On the other hand, a series of read or write operations has maximum performance, we don't think we could possibly do better. Thus, I'm open to ideas, but I don't think that within the current design one can change / improve much. Note that the patch you linked is exactly the one implementing the above semantics.
Comment 3 Paolo Carlini 2010-09-10 12:09:23 UTC
I *think* it can work to minimally change what we have now to not reset the get area buffers when (0, ios::cur) and we have been reading: as far as I can see, if in that specific case we get back to reading again, the get area remains completely valid indeed.
Comment 4 Paolo Carlini 2010-09-10 12:20:35 UTC
Does not work: when we reach the end of the buffer and we access again the file to refill it, we start reading from the wrong position, the position we seeked to.
Comment 5 Paolo Carlini 2010-09-10 12:36:59 UTC
To clarify: when we start reading in a buffered mode, the first underflow reads the buffer and leaves the physical file at the first char beyond the buffer. If we do afterwards a seek to the current reading position, belonging to the buffer, the physical position along the file also changes of course, an underlying fseek is performed. Then, if we don't refresh the buffer with a new underflow, an inconsistency is born: the physical position along the file doesn't correspond to the first char after the buffer and the next underflow will read from the wrong position.
Comment 6 Don Wakefield 2010-09-10 14:06:36 UTC
Re: comment 5 - what is needed is for filebuf::seekoff(0,ios::cur) to:

  1) *not* invalidate the buffer
  2) *not* move the file pointer

since all that special case asks is "where am I in the 'logical' file?"

This can be accomplished by having filebuf::seekoff() recognize an off_type of 0, and a seekdir of cur, and special-case the code to call _M_file.seekoff(0,cur) (not moving the file pointer, right?) and then adjusting the resulting pos_type to reflect the true, earlier position held by gptr().

Am I missing something?
Comment 7 Paolo Carlini 2010-09-10 14:39:58 UTC
Then, seekoff would also return a position beyond the buffer, right? Or you want it to return 1 anyway? Actually, I think the standard want us to use width * off for the underlying fseek anyway, not only for off == 0, and this is not what we have been doing. I think there is something seriously different here, beyond the performance issue, which we should ponder much more, after so many years.
Comment 8 Don Wakefield 2010-09-10 14:54:07 UTC
Paolo, yes, _M_file.seekoff(0,cur) would return the current physical file position, and then filebuf::seekoff would adjust the returned pos_type to reflect the position within the *logical* file, framed by the buffer and pointed to by gptr().

As for the mechanics of width*off, I confess that locale issues leave me completely befuddled, so I won't try to address that.
Comment 9 Paolo Carlini 2010-09-10 15:00:50 UTC
Ok. I don't think we should change the code to deal such specially with off == 0, if we are going to change it we should decouple the return value from what the underlying seek returns, and always call fseek(..., width * off, ...) as the standard mandates. Then dealing with off == 0 becomes simple.
Comment 10 Don Wakefield 2010-09-10 15:15:16 UTC
(In reply to comment #9)
> Ok. I don't think we should change the code to deal such specially with off ==
> 0, if we are going to change it we should decouple the return value from what
> the underlying seek returns, and always call fseek(..., width * off, ...) as
> the standard mandates. Then dealing with off == 0 becomes simple.

I'm not sure I understand what you are saying. My concern is that calls to filebuf::seekoff(0,ios::cur) should not invalidate the buffer just to return a correct answer to "where am I?". Whether this is an accident of history or not, I've seen this usage enshrined in code as a respected idiom.

Does your text above specify this?
Comment 11 Paolo Carlini 2010-09-10 15:19:34 UTC
Sure. What I meant - contrary to wait you said, I think - is that an elegant and complete solution to this issue involves changing much more generally our code to *always* behave as if fseek(off * width) were called, not just fseek(0) in the special case you care about.
Comment 12 Don Wakefield 2010-09-10 15:24:00 UTC
(In reply to comment #11)
> Sure. What I meant - contrary to wait you said, I think - is that an elegant
> and complete solution to this issue involves changing much more generally our
> code to *always* behave as if fseek(off * width) were called, not just fseek(0)
> in the special case you care about.

Okay, I understand better now. My apologies, and thanks for your comments on this issue.
Comment 13 Paolo Carlini 2010-09-10 15:45:13 UTC
Good, I think we are close to a fix, I'm already testing something. So, do we have a symmetric issue with the put area or not? I'm not sure.
Comment 14 David Krauss 2010-09-10 15:59:57 UTC
(In reply to comment #13)
> Good, I think we are close to a fix, I'm already testing something. So, do we
> have a symmetric issue with the put area or not? I'm not sure.

I believe so. tellg and tellp are both handled by seekoff( 0, ios::cur ), and the required behavior doesn't differentiate between them, partly because for an fstream the get and put pointers are the same. (The result doesn't depend on the pointers, it comes from fseek.)
Comment 15 David Krauss 2010-09-10 16:15:55 UTC
(In reply to comment #14)

> (The result doesn't depend on
> the pointers, it comes from fseek.)

I re-read Comment 5 and understand it this time ;v) . Well, any solution should fix both tellg and tellp, since the pointers are the same upon synchronization.

Then, yes, we need to check which type of operation occurred last, and either update whichever pointer is stale, or selectively use the correct one.
Comment 16 Paolo Carlini 2010-09-10 17:11:48 UTC
Actually, however, I don't think we can really always call fseek(off * width) as the Standard want us to do. In a sense I'm happy because the change is gonna be less invasive, on the other hand I'm a bit puzzled.
Comment 17 David Krauss 2010-09-10 17:25:33 UTC
(In reply to comment #16)
> Actually, however, I don't think we can really always call fseek(off * width)
> as the Standard want us to do. In a sense I'm happy because the change is gonna
> be less invasive, on the other hand I'm a bit puzzled.

Could you post a patch? I'm not sure what you mean by generalizing (off * width), if off == 0 then width is irrelevant. If width < 0 then the only valid value for off is 0. If width > 0, off != 0 then repositioning is the primary effect.

The task is to call fseek(0,cur), and then subtract the number of bytes in the put area plus the "external characters," right?
Comment 18 Paolo Carlini 2010-09-10 17:29:41 UTC
I'm almost ready for the patch, please be patient ;) If look at the standard, it says that the last step of seekoff is *always* as if calling fseek(..., off * width, ...). If look at the current code, we have the concept of __computed_off and, in many cases we end up calling the equivalent of fseek with something != off * width. I'm changing that to (0, cur) for the case you care about, but not changing anything else otherwise.
Comment 19 Paolo Carlini 2010-09-10 17:30:49 UTC
Of course here I'm always under the assumption width > 0.
Comment 20 David Krauss 2010-09-10 17:35:48 UTC
(In reply to comment #17)

> The task is to call fseek(0,cur), and then subtract the number of bytes in the
> put area plus the "external characters," right?

Er, I don't mean "bytes in the put area" exactly, but you know what I mean… what I'm asking is, how does your simplification relate to the task of figuring out how many file bytes the buffers hold, without flushing them, which the code does not currently seem designed to do.

For regression, note that the code previous to the linked patch was

// NB: Need to do this in case _M_file in indeterminate
// state, ie _M_file._offset == -1
pos_type __tmp = _M_file.seekoff(__off, ios_base::cur, __mode);
if (__tmp >= 0)
  {
    // Seek successful.
    __ret = __tmp;
    __ret += std::max(this->_M_out_cur, this->_M_in_cur) 
	     - _M_filepos;
  }

which does not appear to do multibyte compensation correctly. Was _M_filepos the number of file bytes in whichever area was currently being used?
Comment 21 David Krauss 2010-09-10 17:40:12 UTC
(In reply to comment #18)
> I'm almost ready for the patch, please be patient ;) If look at the standard,
> it says that the last step of seekoff is *always* as if calling fseek(..., off
> * width, ...). If look at the current code, we have the concept of
> __computed_off and, in many cases we end up calling the equivalent of fseek
> with something != off * width. I'm changing that to (0, cur) for the case you
> care about, but not changing anything else otherwise.

The standard says always to use (off * width, whence) but that is just the external effect if buffering is transparent. __computed_off compensates for the file pointer being necessarily different from gptr(), pptr(). (You can't seek for every putc!)

Don't mean to be impatient, just trying to follow along the discussion…
Comment 22 Paolo Carlini 2010-09-10 17:42:25 UTC
Good. Then I have a draft almost ready ;)
Comment 23 David Krauss 2010-09-10 18:53:49 UTC
(In reply to comment #22)
> Good. Then I have a draft almost ready ;)

I have a very straightforward, low-impact solution, but I haven't tested it. (My tree is pretty out of date.) Would you like to try it, if you're having trouble?
Comment 24 Paolo Carlini 2010-09-10 19:01:38 UTC
Created attachment 21768 [details]
Draft

This is what I have so far, unfortunately I cannot work only on this today. Anyway, it passes testing and this specific testcase, but is incomplete vs wchar_t. If you have something which you are confident works fine for wchar_t too, I can give it a try later today or over the next days, thanks.
Comment 25 David Krauss 2010-09-10 19:26:23 UTC
Created attachment 21769 [details]
alternative approach. untested

I hope this compiles ;v) . But it seems to "color within the lines."

Why does your patch call setp/setg to (re?)invalidate the opposite area? And then declares it is neither reading nor writing? Also, -1 return from _M_seek is not handled in seekoff.
Comment 26 David Krauss 2010-09-10 19:30:00 UTC
(In reply to comment #25)
> Created an attachment (id=21769) [edit]
> alternative approach. untested
> 
> I hope this compiles ;v) . But it seems to "color within the lines."

Bah, it doesn't. I missed an underscore at 

    vv
    __is_tell? __state_type(_M_state_last) : move(_M_state_last);
    ^^
Comment 27 Paolo Carlini 2010-09-10 19:31:19 UTC
Note that certainly we don't want to use C++0x stuff here. Also, one thing at a time of course, thus if we have been missing some error checking, etc, it's for another time.
Comment 28 Paolo Carlini 2010-09-10 19:34:20 UTC
PS: you are right that we have to check that _M_seek succeeds before adding back __computed_off.
Comment 29 Paolo Carlini 2010-09-10 19:51:45 UTC
And, please, if you want to help, manage to run the testsuite, we have got some pretty nasty testcases ;)
Comment 30 David Krauss 2010-09-10 20:33:54 UTC
(In reply to comment #29)
> And, please, if you want to help, manage to run the testsuite, we have got some
> pretty nasty testcases ;)

I'll see if I can compile the latest… guess it's more useless to have an old tree than one that merely doesn't compile.

Also, I failed to account for overflow() called from _M_terminate_output flushing the put sequence and setting the file position to the actual location. So my patch is buggy with tell after write. It appears that your patch will still flush upon tell after write.

C++0x, easy enough to eliminate. I'm not aware of my patch fixing anything besides the problem at hand…
Comment 31 Paolo Carlini 2010-09-11 04:27:45 UTC
I'm afraid that the situation I outlined in Comment #5 is just the simple one. The real problem with the new scheme - which tries to deal specially with (0, cur) by not moving the file pointer - is when *writes* follow the seek. After a while the buffer becomes full and must be flushed to the file starting at the logical position corresponding to the previous seek. Thus - it seems to me - the file pointer must be finally adjusted. How to do that without saving anything in the filebuf? (note that within the current ABI we cannot add data members)
Comment 32 David Krauss 2010-09-11 04:49:30 UTC
(In reply to comment #31)
> I'm afraid that the situation I outlined in Comment #5 is just the simple one.
> The real problem with the new scheme - which tries to deal specially with (0,
> cur) by not moving the file pointer - is when *writes* follow the seek. After a
> while the buffer becomes full and must be flushed to the file starting at the
> logical position corresponding to the previous seek. Thus - it seems to me -
> the file pointer must be finally adjusted. How to do that without saving
> anything in the filebuf? (note that within the current ABI we cannot add data
> members)

I don't see how this is particularly difficult. A seekoff(0,ios_cur) operation should only ever call lseek(0,SEEK_CUR). It does not point the file position inside the buffer.

For simple byte-oriented case:

read, tell, write: egptr corresponds to to file marker. Tell finds logical position using gptr-egptr. Write invalidates get area and starts fresh.

write, tell, write: pbase corresponds to file marker. Tell finds logical position using pptr-pbase. Pointers and marker are still valid for next write.

To handle writes, we simply have to avoid calling _M_seek and overflow.
Comment 33 David Krauss 2010-09-11 04:56:52 UTC
Note, I am not attempting to tell after write with a nontrivial codecvt installed. Maybe the issue of Comment #5 is only in the general case?

I suppose it leaves UTF-8 files still a bit slow, but I still think that's pretty well justified.
Comment 34 Paolo Carlini 2010-09-11 09:21:49 UTC
Run the full testsuite, and you will see. In general, if you simply do fseek(0, cur) and then start writing, when eventually you have to flush you need the actual logical position in the file - the last fseek(0, cur) - 'something' - which is not available anywhere. I'm not saying it cannot be implemented, I'm very dubious it can without breaking the ABI by adding an additional data member, which we cannot do at the moment. To be honest I also don't think the issue is very serious if only because nobody complained in 7 years, and we have a lot to do for C++0x. Thus, if you can help with something concrete minimally passing the testsuite and clearly addressing the concerns above, excellent, otherwise, I cannot anticipate now when we are going to do something here. Just to be honest.
Comment 35 David Krauss 2010-09-11 09:43:26 UTC
(In reply to comment #34)
> Run the full testsuite, and you will see.

Lol, you're still looking at this too? I *just* got those pesky four testcases done. I wasn't manually putting the codecvt state into the fpos in the special case. I'll rerun the entire suite and post the patch here.

> In general, if you simply do fseek(0,
> cur) and then start writing, when eventually you have to flush you need the
> actual logical position in the file - the last fseek(0, cur) - 'something' -
> which is not available anywhere.

No, if fseek(0,cur) is implemented to have no side effects, it has NO side effects. Nothing is lost. When the flush happens, it uses the logical position obtained at the last flush, just as if fseek(0,cur) never occurred.
Comment 36 Paolo Carlini 2010-09-11 10:03:09 UTC
I'm traveling. Note, I don't understand how you are addressing my concerns, thus whatever results you get from the testsuite, make sure we are not regressing on the situation I outlined, thus write a new testcase reading in in the buffer, say, 0123456789, then seeking to 0, reading consecutive positions up to 5 via simple get, calling seekoff(0, cur), put x in the place of 5. Then close, reopen, and check that you have 01234x6789.
Comment 37 Paolo Carlini 2010-09-17 12:42:18 UTC
Created attachment 21819 [details]
Tested x86_64-linux, mainline

This is a carefully tested patch (tested in mainline, per the normal policy, where I also added two additional seekoff correctness testcases), which works in limited circumstances (enough to fix the testcase, anyway) when I can convince myself it's fully correct and consistent with our general framework. My plan is committing it first and then possibly generalizing it, always together with additional accompanying testcases, anyway.
Comment 38 David Krauss 2010-09-17 17:51:59 UTC
Created attachment 21822 [details]
Works with codecvt. Tested Tested x86_64-darwin, mainline

Ah, now I see the trick:

	if (__off == 0 && !(_M_mode & ios_base::out))

So if the file is open for writing, disable the optimization.

I had a problem with this condition for these testcases:

27_io/basic_filebuf/sputbackc/char/1-io.cc
27_io/basic_filebuf/sputbackc/char/2-io.cc

which contain code such as

    strmsz_2 = fb_01.sputn(", i wanna reach out and", 10);
    fb_01.pubseekoff(0, std::ios_base::cur); // if this doesn't flush
    c1 = fb_01.sgetc(); // this underflow is ignored
    c2 = fb_01.sputbackc('z'); // as well as this putback

Essentially, pubseekoff(0,cur) is being used as a sync(). I see nothing in the Standard to support that, and indeed the sync() shouldn't be needed either, so I was planning to open a new bug.

Anyway, if I apply your limitation to my patch, it passes the unmodified testsuite, so here it is.
Comment 39 David Krauss 2010-09-17 18:04:14 UTC
Oops, no, I'm on the 4.5.2 series, not mainline.
Comment 40 Paolo Carlini 2010-09-17 18:53:36 UTC
In general, our users know that seeking allows to switch from reading to writing, and viceversa (when the stream has been appropriately opened of course). This assumption remained true for years and years. Thus, for now at least, I would rather not change it, whether the Standard is completely clear in this area or not.

Also, I don't think the name __is_tell is appropriate, because of course this kinf of situation in principle can occur also when tell is not involved (like in your testcase ;)

Modulo the above comments, I think we can enable the optimization for codecvt too, yes, let me reformat your other tweaks and more cleanly incorporate the !(_M_mode & ios_base::out) thing. 
Comment 41 David Krauss 2010-09-17 19:04:54 UTC
Well, I'm happy if you'd like to merge the diffs. My code was written with the intent of optimizing the output case, too, but I guess it's not too inefficient or awkward from the perspective of input only.

I just filed a bug http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45708 about the requirement to separate reads and writes. I suppose it will be a duplicate, but maybe it is worded a little better than existing bugs. Although your current users might know the restriction, I don't think it's really well-documented. I personally observed this behavior when getting started with iostreams, and it was quite discouraging. GNU having a large part of the iostreams marketshare, I wouldn't be surprised if this were a minor stumbling block to iostreams adoption overall.
Comment 42 Paolo Carlini 2010-09-17 19:10:27 UTC
Before any other bug or analysis, I would recommend going back to the ton of discussions in 2002 / 2003 when the design of basic_filebuf has been changed to use _M_reading and _M_writing, **on purpose**. Didn't happen by chance, was a deliberate redesign of the previous design which allowed major performance improvements. And, to be clear, nobody complained anymore, *ever* all these years. After you have analyzed those discussions (look in particular for Nathan Myers and me), we can consider, for the future, alternate designs.
Comment 43 David Krauss 2010-09-17 19:11:41 UTC
Thanks for the pointer, I'll read that.
Comment 44 Paolo Carlini 2010-09-17 19:17:37 UTC
By the way, if, for the future, you mean to contribute in these areas, if you are really interested in these topics, I would recommend starting immediately the Copyright assignment paperwork http://gcc.gnu.org/contribute.html send an email to assignments@ 
  
Comment 45 David Krauss 2010-09-17 19:21:32 UTC
Already did copyright assignment :vP
Comment 46 Paolo Carlini 2010-09-17 19:26:37 UTC
Ok, thanks.
Comment 47 Paolo Carlini 2010-09-17 19:38:00 UTC
To further clarify: what you have in mind isn't something which can belong to a casual PR, is a major redesign of basic_filebuf, according to a different basic philosophy, which at the time, Nathan called unified vs non-unified, if I remember correctly. At the time we moved *away* from what you essentially want, because we believed the new design to be superior in terms of performance. Anyway, if you want to propose something different, or a variant of the old design, please post messages to the libstdc++ mailing list, not here, remember to involve Nathan in the discussions, benchmark in various circumstances the various options, in particular, if you want something similar to the old scheme make sure you are *improving* on it. Remember, in practice, that in the 7 years since we moved to new scheme, **nobody ever** asked for the old behavior, nobody complained about the performance of basic_filebuf, thus, if, in the future, we are going to change it again, we really want to be sure to do it after a **very** serious and public analysis.
Comment 48 David Krauss 2010-09-17 19:45:49 UTC
I'm having trouble finding the discussion that precedes the June 24, 2003 redesign, but I'll add "unified" to the search terms.

Actually, last week I started some changes with the aim of fixing the bug, not changing the philosophy. Rather than eliminate the state variables, just call overflow and underflow in the cases that otherwise fail. So if I finish that, and it performs well, I suppose I'll start an RFC.
Comment 49 Paolo Carlini 2010-09-17 19:50:05 UTC
It was **a ton** of work and discussions in public and among the maintainers, in private. Anyway, if you have something which doesn't touch basic_streambuf, keeps the get and put areas of basic_filebuf completely separate, with seeks switching between reading and writing via state variables, then it's fine, in principle.
Comment 50 David Krauss 2010-09-17 19:56:57 UTC
(In reply to comment #49)
> with seeks
> switching between reading and writing via state variables, then it's fine, in
> principle.

Why require seeks? The whole point is that they are extraneous. I'm not proposing to break code that calls seekoff, just to make it properly redundant.

I'm experimenting with switching states on any operation that requires it. Overlapping get and put areas are obviously suicide; if that is what you attempted before, then I'm encouraged about remaining alternatives. Outside the context of the closed bug, I'm not proposing anything yet.
Comment 51 Paolo Carlini 2010-09-17 20:07:31 UTC
If you can allow writes after reads and viceversa *also* without seeks in the middle, and without affecting performance and without adding data members, that's fine. Let's see what you come up with it. By the way, get and put areas never overlapped in the past, just look at the code in SVN, the movement of the pointers was synced, true, exactly because one wanted each area to somehow "know" what the other area was doing in order to "more easily" switch between reads and writes without seeks. And nobody liked that scheme, was slow and buggy. That happened way before I started contributing, for the record. Then the new design came, outlined by Nathan, and simply inspired by C stdio, nothing strange. As a matter of fact, many users found it also quite easy to use, because - in case wasn't clear already from my previous comments - **nobody ever** complained.
Comment 52 Don Wakefield 2010-09-17 21:11:26 UTC
(In reply to comment #51)
>  ...As a matter of fact, many users found it also quite easy to
> use, because - in case wasn't clear already from my previous comments -
> **nobody ever** complained.

I just have to butt in here. I don't expect any sympathy, but the only reason my company never complained about the performance issue with filebuf::seekoff(0,cur) is that we only just started using compilers which included that change. Our company can't be the only one beholden to conservative customers, so I won't be surprised if you get other questions similar to ours, years after the change went in.

In any case, I appreciate all the effort you have put into this.
Comment 53 Paolo Carlini 2010-09-17 21:22:12 UTC
What can I say, I don't know anybody still using GCCs dating back to 2003. In any case, my point wasn't really about seek(0, cur) and its optimization, etc, my point was about the general design, where you use seeks to switch, you have get and put areas completely independent, etc. As far as I know, **nobody** asked to have the old behavior back, not in Bugzilla, not in the mailing lists, nowhere in public discussions. Nobody **ever** commented **anywhere** that using seeks to switch was unusual, entire Linux distros have been quickly recompiled to use the new filebuf, we are now able to perform series of consecutive get or put almost as fast as "C" getc_unlocked and putc_unlocked (see the performance testsuite), etc. To summarize, I have nothing in principle against speeding up this and that (of course) but I do not accept comments implying that the current design is just wrong and should be changed to something else without a careful analysis, benchmarks, a discussion on the mailing list with all the experts involved, etc.
Comment 54 David Krauss 2010-09-17 21:51:42 UTC
Well, for my part, a few years ago I played around with fstream a little, noticed the tellg requirement was weird (I only discovered it by throwing the kitchen sink at the problem), wasn't able to interpret the standard well enough to confirm that GCC was wrong, and decided never to use that part of fstream. It's not really a common use case anyway.

I think that a minority of people actually consider submitting bugs.

Companies are often very conservative with compilers and qualification, and are less motivated to report something the further behind they fall. Under what conditions does a manager actually decide to upgrade? If you're still using 2.95 and considering upgrading to 3.4, why bother reporting a bug against a compiler which is already obsolete?

> but I do not accept comments
> implying that the current design is just wrong and should be changed to
> something else without a careful analysis, benchmarks, a discussion on the
> mailing list with all the experts involved, etc.

Changes should never occur without analysis one way or another. It's easy to see that there is a bug… just look at the Standard's requirements on underflow. As for whether this bug is a symptom of a "wrong design," I don't think so. I'm getting the impression that you guys got tired after a long redesign process and oversimplified the state machine.

While fixing bugs in the overambitious design with simultaneous get and put areas (they might as well be overlapping as synchronized, both are impractical and not the intent of the Standard), the decision was made to cut back features, and non-explicit mode switches got lost.
Comment 55 Paolo Carlini 2010-09-17 21:59:05 UTC
>                                                                          I'm
> getting the impression that you guys got tired after a long redesign process
> and oversimplified the state machine.

Not me. What I remember is that Nathan Myers explained that C stdio, at least traditionally worked exactly like that, and since Nathan *designed* parts of the first C++ Standard itself, actively participated to all the meetings which led to C++98, I trusted him by and large and found the new design straightforward and well performing in most of out benchmarks. I still believe he was quite right. Anyway, when you post something to the mailing list, remember to add him in CC.
Comment 56 Paolo Carlini 2010-09-20 21:32:38 UTC
David himself is on it.
Comment 57 paolo@gcc.gnu.org 2010-09-22 19:41:11 UTC
Subject: Bug 45628

Author: paolo
Date: Wed Sep 22 19:40:43 2010
New Revision: 164529

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=164529
Log:
2010-09-22  David Krauss  <potswa@mac.com>

	PR libstdc++/45628
	* include/bits/fstream.tcc (basic_filebuf::underflow): Add state
	transition to avoid modality requiring seekoff(0,ios::cur).
	(basic_filebuf::pbackfail): Likewise.
	(basic_filebuf::overflow): Likewise.
	(basic_filebuf::_M_seek): Avoid minor unnecessary conversion.
	(basic_filebuf::seekoff): Remove code to _M_get_ext_pos; make
	(0, ios::cur) a special case preserving buffer contents.
	(basic_filebuf::_M_get_ext_pos): New function to obtain status
	about codecvt extern_t buffer for overflow and seekoff.
	* include/std/fstream (basic_filebuf::_M_get_ext_pos): Likewise.
	* config/abi/pre/gnu.ver: Export new symbols.
	* testsuite/27_io/basic_filebuf/seekoff/char/45628-1.cc: New,
	verifies that seekoff(0, ios::cur) preserves buffers.
	* testsuite/27_io/basic_filebuf/seekoff/char/45628-2.cc: Likewise.
	for codecvt case. More lenient as it may still flush put area.
	* testsuite/27_io/basic_filebuf/seekoff/char/4.cc: Modify to
	check that seekoff is not required between read and write.
	* testsuite/27_io/basic_filebuf/seekoff/wchar_t/4.cc: Likewise.
	* testsuite/27_io/basic_filebuf/sync/wchar_t/1.cc: Remove.
	* testsuite/27_io/basic_filebuf/sync/wchar_t/1.cc: Likewise.
	* testsuite/util/testsuite_character.h (codecvt::do_length): Comply
	with 22.2.1.5.2/10 "Returns ... the LARGEST value in the range..."

Added:
    trunk/libstdc++-v3/testsuite/27_io/basic_filebuf/seekoff/45628-2.cc
    trunk/libstdc++-v3/testsuite/27_io/basic_filebuf/seekoff/char/45628-1.cc
Removed:
    trunk/libstdc++-v3/testsuite/27_io/basic_filebuf/sync/char/1.cc
    trunk/libstdc++-v3/testsuite/27_io/basic_filebuf/sync/wchar_t/
Modified:
    trunk/libstdc++-v3/ChangeLog
    trunk/libstdc++-v3/config/abi/pre/gnu.ver
    trunk/libstdc++-v3/include/bits/fstream.tcc
    trunk/libstdc++-v3/include/std/fstream
    trunk/libstdc++-v3/testsuite/27_io/basic_filebuf/seekoff/char/4.cc
    trunk/libstdc++-v3/testsuite/27_io/basic_filebuf/seekoff/wchar_t/4.cc
    trunk/libstdc++-v3/testsuite/util/testsuite_character.h

Comment 58 Paolo Carlini 2010-09-25 11:09:20 UTC
Fixed for 4.6.0.