Optimize Mongo::Connection#receive_message_on_socket and #receive_and_discard_message_on_socket: avoid creating initial input buffer up front so that IO#read only needs to allocate the buffer once.

This commit is contained in:
Hongli Lai (Phusion) 2010-09-13 22:56:37 +02:00 committed by Kyle Banker
parent 6316c939d5
commit b474b29d35
1 changed files with 2 additions and 4 deletions

View File

@ -864,9 +864,8 @@ module Mongo
# Low-level method for receiving data from socket.
# Requires length and an available socket.
def receive_message_on_socket(length, socket)
message = new_binary_string
begin
socket.read(length, message)
message = socket.read(length)
raise ConnectionFailure, "connection closed" unless message.length > 0
if message.length < length
chunk = new_binary_string
@ -887,10 +886,9 @@ module Mongo
# Unlike #receive_message_on_socket, this method immediately discards the data
# and only returns the number of bytes read.
def receive_and_discard_message_on_socket(length, socket)
chunk = new_binary_string
bytes_read = 0
begin
socket.read(length, chunk)
chunk = socket.read(length)
bytes_read = chunk.length
raise ConnectionFailure, "connection closed" unless bytes_read > 0
if bytes_read < length