Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #1239 from alindeman/master

Use set data structure to speed up circular reference checks on large/deeply nested objects
  • Loading branch information...
commit b3f51e3b1c4f9707a456ea51ca5998606383e86d 2 parents 806d600 + 2213479
@josevalim josevalim authored
Showing with 4 additions and 4 deletions.
  1. +4 −4 activesupport/lib/active_support/json/encoding.rb
View
8 activesupport/lib/active_support/json/encoding.rb
@@ -14,6 +14,7 @@
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 @@ class Encoder
def initialize(options = nil)
@options = options
- @seen = []
+ @seen = Set.new
end
def encode(value, use_options = true)
@@ -71,13 +72,12 @@ def escape(string)
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
Please sign in to comment.
Something went wrong with that request. Please try again.