Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

>10x speed increase for large, binary BSON messages using the silly p…

…ure-ruby code
  • Loading branch information...
commit 3d4514f56abf303a406ab5aa229de4e4700c4225 1 parent ee49948
@karlin authored
Showing with 23 additions and 12 deletions.
  1. +8 −12 lib/bson/bson_ruby.rb
  2. +15 −0 lib/bson/byte_buffer.rb
View
20 lib/bson/bson_ruby.rb
@@ -205,10 +205,12 @@ def serialize_key_value(k, v, check_keys)
def deserialize(buf=nil)
# If buf is nil, use @buf, assumed to contain already-serialized BSON.
# This is only true during testing.
- if buf.is_a? String
- @buf = ByteBuffer.new(buf.unpack("C*")) if buf
- else
- @buf = ByteBuffer.new(buf.to_a) if buf
+ if buf
+ if buf.is_a? String
+ @buf = ByteBuffer.new(buf)
+ else
+ @buf = ByteBuffer.new(buf.to_a)
+ end
end
@buf.rewind
@buf.get_int # eat message size
@@ -406,7 +408,7 @@ def deserialize_binary_data(buf)
len = buf.get_int
type = buf.get
len = buf.get_int if type == Binary::SUBTYPE_BYTES
- Binary.new(buf.get(len), type)
+ Binary.new(buf.binary_get(len), type)
end
def serialize_eoo_element(buf)
@@ -577,13 +579,7 @@ def serialize_code_w_scope(buf, key, val)
end
def deserialize_cstr(buf)
- chars = ""
- while true
- b = buf.get
- break if b == 0
- chars << b.chr
- end
- encoded_str(chars)
+ encoded_str(buf.cstr_get)
end
def bson_type(o)
View
15 lib/bson/byte_buffer.rb
@@ -175,6 +175,21 @@ def put_double(d, offset=nil)
put_array(a, offset)
end
+ # dangerous lack of boundary checking, wheee!
+ def cstr_get
+ next_zero = @str.index("\x00", @cursor)
+ start = @cursor
+ @cursor = next_zero + 1
+ @str[start...next_zero]
+ end
+
+ def binary_get(len)
+ check_read_length(len)
+ start = @cursor
+ @cursor += len
+ @str[start, len] # don't unpack
+ end
+
# If +size+ == nil, returns one byte. Else returns array of bytes of length
# # +size+.
if "x"[0].is_a?(Integer)
Please sign in to comment.
Something went wrong with that request. Please try again.