Optimize MongoDB::Connection#receive_response_header by using raw string operations and unpack() instead of the slower ByteBuffer.

This commit is contained in:
Hongli Lai (Phusion) 2010-09-11 21:29:57 +02:00 committed by Kyle Banker
parent e1bf168767
commit c6206eddf4
1 changed files with 4 additions and 7 deletions

View File

@ -768,17 +768,14 @@ module Mongo
end end
def receive_response_header(sock) def receive_response_header(sock)
header_buf = BSON::ByteBuffer.new header_buf = receive_message_on_socket(RESPONSE_HEADER_SIZE, sock)
header_buf.put_array(receive_message_on_socket(RESPONSE_HEADER_SIZE, sock).unpack("C*"))
if header_buf.length != RESPONSE_HEADER_SIZE if header_buf.length != RESPONSE_HEADER_SIZE
raise "Short read for DB response header; " + raise "Short read for DB response header; " +
"expected #{RESPONSE_HEADER_SIZE} bytes, saw #{header_buf.length}" "expected #{RESPONSE_HEADER_SIZE} bytes, saw #{header_buf.length}"
end end
header_buf.rewind flags, cursor_id_a, cursor_id_b, starting_from, number_remaining = header_buf.unpack('VVVVV')
check_response_flags(header_buf.get_int) check_response_flags(flags)
cursor_id = header_buf.get_long cursor_id = (cursor_id_b << 32) + cursor_id_a
starting_from = header_buf.get_int
number_remaining = header_buf.get_int
[number_remaining, cursor_id] [number_remaining, cursor_id]
end end