Optimize Ruby 1.8 implementation of BSON::OrderedHash.

The set in there is unnecessary. Use the hash itself as "set". This makes
BSON::OrderedHash 148% faster on Ruby 1.8.
This commit is contained in:
Hongli Lai (Phusion) 2010-09-11 13:39:13 +02:00 committed by Kyle Banker
parent 117ce2389c
commit 026e409d6d
1 changed files with 1 additions and 8 deletions

View File

@ -20,7 +20,6 @@
#
# Under Ruby 1.9 and greater, this class has no added methods because Ruby's
# Hash already keeps its keys ordered by order of insertion.
require 'set'
module BSON
class OrderedHash < Hash
@ -60,7 +59,6 @@ module BSON
def initialize(*a, &b)
super
@ordered_keys = []
@ordered_set = Set.new
end
def keys
@ -69,10 +67,8 @@ module BSON
def []=(key, value)
@ordered_keys ||= []
@ordered_set ||= Set.new
unless @ordered_set.member?(key)
unless has_key?(key)
@ordered_keys << key
@ordered_set.add(key)
end
super(key, value)
end
@ -103,7 +99,6 @@ module BSON
@ordered_keys ||= []
@ordered_keys += other.keys # unordered if not an BSON::OrderedHash
@ordered_keys.uniq!
@ordered_set = Set.new(@ordered_keys)
super(other)
end
@ -117,7 +112,6 @@ module BSON
def delete(key, &block)
@ordered_keys.delete(key) if @ordered_keys
@ordered_set.delete(key) if @ordered_set
super
end
@ -137,7 +131,6 @@ module BSON
def clear
super
@ordered_set.clear
@ordered_keys = []
end