Re:Goroutines, Nonblocking I/O, and Memory Usage
by kevinherron on Thursday January 05, 2017 @04:42PM
(#13331696)
I think you misunderstand. It's not that you can't get a pooled buffer in Go. The problem is that you don't know if/when you need that buffer without calling Read(), and the call to Read() requires a buffer that's ready to use.
[ Parent
| on HN ]
Re:Goroutines, Nonblocking I/O, and Memory Usage
by kornish on Thursday January 05, 2017 @04:45PM
(#13331714)
Yep, you're totally right. Reread a couple times and the crux of the matter seems to lie here: "Unlike with C, in Go there is no way to know if a connection is readable, other than to actually try to read data from it."
Re:Goroutines, Nonblocking I/O, and Memory Usage
by vendakka on Thursday January 05, 2017 @04:47PM
(#13331727)
Exactly. The sync.Pool will only be useful if net.Conn.Read accepted a Writer instead of []byte.
Re:Goroutines, Nonblocking I/O, and Memory Usage
by masklinn on Thursday January 05, 2017 @05:06PM
(#13331876)
That wouldn't make much sense since the Reader interface requires []byte, but the specific connection could inherit WriterTo: https://golang.org/pkg/io/#WriterTo
(TCPConn implements ReaderFrom, but not WriterTo)
Re:Goroutines, Nonblocking I/O, and Memory Usage
by vendakka on Thursday January 05, 2017 @05:09PM
(#13331898)
I wasn't very clear. A hypothetical conn.Read(w Writer) would call Write on w instead of filling a byte array. Write in turn allocates as needed. WriterTo is a cleaner way of doing this.
Re:Goroutines, Nonblocking I/O, and Memory Usage
by masklinn on Thursday January 05, 2017 @05:13PM
(#13331925)
> A hypothetical conn.Read(w Writer) would call Write on w instead of filling a byte array.
Yes I understand the purpose — hence suggesting WriterTo which is supposed to do that — I simply noted that conn.Read(w Writer) would make Conn not extend io.Reader anymore since it takes a buffer.