Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fiber refinement #335

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .projections.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"lib/async/*.rb": {
"alternate": "test/async/{}.rb"
},
"lib/kernel/*.rb": {
"alternate": "test/kernel/{}.rb"
},
"test/async/*.rb": {
"alternate": "lib/async/{}.rb"
},
"test/kernel/*.rb": {
"alternate": "lib/kernel/{}.rb"
}
}
48 changes: 48 additions & 0 deletions benchmark/monkey_patch_vs_refinement.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2024, by Patrik Wenger.

require 'benchmark/ips'

GC.disable

Fiber.attr_accessor :foo

module FiberWithBar
refine Fiber do
attr_accessor :bar
end
end

using FiberWithBar

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

benchmark.report("monkey patch") do |count|
while count > 0
Fiber.new do
count -= 1

Fiber.current.foo = :baz
fail if Fiber.current.foo != :baz
end.resume
end
end

benchmark.report("refinement") do |count|
while count > 0
Fiber.new do
count -= 1

Fiber.current.bar = :baz
fail if Fiber.current.bar != :baz
end.resume
end
end

benchmark.compare!
end
10 changes: 8 additions & 2 deletions lib/async/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@
require_relative 'node'
require_relative 'condition'

Fiber.attr_accessor :async_task

module Async
module FiberWithAsyncTask
refine Fiber do
attr_accessor :async_task
end
end

using FiberWithAsyncTask

# Raised when a task is explicitly stopped.
class Stop < Exception
# Used to defer stopping the current task until later.
Expand Down
Loading