The Ruby driver was quite inefficient with handling data. Strings (read from the network or passed by the user) were being unpacked into arrays all over the place and vice versa. We’ve modified the driver to work with strings instead of byte arrays as much as possible. Most notably: ByteBuffer has been rewritten to use a binary string as underlying storage object instead of an array.
The Ruby 1.8 implementation of BSON::OrderedHash was inefficient: it uses a Set even though it’s not necessary. We removed the dependency on Set and greatly improved OrderedHash’s 1.8 performance.
The end result is a driver that’s 274% faster on Ruby 1.8 and 204% faster on Ruby 1.9. We used the following benchmark:
requests = Mongo::Connection.new.db('foobar').collection('requests')
query = { :_id => "6d61bbc7e32795e7ace8b98e8e83961cb8e3ee53" }
40000.times do
requests.find(query, :limit => -1) do |cursor|
cursor.next_document
end
endOriginal runtime: 74.0s (Ruby 1. / 33.9s (Ruby 1.9)
New runtime: 25.8s (Ruby 1. / 16.7s (Ruby 1.9)