From c6206eddf46600cb383c695a23c540c063b5d846 Mon Sep 17 00:00:00 2001 From: "Hongli Lai (Phusion)" Date: Sat, 11 Sep 2010 21:29:57 +0200 Subject: [PATCH] Optimize MongoDB::Connection#receive_response_header by using raw string operations and unpack() instead of the slower ByteBuffer. --- lib/mongo/connection.rb | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/lib/mongo/connection.rb b/lib/mongo/connection.rb index 0c7a4e3..1610975 100644 --- a/lib/mongo/connection.rb +++ b/lib/mongo/connection.rb @@ -768,17 +768,14 @@ module Mongo end def receive_response_header(sock) - header_buf = BSON::ByteBuffer.new - header_buf.put_array(receive_message_on_socket(RESPONSE_HEADER_SIZE, sock).unpack("C*")) + header_buf = receive_message_on_socket(RESPONSE_HEADER_SIZE, sock) if header_buf.length != RESPONSE_HEADER_SIZE raise "Short read for DB response header; " + "expected #{RESPONSE_HEADER_SIZE} bytes, saw #{header_buf.length}" end - header_buf.rewind - check_response_flags(header_buf.get_int) - cursor_id = header_buf.get_long - starting_from = header_buf.get_int - number_remaining = header_buf.get_int + flags, cursor_id_a, cursor_id_b, starting_from, number_remaining = header_buf.unpack('VVVVV') + check_response_flags(flags) + cursor_id = (cursor_id_b << 32) + cursor_id_a [number_remaining, cursor_id] end