1
0
mirror of https://github.com/meineerde/rackstash.git synced 2025-12-19 15:01:12 +00:00

Don't use Concurrent::{Hash|Array} objects in Fields

Since the fields are (through the thread-local BufferStack) only ever
accessed from a single Thread, there is no need to accept the additional
locking overhead of the Concurrent raw values.

We can just use simple Hashes and Arrays here for higher performance.
This commit is contained in:
Holger Just 2017-08-03 21:09:45 +02:00
parent 65e272347d
commit 697641635b
5 changed files with 12 additions and 15 deletions

View File

@ -115,7 +115,7 @@ module Rackstash
when Rackstash::Fields::Hash, Rackstash::Fields::Array when Rackstash::Fields::Hash, Rackstash::Fields::Array
return wrap ? value : value.raw return wrap ? value : value.raw
when ::Hash when ::Hash
hash = Concurrent::Hash.new hash = {}
value.each_pair do |k, v| value.each_pair do |k, v|
hash[utf8_encode(k)] = normalize(v, scope: scope) hash[utf8_encode(k)] = normalize(v, scope: scope)
end end
@ -126,10 +126,7 @@ module Rackstash
end end
return hash return hash
when ::Array, ::Set, ::Enumerator when ::Array, ::Set, ::Enumerator
array = Concurrent::Array.new array = value.map { |e| normalize(e, scope: scope) }
value.each do |e|
array << normalize(e, scope: scope)
end
if wrap if wrap
array = Rackstash::Fields::Array.new.tap do |array_field| array = Rackstash::Fields::Array.new.tap do |array_field|
array_field.raw = array array_field.raw = array

View File

@ -11,7 +11,7 @@ module Rackstash
module Fields module Fields
class Array < AbstractCollection class Array < AbstractCollection
def initialize def initialize
@raw = Concurrent::Array.new @raw = []
end end
# @!method +(array) # @!method +(array)
@ -93,7 +93,7 @@ module Rackstash
# start index is out of range # start index is out of range
def [](index, length = nil) def [](index, length = nil)
result = length.nil? ? @raw[index] : @raw[index, length] result = length.nil? ? @raw[index] : @raw[index, length]
result = new(result) if ::Concurrent::Array === result result = new(result) if ::Array === result
result result
end end
alias slice [] alias slice []
@ -167,7 +167,7 @@ module Rackstash
# @return [::Array] deep-transforms the array into a plain Ruby Array # @return [::Array] deep-transforms the array into a plain Ruby Array
def as_json(*) def as_json(*)
@raw.to_a.map! { |value| @raw.map { |value|
value.is_a?(AbstractCollection) ? value.as_json : value value.is_a?(AbstractCollection) ? value.as_json : value
} }
end end

View File

@ -17,7 +17,7 @@ module Rackstash
# @param forbidden_keys [Set<String>,::Array<String>] a list of strings # @param forbidden_keys [Set<String>,::Array<String>] a list of strings
# which are not allowed to be used as keys in this hash # which are not allowed to be used as keys in this hash
def initialize(forbidden_keys: EMPTY_SET) def initialize(forbidden_keys: EMPTY_SET)
@raw = Concurrent::Hash.new @raw = {}
unless forbidden_keys.is_a?(Set) && unless forbidden_keys.is_a?(Set) &&
forbidden_keys.frozen? && forbidden_keys.frozen? &&
@ -62,7 +62,7 @@ module Rackstash
# @return [::Hash] deep-transforms the hash into a plain Ruby Hash # @return [::Hash] deep-transforms the hash into a plain Ruby Hash
def as_json(*) def as_json(*)
hash = @raw.to_h hash = @raw.dup
hash.each_pair do |key, value| hash.each_pair do |key, value|
hash[key] = value.as_json if value.is_a?(AbstractCollection) hash[key] = value.as_json if value.is_a?(AbstractCollection)
end end

View File

@ -13,7 +13,7 @@ module Rackstash
module Fields module Fields
class Tags < AbstractCollection class Tags < AbstractCollection
def initialize def initialize
@raw = Concurrent::Hash.new @raw = {}
end end
def <<(tag) def <<(tag)

View File

@ -198,9 +198,9 @@ describe Rackstash::Fields::AbstractCollection do
expect(normalize(hash, wrap: false).keys).to all be_frozen expect(normalize(hash, wrap: false).keys).to all be_frozen
end end
it 'returns a Concurrent::Hash with wrap: false' do it 'returns a ::Hash with wrap: false' do
hash = { 'one' => 1 } hash = { 'one' => 1 }
expect(normalize(hash, wrap: false)).to be_an_instance_of(Concurrent::Hash) expect(normalize(hash, wrap: false)).to be_an_instance_of(::Hash)
end end
it 'normalizes all values' do it 'normalizes all values' do
@ -267,9 +267,9 @@ describe Rackstash::Fields::AbstractCollection do
expect(normalize(array, wrap: false)).to all be_frozen expect(normalize(array, wrap: false)).to all be_frozen
end end
it 'returns a Concurrent::Array with wrap: false' do it 'returns an ::Array with wrap: false' do
array = [1, :two, 'three'] array = [1, :two, 'three']
expect(normalize(array, wrap: false)).to be_an_instance_of(Concurrent::Array) expect(normalize(array, wrap: false)).to be_an_instance_of(::Array)
end end
it 'normalizes all values' do it 'normalizes all values' do