Skip to content

Commit

Permalink
Add cache for calculating timezone offset
Browse files Browse the repository at this point in the history
For `NUMERIC_PATTERN`, just return static value.
For `NAME_PATTERN`, return lambda to calculate offset later.

This version is 5-10% faster than the previous version.

Signed-off-by: Kenji Okimoto <[email protected]>
  • Loading branch information
okkez committed Jul 17, 2018
1 parent f178b87 commit 54bda48
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
4 changes: 3 additions & 1 deletion lib/fluent/plugin/output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ def configure(conf)
@timekey_zone = @buffer_config.timekey_use_utc ? '+0000' : @buffer_config.timekey_zone
@timekey = @buffer_config.timekey
@timekey_use_utc = @buffer_config.timekey_use_utc
@offset = Fluent::Timezone.utc_offset(@timekey_zone)
@calculate_offset = @offset.respond_to?(:call) ? @offset : nil
@output_time_formatter_cache = {}
end

Expand Down Expand Up @@ -829,7 +831,7 @@ def calculate_timekey(time)
if @timekey_use_utc
(time_int - (time_int % @timekey)).to_i
else
offset = Fluent::Timezone.utc_offset(time, @timekey_zone)
offset = @calculate_offset ? @calculate_offset.call(time) : @offset
(time_int - ((time_int + offset)% @timekey)).to_i
end
end
Expand Down
6 changes: 4 additions & 2 deletions lib/fluent/timezone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,17 @@ def self.formatter(timezone = nil, format = nil)
return nil
end

def self.utc_offset(time, timezone)
def self.utc_offset(timezone)
return 0 if timezone.nil?

case timezone
when NUMERIC_PATTERN
Time.zone_offset(timezone)
when NAME_PATTERN
tz = TZInfo::Timezone.get(timezone)
tz.period_for_utc(time).utc_total_offset
->(time) {
tz.period_for_utc(time).utc_total_offset
}
end
end
end
Expand Down

0 comments on commit 54bda48

Please sign in to comment.