10000 Avoid rbuf copying on the first read by marshall-lee · Pull Request #15 · ruby/net-protocol · GitHub
[go: up one dir, main page]

Skip to content

Avoid rbuf copying on the first read #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions lib/net/protocol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,15 @@ def close

public

def read(len, dest = ''.b, ignore_eof = false)
def read(len, dest = nil, ignore_eof = false)
LOG "reading #{len} bytes..."
read_bytes = 0
begin
if dest.nil?
dest = rbuf_consume0(len)
read_bytes = dest.size
else
read_bytes = 0
end
while read_bytes + @rbuf.size < len
s = rbuf_consume(@rbuf.size)
read_bytes += s.size
Expand All @@ -170,10 +175,15 @@ def read(len, dest = ''.b, ignore_eof = false)
dest
end

def read_all(dest = ''.b)
def read_all(dest = nil)
LOG 'reading all...'
read_bytes = 0
begin
if dest.nil?
dest = rbuf_consume0(BUFSIZE)
read_bytes = dest.size
else
read_bytes = 0
end
while true
s = rbuf_consume(@rbuf.size)
read_bytes += s.size
Expand Down Expand Up @@ -229,7 +239,7 @@ def rbuf_fill
end

def rbuf_consume(len)
if len == @rbuf.size
if len >= @rbuf.size
s = @rbuf
@rbuf = ''.b
else
Expand All @@ -239,6 +249,11 @@ def rbuf_consume(len)
s
end

def rbuf_consume0(len)
rbuf_fill if @rbuf.size == 0
rbuf_consume(len)
end

#
# Write
#
Expand Down
0