Closed GoogleCodeExporter closed 9 years ago
Interesting... my expectation would be that a Writer implementation, given a
single large String, should be able to handle it as efficiently as possible
since it has all the cards so to speak. I imagine BufferedWriter to be more
useful for collecting many small writes.
But it looks like regardless of how large a chunk you provide the characters to
the Writer in, an OutputStreamWriter will only write out 8192 bytes at a time,
so copying the large String's char array is basically just a big waste of time.
It's actually really surprising to me that StreamEncoder doesn't handle this
differently... it could just wrap the String in a CharBuffer and use that with
CharsetEncoder without needing to copy the char array at all. Maybe there's
some good reason for that I'm not aware of.
Anyway, this does look like a good potential solution.
> A similar tweak may be applicable to ByteSink? (Not tried.)
This seems less likely since byte arrays don't need to go through encoding but
can just be passed directly to a native method to be written.
Original comment by cgdecker@google.com
on 23 Sep 2014 at 4:02
> an OutputStreamWriter will only write out 8192 bytes at a time (...)
Huh, so it does. I missed that.
> byte arrays don't need to go through encoding but can just be passed directly
to a native method to be written
Indeed. Sorry I should have taken a few minutes to check that.
> It's actually really surprising to me that StreamEncoder doesn't handle this
differently (...) Maybe there's some good reason for that I'm not aware of.
I agree. Possibly because StreamEncoder is itself a Writer, and the JDK Writers
seem to follow this (undocumented?) pattern:
1. call write(char[], int, int) to do the real work
2. only call it once
(1) makes some sense when there's lots of overridden and overridable methods in
a deep hierarchy. (But maybe it hints of yearning for a better design?)
(2) could be concern about per-call-costs in derived classes? (multiply-wrapped
Writers, synchronisation, class CarrierPidgeonIpWriter {handy in the days of
NSA snooping} etc.) Or just trying to be more friendly to overridden classes?
Anyway, I'd still rate performance as a higher concern for StreamEncoder than
these other things... but no point crying over Java's spilt milk, I guess. It
ain't going to help ;-)
Original comment by luke.ush...@gmail.com
on 23 Sep 2014 at 7:35
This issue has been migrated to GitHub.
It can be found at https://github.com/google/guava/issues/<issue id>
Original comment by cgdecker@google.com
on 1 Nov 2014 at 4:08
Original comment by cgdecker@google.com
on 1 Nov 2014 at 4:17
Original comment by cgdecker@google.com
on 3 Nov 2014 at 9:07
Original issue reported on code.google.com by
luke.ush...@gmail.com
on 23 Sep 2014 at 1:37