From 6316c939d5f7df7669fa3f12ae9f74821670ff0c Mon Sep 17 00:00:00 2001 From: "Hongli Lai (Phusion)" Date: Mon, 13 Sep 2010 22:55:36 +0200 Subject: [PATCH] Optimize Mongo::Connection#send_message_on_socket: don't slice the input when not necessary. --- lib/mongo/connection.rb | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/mongo/connection.rb b/lib/mongo/connection.rb index 3b6e859..941a2e1 100644 --- a/lib/mongo/connection.rb +++ b/lib/mongo/connection.rb @@ -845,11 +845,14 @@ module Mongo # @return [Integer] number of bytes sent def send_message_on_socket(packed_message, socket) begin - total_bytes_sent = 0 - while packed_message.size > 0 - byte_sent = socket.send(packed_message, 0) - total_bytes_sent += byte_sent - packed_message.slice!(0, byte_sent) + total_bytes_sent = socket.send(packed_message, 0) + if total_bytes_sent != packed_message.size + packed_message.slice!(0, total_bytes_sent) + while packed_message.size > 0 + byte_sent = socket.send(packed_message, 0) + total_bytes_sent += byte_sent + packed_message.slice!(0, byte_sent) + end end total_bytes_sent rescue => ex