Skip to content

Commit

Permalink
perf: remove flush from ser::into_writer.
Browse files Browse the repository at this point in the history
When serializing many records into a writer, it is advantageous to
batch the writes when persisting to a disk or transmitting them over a
socket. The current implementation of `into_writer()` flushes the
encoder after every call which, in turn, flushes the underlying
writer.

This commit removes the flush from the call to `into_writer()`
allowing the programmer to take advantage of tools like
`std::io::BufWriter`.

I was concerned that the encoder could maintain some state that would
be lost if it was dropped before a flush, however, I do not believe
this to be the case because the `Encoder` type is a simple wrapper
around a type implementing the `Write` trait, and the invocation of
`flush()` is on that inner type rather than on anything in the encoder
itself. Therefore call to flush cannot affect the `Encoder` in any
way.

Signed-off-by: John VanEnk <[email protected]>
  • Loading branch information
sw17ch authored and enarxbot committed Oct 6, 2022
1 parent 234dea6 commit 109c371
Showing 1 changed file with 1 addition and 2 deletions.
3 changes: 1 addition & 2 deletions ciborium/src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,5 @@ where
W::Error: core::fmt::Debug,
{
let mut encoder = Serializer::from(writer);
value.serialize(&mut encoder)?;
Ok(encoder.0.flush()?)
value.serialize(&mut encoder)
}

0 comments on commit 109c371

Please sign in to comment.