1
0
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:
Holger Just 2017-06-16 23:01:23 +02:00
parent da3113e880
commit 1a73062758
2 changed files with 4 additions and 4 deletions

View File

@ -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

View File

@ -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