https://codereview.appspot.com/6632046/diff/1002/src/pkg/bytes/reader.go File src/pkg/bytes/reader.go (right): https://codereview.appspot.com/6632046/diff/1002/src/pkg/bytes/reader.go#newcode131 src/pkg/bytes/reader.go:131: m, e := w.Write(b) I can see the advantage ...
12 years, 5 months ago
(2012-10-09 04:46:53 UTC)
#2
On Tue, Oct 9, 2012 at 11:27 AM, <chickencha@gmail.com> wrote: > > https://codereview.appspot.**com/6632046/diff/7002/src/pkg/** > bytes/reader.go<https://codereview.appspot.com/6632046/diff/7002/src/pkg/bytes/reader.go> ...
12 years, 5 months ago
(2012-10-09 18:31:34 UTC)
#6
On Tue, Oct 9, 2012 at 11:27 AM, <chickencha@gmail.com> wrote:
>
> https://codereview.appspot.**com/6632046/diff/7002/src/pkg/**
>
bytes/reader.go<https://codereview.appspot.com/6632046/diff/7002/src/pkg/bytes/reader.go>
> File src/pkg/bytes/reader.go (right):
>
> https://codereview.appspot.**com/6632046/diff/7002/src/pkg/**
>
bytes/reader.go#newcode133<https://codereview.appspot.com/6632046/diff/7002/src/pkg/bytes/reader.go#newcode133>
> src/pkg/bytes/reader.go:133: panic("bytes.Reader.WriteTo: invalid Write
> count")
> On 2012/10/09 18:23:57, bradfitz wrote:
>
>> I'm not sure this is panic-worthy. Why not just return an error here?
>>
>
> I agree, but I was following what happens in bytes.Buffer. Want to
> change that one too?
>
Let's wait for others' opinions.
http://codereview.appspot.com/6632046/diff/7002/src/pkg/strings/reader.go File src/pkg/strings/reader.go (right): http://codereview.appspot.com/6632046/diff/7002/src/pkg/strings/reader.go#newcode130 src/pkg/strings/reader.go:130: m, e := io.WriteString(w, s) if the writer has ...
12 years, 5 months ago
(2012-10-09 21:58:45 UTC)
#7
https://codereview.appspot.com/6632046/diff/7002/src/pkg/strings/reader.go File src/pkg/strings/reader.go (right): https://codereview.appspot.com/6632046/diff/7002/src/pkg/strings/reader.go#newcode130 src/pkg/strings/reader.go:130: m, e := io.WriteString(w, s) On 2012/10/09 21:58:46, remyoudompheng ...
12 years, 5 months ago
(2012-10-09 22:04:47 UTC)
#8
https://codereview.appspot.com/6632046/diff/7002/src/pkg/strings/reader.go
File src/pkg/strings/reader.go (right):
https://codereview.appspot.com/6632046/diff/7002/src/pkg/strings/reader.go#ne...
src/pkg/strings/reader.go:130: m, e := io.WriteString(w, s)
On 2012/10/09 21:58:46, remyoudompheng wrote:
> if the writer has no WriteString() method it will allocate a large bytestring
> and copy the whole string contents to it, whereas io.Copy will allocate a
fixed
> size buffer and reuse it, hence creating less garbage if the underlying string
> is larg, but probably more garbage in most cases (32kB).
>
> What do you think?
If we were to do such an optimization, why here instead of just fixing
io.WriteString to do that if/when it makes sense? Since WriteString and Copy
are in the same package, that 32kB constant can remain package-private.
But--- we don't know whether it's cheaper to to make a copy of a 5MB
string->[]byte or do a Write call. What if the destination io.Writer is doing
slow network round-trips and each Write is 350 ms. In that case it might be
better to just give it one large []byte and let it handle it.
http://codereview.appspot.com/6632046/diff/7002/src/pkg/strings/reader.go File src/pkg/strings/reader.go (right): http://codereview.appspot.com/6632046/diff/7002/src/pkg/strings/reader.go#newcode130 src/pkg/strings/reader.go:130: m, e := io.WriteString(w, s) I lean towards leaving ...
12 years, 5 months ago
(2012-10-10 02:39:21 UTC)
#9
http://codereview.appspot.com/6632046/diff/7002/src/pkg/strings/reader.go
File src/pkg/strings/reader.go (right):
http://codereview.appspot.com/6632046/diff/7002/src/pkg/strings/reader.go#new...
src/pkg/strings/reader.go:130: m, e := io.WriteString(w, s)
I lean towards leaving io.WriteString alone at least for now. Strings can be
big but usually aren't, and it's nice to generate one syscall. Plus if you
decide to break it up you need heuristics about what size is correct. Plus most
of the guys where it really matters implement WriteString anyway.
But I'm not adamant and could be swayed.
But I agree that calling out to WriteString feels wrong here. However I think
Copy won't do it because it'll come right back to Reader.WriteTo, won't it?
I think WriteString is fine. Safer, less surprising default. And not changing behavior. Thoughts though ...
12 years, 5 months ago
(2012-10-10 02:58:28 UTC)
#11
I think WriteString is fine. Safer, less surprising default. And not
changing behavior.
Thoughts though on panic versus error?
On Oct 9, 2012 7:40 PM, "Rob Pike" <r@golang.org> wrote:
> I guess that wasn't very helpful. Apologies for that.
>
> -rob
>
http://codereview.appspot.com/6632046/diff/7002/src/pkg/strings/reader.go File src/pkg/strings/reader.go (right): http://codereview.appspot.com/6632046/diff/7002/src/pkg/strings/reader.go#newcode130 src/pkg/strings/reader.go:130: m, e := io.WriteString(w, s) On 2012/10/10 02:39:21, r ...
12 years, 5 months ago
(2012-10-10 03:00:18 UTC)
#12
http://codereview.appspot.com/6632046/diff/7002/src/pkg/strings/reader.go
File src/pkg/strings/reader.go (right):
http://codereview.appspot.com/6632046/diff/7002/src/pkg/strings/reader.go#new...
src/pkg/strings/reader.go:130: m, e := io.WriteString(w, s)
On 2012/10/10 02:39:21, r wrote:
> But I agree that calling out to WriteString feels wrong here. However I think
> Copy won't do it because it'll come right back to Reader.WriteTo, won't it?
One way or another I think it's important to call the Writer's WriteString
method when available to avoid allocating.
It's possible to call back into io.Copy without recursing if we wrap the Reader
in a struct that only has a Read method. net does something similar with its
unexported writerOnly type.
LGTM with some minor grumbling about unnecessary named return values. https://codereview.appspot.com/6632046/diff/9007/src/pkg/bytes/reader.go File src/pkg/bytes/reader.go (right): https://codereview.appspot.com/6632046/diff/9007/src/pkg/bytes/reader.go#newcode125 ...
12 years, 5 months ago
(2012-10-10 23:51:52 UTC)
#17
Issue 6632046: code review 6632046: bytes, strings: add (*Reader).WriteTo
(Closed)
Created 12 years, 5 months ago by eds
Modified 12 years, 5 months ago
Reviewers:
Base URL:
Comments: 19