-
Notifications
You must be signed in to change notification settings - Fork 389
Flush CodedOutputStream buffer on drop. #147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Well, it is intentional. I think This behavior however contradics behavior of Need to think about it. |
The problem with current You propose to panic instead of ignore, which is also not good. |
Hmm, I see. It seems there's no perfect solution -- as long as something is buffered, there's potential for a delayed error. We either have to drop bytes, drop the error, or panic. So I understand your reasoning. I'm fine if you decide to keep things as they are, but one more proposal just came to mind: It seems to me, in one way of thinking, that dropping without flushing is "indeterminate" in the same way that a race condition or uninitialized memory are: no correct program should do that, and you'll get something but you don't know what. It might be better to demand that the user either explicitly flushes or explicitly clears the buffer before dropping, and panic if not. That is:
The difference is that this panic would be due to incorrect use of API (i.e., always occuring), rather than only occurring on some infrequent error, so at least it would be less error-prone. What do you think? I'm happy to drop this and write my own RAII wrapper if you disagree. Thanks for considering :-) |
I like your solution, except it has a problem:
To solve the problem, I think the issue should be raised in rust about |
Found it: panicking. So, yes, your solution should work. I'll open an issue in Rust. |
The issue about |
The rust-lang issue is still open, but I had a thought in the meantime: why not have
|
Several reasons:
I think we can't use I'm not sure I understand what do you propose right now. My point is that |
OK, thanks. I was trying to figure out if we could make forward progress here by simply delegating to |
This commit removes the internal buffering in CodedOutputStream. The protobuf library should rely on the application to provide a buffered Write implementation, if it so desires. By buffering internally, the protobuf library forces double buffering in situations where the application is already buffering. Additionally, it forces awkward resource lifetime issues as discussed in stepancheg#147. Removing the internal buffering simplifies the internals of CodedOutputStream and improves performance on the rust-protobuf-perftest write tests. Before: test1: write: 86 ns per iter test_repeated_bool: write: 91 ns per iter test_repeated_packed_int32: write: 127 ns per iter test_repeated_messages: write: 224 ns per iter test_optional_messages: write: 202 ns per iter test_strings: write: 136 ns per iter test_small_bytearrays: write: 1098 ns per iter test_large_bytearrays: write: 119221 ns per iter After: test1: write: 55 ns per iter test_repeated_bool: write: 66 ns per iter test_repeated_packed_int32: write: 122 ns per iter test_repeated_messages: write: 248 ns per iter test_optional_messages: write: 207 ns per iter test_strings: write: 118 ns per iter test_small_bytearrays: write: 1064 ns per iter test_large_bytearrays: write: 109567 ns per iter
This is merged into master long time ago, but it is not in stable though. |
rm generated files
The current behavior -- that some leftover bytes may be silently forgotten in a
CodedOutputStream
's output buffer on drop -- caught me by surprise.I'm not sure if the current behavior is intentional. Perhaps you wanted to be able to explicitly return a
Result
onflush()
, whereasdrop()
cannot do that? But in that case the user who wants to catch the error can now explicitlyflush()
first to avoid any panic on drop, and they should be doing that anyway to avoid silently forgetting bytes. In any case, the new semantics seem less error-prone to me.Thanks!
This change is