diff options
author | José Valim <jose.valim@gmail.com> | 2011-05-24 12:59:06 -0700 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2011-05-24 12:59:06 -0700 |
commit | b3f51e3b1c4f9707a456ea51ca5998606383e86d (patch) | |
tree | c95ecf7b65c6e84a08fc573a8765893464b79c12 /activesupport/lib/active_support/json/encoding.rb | |
parent | 806d6009f3e931adb44460c0bae05fbba2d3c4cf (diff) | |
parent | 2213479a12533e0f980843e1db37f0fd3dc68d03 (diff) | |
download | rails-b3f51e3b1c4f9707a456ea51ca5998606383e86d.tar.gz rails-b3f51e3b1c4f9707a456ea51ca5998606383e86d.tar.bz2 rails-b3f51e3b1c4f9707a456ea51ca5998606383e86d.zip |
Merge pull request #1239 from alindeman/master
Use set data structure to speed up circular reference checks on large/deeply nested objects
Diffstat (limited to 'activesupport/lib/active_support/json/encoding.rb')
-rw-r--r-- | activesupport/lib/active_support/json/encoding.rb | 8 |
1 files changed, 4 insertions, 4 deletions
diff --git a/activesupport/lib/active_support/json/encoding.rb b/activesupport/lib/active_support/json/encoding.rb index d22fe14b33..c2c45e9f9d 100644 --- a/activesupport/lib/active_support/json/encoding.rb +++ b/activesupport/lib/active_support/json/encoding.rb @@ -14,6 +14,7 @@ require 'time' require 'active_support/core_ext/time/conversions' require 'active_support/core_ext/date_time/conversions' require 'active_support/core_ext/date/conversions' +require 'set' module ActiveSupport class << self @@ -39,7 +40,7 @@ module ActiveSupport def initialize(options = nil) @options = options - @seen = [] + @seen = Set.new end def encode(value, use_options = true) @@ -71,13 +72,12 @@ module ActiveSupport private def check_for_circular_references(value) - if @seen.any? { |object| object.equal?(value) } + unless @seen.add?(value.__id__) raise CircularReferenceError, 'object references itself' end - @seen.unshift value yield ensure - @seen.shift + @seen.delete(value.__id__) end end |