From: Aaron Patterson Date: 2013-07-10T09:52:58+09:00 Subject: [ruby-core:55892] Re: [ruby-trunk - Feature #5138] Add nonblocking IO that does not use exceptions for EOF and EWOULDBLOCK On Wed, Jul 10, 2013 at 09:03:19AM +0900, Eric Wong wrote: > "tenderlovemaking (Aaron Patterson)" wrote: > > As for Erlang style return values. It seems interesting, but that > > means every call to `try_read_nonblock` would allocate an array. The > > only possible return values would be: > > I prefer to avoid the requirement for allocating _anything_ in the > common case. So "no" to the tuple retvals. > > How about making the destination buffer a required argument, Does this mean someone would have to pre-allocate the buffer? For example, the "buffer" variable below would need to be defined as: buffer = " " * maxlen > and returning: > > Integer - length in bytes on successful read > Symbol - :wait_readable/:wait_writable (common) > nil - EOF Is this really advantageous over: String - the buffer read in Symbol - :wait_readable/:wait_writable (common) nil - EOF Length of bytes can be derived from buffer length. > > case ret = io.try_read(maxlen, buffer) > when Integer > process_buffer(buffer, ret) > when nil > break > when Symbol > io.__send__(ret, timeout_sec) # :wait_readable,:wait_writable > end while true > > I think making the buffer a required argument makes sense anyways for > performance (not just GC, but keeping the CPU cache hot regardless of > VM/language, too). -- Aaron Patterson http://tenderlovemaking.com/