Skip to content

time: use Numeric#divmod to create Fluent::EventTime instance#5287

Merged
kenhys merged 1 commit into
fluent:masterfrom
Watson1978:time
Mar 23, 2026
Merged

time: use Numeric#divmod to create Fluent::EventTime instance#5287
kenhys merged 1 commit into
fluent:masterfrom
Watson1978:time

Conversation

@Watson1978

@Watson1978 Watson1978 commented Mar 21, 2026

Copy link
Copy Markdown
Contributor

Which issue(s) this PR fixes:
Fixes #

What this PR does / why we need it:
This PR slightly improves the performance of creating Fluent::EventTime instances by replacing separate / and % operations with a single divmod call.

When run rake benchmark:run:in_tail with 10 GB data, here is the result of before/after changing:

  before after
rake benchmark:run:in_tail 43.305892 43.170137

Microbenchmark

require 'bundler/inline'
gemfile do
  source 'https://rubygems.org'
  gem 'benchmark-ips'
end

Benchmark.ips do |x|

  now = Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond)

  x.report("div + mod") {
    now / 1_000_000_000
    now % 1_000_000_000
  }

  x.report("divmod") {
    now.divmod(1_000_000_000)
  }

  x.compare!
end
  • result
ruby 4.0.1 (2026-01-13 revision e04267a14b) +PRISM [x64-mingw-ucrt]
Warming up --------------------------------------
           div + mod   769.369k i/100ms
              divmod     1.053M i/100ms
Calculating -------------------------------------
           div + mod      7.356M (± 3.6%) i/s  (135.94 ns/i) -     36.930M in   5.027199s
              divmod     10.262M (± 5.5%) i/s   (97.45 ns/i) -     51.612M in   5.045684s

Comparison:
              divmod: 10262087.7 i/s
           div + mod:  7356170.0 i/s - 1.40x  slower

Docs Changes:
N/A

Release Note:

  • core: Improve performance of Fluent::EventTime creation using divmod

Signed-off-by: Shizuo Fujita <fujita@clear-code.com>
@Watson1978 Watson1978 requested a review from kenhys March 21, 2026 06:57
@Watson1978 Watson1978 added this to the v1.20.0 milestone Mar 21, 2026

@kenhys kenhys left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@kenhys kenhys merged commit 7619631 into fluent:master Mar 23, 2026
21 checks passed
@Watson1978 Watson1978 deleted the time branch March 24, 2026 04:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants