Skip to content

Commit

Permalink
Prefer IO::Event::Timers.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Jun 3, 2024
1 parent 0886df5 commit c0d2722
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 11 deletions.
3 changes: 1 addition & 2 deletions async.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,5 @@ Gem::Specification.new do |spec|

spec.add_dependency "console", ["~> 1.25", ">= 1.25.2"]
spec.add_dependency "fiber-annotation"
spec.add_dependency "io-event", ["~> 1.5", ">= 1.5.1"]
spec.add_dependency "timers", "~> 4.1"
spec.add_dependency "io-event", "~> 1.6"
end
35 changes: 35 additions & 0 deletions benchmark/timers/after_0.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2024, by Samuel Williams.

require 'benchmark/ips'

require 'timers'
require 'io/event/timers'

Benchmark.ips do |benchmark|
benchmark.time = 1
benchmark.warmup = 1

benchmark.report("Timers::Group") do |count|
timers = Timers::Group.new

while count > 0
timers.after(0) {count -= 1}
timers.fire
end
end

benchmark.report("IO::Event::Timers") do |count|
timers = IO::Event::Timers.new

while count > 0
timers.after(0) {count -= 1}
timers.fire
end
end

benchmark.compare!
end
41 changes: 41 additions & 0 deletions benchmark/timers/after_cancel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2024, by Samuel Williams.

require 'benchmark/ips'

require 'timers'
require 'io/event/timers'

Benchmark.ips do |benchmark|
benchmark.time = 1
benchmark.warmup = 1

benchmark.report("Timers::Group") do |count|
timers = Timers::Group.new

while count > 0
timer = timers.after(0) {}
timer.cancel
count -= 1
end

timers.fire
end

benchmark.report("IO::Event::Timers") do |count|
timers = IO::Event::Timers.new

while count > 0
timer = timers.after(0) {}
timer.cancel!
count -= 1
end

timers.fire
end

benchmark.compare!
end
39 changes: 39 additions & 0 deletions benchmark/timers/after_n.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2024, by Samuel Williams.

require 'benchmark/ips'

require 'timers'
require 'io/event/timers'

Benchmark.ips do |benchmark|
benchmark.time = 1
benchmark.warmup = 1

benchmark.report("Timers::Group") do |count|
timers = Timers::Group.new

while count > 0
timers.after(0) {}
count -= 1
end

timers.fire
end

benchmark.report("IO::Event::Timers") do |count|
timers = IO::Event::Timers.new

while count > 0
timers.after(0) {}
count -= 1
end

timers.fire
end

benchmark.compare!
end
13 changes: 6 additions & 7 deletions lib/async/scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
require 'io/event'

require 'console'
require 'timers'
require 'resolv'

module Async
Expand Down Expand Up @@ -40,7 +39,7 @@ def initialize(parent = nil, selector: nil)
@busy_time = 0.0
@idle_time = 0.0

@timers = ::Timers::Group.new
@timers = ::IO::Event::Timers.new
end

# Compute the scheduler load according to the busy and idle times that are updated by the run loop.
Expand Down Expand Up @@ -165,7 +164,7 @@ def block(blocker, timeout)
@blocked -= 1
end
ensure
timer&.cancel
timer&.cancel!
end

# @asynchronous May be called from any thread.
Expand Down Expand Up @@ -225,7 +224,7 @@ def io_wait(io, events, timeout = nil)

return @selector.io_wait(fiber, io, events)
ensure
timer&.cancel
timer&.cancel!
end

if ::IO::Event::Support.buffer?
Expand All @@ -240,7 +239,7 @@ def io_read(io, buffer, length, offset = 0)

@selector.io_read(fiber, io, buffer, length, offset)
ensure
timer&.cancel
timer&.cancel!
end

if RUBY_ENGINE != "ruby" || RUBY_VERSION >= "3.3.1"
Expand All @@ -255,7 +254,7 @@ def io_write(io, buffer, length, offset = 0)

@selector.io_write(fiber, io, buffer, length, offset)
ensure
timer&.cancel
timer&.cancel!
end
end
end
Expand Down Expand Up @@ -416,7 +415,7 @@ def with_timeout(duration, exception = TimeoutError, message = "execution expire

yield timer
ensure
timer.cancel if timer
timer&.cancel!
end

def timeout_after(duration, exception, message, &block)
Expand Down
3 changes: 1 addition & 2 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# ![Async](logo.svg)

Async is a composable asynchronous I/O framework for Ruby based on [io-event](https://github.com/socketry/io-event) and
[timers](https://github.com/socketry/timers).
Async is a composable asynchronous I/O framework for Ruby based on [io-event](https://github.com/socketry/io-event).

> "Lately I've been looking into `async`, as one of my projects –
> [tus-ruby-server](https://github.com/janko/tus-ruby-server) – would really benefit from non-blocking I/O. It's really
Expand Down

0 comments on commit c0d2722

Please sign in to comment.