mirror of
https://github.com/meineerde/rackstash.git
synced 2026-02-01 01:37:12 +00:00
Avoid Array instances when JSON'ifying Arrays / Hashes
Similar to the previous commit, we avoid the unnecessary allocation of a temporary array when looping over the `@raw` value. In addition to that, we ensure thread-safety by always operating on a copy of the raw data so that other threads can not change anything under our feet anymore.
This commit is contained in:
parent
da3113e880
commit
1a73062758
@ -89,7 +89,7 @@ module Rackstash
|
||||
|
||||
# @return [::Array] deep-transforms the array into a plain Ruby Array
|
||||
def as_json(*)
|
||||
@raw.map { |value|
|
||||
@raw.to_a.map! { |value|
|
||||
value.is_a?(AbstractCollection) ? value.as_json : value
|
||||
}
|
||||
end
|
||||
|
||||
@ -54,9 +54,9 @@ module Rackstash
|
||||
|
||||
# @return [::Hash] deep-transforms the hash into a plain Ruby Hash
|
||||
def as_json(*)
|
||||
@raw.each_with_object({}) do |(key, value), memo|
|
||||
value = value.as_json if value.is_a?(AbstractCollection)
|
||||
memo[key] = value
|
||||
hash = @raw.to_h
|
||||
hash.each_pair do |key, value|
|
||||
hash[key] = value.as_json if value.is_a?(AbstractCollection)
|
||||
end
|
||||
end
|
||||
alias to_hash as_json
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user