From 6f6da556e5d280bcab30574a761813bb83e21cf5 Mon Sep 17 00:00:00 2001 From: Masahiro Nakagawa Date: Thu, 12 Apr 2018 08:40:13 +0900 Subject: [PATCH 1/2] filter_grep: Improve the performance MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Remove catch - Reduce method call Warming up -------------------------------------- old 3.371k i/100ms new 3.522k i/100ms Calculating ------------------------------------- old 33.531k (± 4.1%) i/s - 168.550k in 5.035547s new 35.888k (± 3.4%) i/s - 179.622k in 5.011044s Signed-off-by: Masahiro Nakagawa --- lib/fluent/plugin/filter_grep.rb | 40 ++++++++++++++++---------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/lib/fluent/plugin/filter_grep.rb b/lib/fluent/plugin/filter_grep.rb index 7704bf2be2..5c62f0127b 100644 --- a/lib/fluent/plugin/filter_grep.rb +++ b/lib/fluent/plugin/filter_grep.rb @@ -25,10 +25,10 @@ class GrepFilter < Filter def initialize super - @_regexp_and_conditions = [] - @_exclude_and_conditions = [] - @_regexp_or_conditions = [] - @_exclude_or_conditions = [] + @_regexp_and_conditions = nil + @_exclude_and_conditions = nil + @_regexp_or_conditions = nil + @_exclude_or_conditions = nil end # for test @@ -153,35 +153,35 @@ def configure(conf) end end - @_regexp_and_conditions = regexp_and_conditions.values - @_exclude_and_conditions = exclude_and_conditions.values - @_regexp_or_conditions = regexp_or_conditions.values - @_exclude_or_conditions = exclude_or_conditions.values + @_regexp_and_conditions = regexp_and_conditions.values unless regexp_and_conditions.empty? + @_exclude_and_conditions = exclude_and_conditions.values unless exclude_and_conditions.empty? + @_regexp_or_conditions = regexp_or_conditions.values unless regexp_or_conditions.empty? + @_exclude_or_conditions = exclude_or_conditions.values unless exclude_or_conditions.empty? end def filter(tag, time, record) - result = nil begin - catch(:break_loop) do + if @_regexp_and_conditions @_regexp_and_conditions.each do |expression| - throw :break_loop unless expression.match?(record) - end - if !@_regexp_or_conditions.empty? && @_regexp_or_conditions.none? {|expression| expression.match?(record) } - throw :break_loop - end - if !@_exclude_and_conditions.empty? && @_exclude_and_conditions.all? {|expression| expression.match?(record) } - throw :break_loop + return nil unless expression.match?(record) end + end + if @_regexp_or_conditions && @_regexp_or_conditions.none? { |expression| expression.match?(record) } + return nil + end + if @_exclude_and_conditions && @_exclude_and_conditions.all? { |expression| expression.match?(record) } + return nil + end + if @_exclude_or_conditions @_exclude_or_conditions.each do |expression| - throw :break_loop if expression.match?(record) + return nil if expression.match?(record) end - result = record end rescue => e log.warn "failed to grep events", error: e log.warn_backtrace end - result + record end Expression = Struct.new(:key, :pattern) do From ba4e740b31f6c92d987e2a766ea39f9f59b1db0f Mon Sep 17 00:00:00 2001 From: Masahiro Nakagawa Date: Fri, 13 Apr 2018 06:00:30 +0900 Subject: [PATCH 2/2] Use better method instead of #each Signed-off-by: Masahiro Nakagawa --- lib/fluent/plugin/filter_grep.rb | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/lib/fluent/plugin/filter_grep.rb b/lib/fluent/plugin/filter_grep.rb index 5c62f0127b..f590550b38 100644 --- a/lib/fluent/plugin/filter_grep.rb +++ b/lib/fluent/plugin/filter_grep.rb @@ -161,10 +161,8 @@ def configure(conf) def filter(tag, time, record) begin - if @_regexp_and_conditions - @_regexp_and_conditions.each do |expression| - return nil unless expression.match?(record) - end + if @_regexp_and_conditions && @_regexp_and_conditions.any? { |expression| !expression.match?(record) } + return nil end if @_regexp_or_conditions && @_regexp_or_conditions.none? { |expression| expression.match?(record) } return nil @@ -172,10 +170,8 @@ def filter(tag, time, record) if @_exclude_and_conditions && @_exclude_and_conditions.all? { |expression| expression.match?(record) } return nil end - if @_exclude_or_conditions - @_exclude_or_conditions.each do |expression| - return nil if expression.match?(record) - end + if @_exclude_or_conditions && @_exclude_or_conditions.any? { |expression| expression.match?(record) } + return nil end rescue => e log.warn "failed to grep events", error: e