Skip to content

Commit c0d2722

Browse files
committed
Prefer IO::Event::Timers.
1 parent 0886df5 commit c0d2722

File tree

6 files changed

+123
-11
lines changed

6 files changed

+123
-11
lines changed

async.gemspec

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,5 @@ Gem::Specification.new do |spec|
2727

2828
spec.add_dependency "console", ["~> 1.25", ">= 1.25.2"]
2929
spec.add_dependency "fiber-annotation"
30-
spec.add_dependency "io-event", ["~> 1.5", ">= 1.5.1"]
31-
spec.add_dependency "timers", "~> 4.1"
30+
spec.add_dependency "io-event", "~> 1.6"
3231
end

benchmark/timers/after_0.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
# Released under the MIT License.
5+
# Copyright, 2024, by Samuel Williams.
6+
7+
require 'benchmark/ips'
8+
9+
require 'timers'
10+
require 'io/event/timers'
11+
12+
Benchmark.ips do |benchmark|
13+
benchmark.time = 1
14+
benchmark.warmup = 1
15+
16+
benchmark.report("Timers::Group") do |count|
17+
timers = Timers::Group.new
18+
19+
while count > 0
20+
timers.after(0) {count -= 1}
21+
timers.fire
22+
end
23+
end
24+
25+
benchmark.report("IO::Event::Timers") do |count|
26+
timers = IO::Event::Timers.new
27+
28+
while count > 0
29+
timers.after(0) {count -= 1}
30+
timers.fire
31+
end
32+
end
33+
34+
benchmark.compare!
35+
end

benchmark/timers/after_cancel.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
# Released under the MIT License.
5+
# Copyright, 2024, by Samuel Williams.
6+
7+
require 'benchmark/ips'
8+
9+
require 'timers'
10+
require 'io/event/timers'
11+
12+
Benchmark.ips do |benchmark|
13+
benchmark.time = 1
14+
benchmark.warmup = 1
15+
16+
benchmark.report("Timers::Group") do |count|
17+
timers = Timers::Group.new
18+
19+
while count > 0
20+
timer = timers.after(0) {}
21+
timer.cancel
22+
count -= 1
23+
end
24+
25+
timers.fire
26+
end
27+
28+
benchmark.report("IO::Event::Timers") do |count|
29+
timers = IO::Event::Timers.new
30+
31+
while count > 0
32+
timer = timers.after(0) {}
33+
timer.cancel!
34+
count -= 1
35+
end
36+
37+
timers.fire
38+
end
39+
40+
benchmark.compare!
41+
end

benchmark/timers/after_n.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
# Released under the MIT License.
5+
# Copyright, 2024, by Samuel Williams.
6+
7+
require 'benchmark/ips'
8+
9+
require 'timers'
10+
require 'io/event/timers'
11+
12+
Benchmark.ips do |benchmark|
13+
benchmark.time = 1
14+
benchmark.warmup = 1
15+
16+
benchmark.report("Timers::Group") do |count|
17+
timers = Timers::Group.new
18+
19+
while count > 0
20+
timers.after(0) {}
21+
count -= 1
22+
end
23+
24+
timers.fire
25+
end
26+
27+
benchmark.report("IO::Event::Timers") do |count|
28+
timers = IO::Event::Timers.new
29+
30+
while count > 0
31+
timers.after(0) {}
32+
count -= 1
33+
end
34+
35+
timers.fire
36+
end
37+
38+
benchmark.compare!
39+
end

lib/async/scheduler.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
require 'io/event'
1212

1313
require 'console'
14-
require 'timers'
1514
require 'resolv'
1615

1716
module Async
@@ -40,7 +39,7 @@ def initialize(parent = nil, selector: nil)
4039
@busy_time = 0.0
4140
@idle_time = 0.0
4241

43-
@timers = ::Timers::Group.new
42+
@timers = ::IO::Event::Timers.new
4443
end
4544

4645
# Compute the scheduler load according to the busy and idle times that are updated by the run loop.
@@ -165,7 +164,7 @@ def block(blocker, timeout)
165164
@blocked -= 1
166165
end
167166
ensure
168-
timer&.cancel
167+
timer&.cancel!
169168
end
170169

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

226225
return @selector.io_wait(fiber, io, events)
227226
ensure
228-
timer&.cancel
227+
timer&.cancel!
229228
end
230229

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

241240
@selector.io_read(fiber, io, buffer, length, offset)
242241
ensure
243-
timer&.cancel
242+
timer&.cancel!
244243
end
245244

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

256255
@selector.io_write(fiber, io, buffer, length, offset)
257256
ensure
258-
timer&.cancel
257+
timer&.cancel!
259258
end
260259
end
261260
end
@@ -416,7 +415,7 @@ def with_timeout(duration, exception = TimeoutError, message = "execution expire
416415

417416
yield timer
418417
ensure
419-
timer.cancel if timer
418+
timer&.cancel!
420419
end
421420

422421
def timeout_after(duration, exception, message, &block)

readme.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# ![Async](logo.svg)
22

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

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

0 commit comments

Comments
 (0)