1
0
mirror of https://github.com/meineerde/rackstash.git synced 2025-10-17 14:01:01 +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
return wrap ? value : value.raw
when ::Hash
hash = Concurrent::Hash.new
hash = {}
value.each_pair do |k, v|
hash[utf8_encode(k)] = normalize(v, scope: scope)
end
@ -126,10 +126,7 @@ module Rackstash
end
return hash
when ::Array, ::Set, ::Enumerator
array = Concurrent::Array.new
value.each do |e|
array << normalize(e, scope: scope)
end
array = value.map { |e| normalize(e, scope: scope) }
if wrap
array = Rackstash::Fields::Array.new.tap do |array_field|
array_field.raw = array

View File

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

View File

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

View File

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

View File

@ -198,9 +198,9 @@ describe Rackstash::Fields::AbstractCollection do
expect(normalize(hash, wrap: false).keys).to all be_frozen
end
it 'returns a Concurrent::Hash with wrap: false' do
it 'returns a ::Hash with wrap: false' do
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
it 'normalizes all values' do
@ -267,9 +267,9 @@ describe Rackstash::Fields::AbstractCollection do
expect(normalize(array, wrap: false)).to all be_frozen
end
it 'returns a Concurrent::Array with wrap: false' do
it 'returns an ::Array with wrap: false' do
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
it 'normalizes all values' do