Skip to content

Commit

Permalink
RUBY-1019: Document strings don't need conversion on deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Sep 1, 2015
1 parent 20d5875 commit 90ccf82
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/bson/document.rb
Expand Up @@ -79,7 +79,7 @@ def []=(key, value)
# @since 3.0.0
def initialize(elements = nil)
super()
(elements || {}).each_pair{ |key, value| self[key] = value }
elements.each{ |key, value| self[key] = value } if elements
end

# Merge this document with another document, returning a new document in
Expand Down
4 changes: 2 additions & 2 deletions lib/bson/hash.rb
Expand Up @@ -72,11 +72,11 @@ module ClassMethods
#
# @since 2.0.0
def from_bson(bson)
hash = new
hash = allocate
bson.read(4) # Swallow the first four bytes.
while (type = bson.readbyte.chr) != NULL_BYTE
field = bson.gets(NULL_BYTE).from_bson_string.chop!
hash[field] = BSON::Registry.get(type).from_bson(bson)
hash.store(field, BSON::Registry.get(type).from_bson(bson))
end
hash
end
Expand Down
10 changes: 10 additions & 0 deletions perf/bench.rb
Expand Up @@ -145,5 +145,15 @@ def benchmark!
bench.report("Time#from_bson -------->") do
count.times { Time.from_bson(StringIO.new(time_bytes)) }
end

doc = BSON::Document.new(
_id: 1, name: "embedded-485", uat: Time.now, tp: "embedded", ct: 485
)
refs = 100.times.map { |i| { _id: i, n: "embed-#{i}", v: i }}
doc[:refs] = refs
doc_bytes = doc.to_bson
bench.report("Document#from_bson ---->") do
10_000.times { BSON::Document.from_bson(StringIO.new(doc_bytes)) }
end
end
end
8 changes: 8 additions & 0 deletions perf/test.rb
@@ -0,0 +1,8 @@
class MyClass

def initialize
10000000.times { |i| p i }
end
end

MyClass.new

0 comments on commit 90ccf82

Please sign in to comment.