Skip to content

Commit f8bbbc8

Browse files
committed
Removed LazyReference now that Delay is better, stronger, faster.
1 parent ca16497 commit f8bbbc8

File tree

13 files changed

+48
-159
lines changed

13 files changed

+48
-159
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@
4040
* All high-level abstractions default to the "io executor"
4141
* Fixed bug in `Actor` causing it to prematurely warm global thread pools on gem load
4242
- This also fixed a `RejectedExecutionError` bug when running with minitest/autorun via JRuby
43-
* Added `LazyReference`, a simpler and faster varition of `Delay`
44-
- Updated most internal uses of `Delay` with `LazyReference`
4543
* Moved global logger up to the `Concurrent` namespace and refactored the code
44+
* Optimized the performance of `Delay`
45+
- Fixed a bug in which no executor option on construction caused block execution on a global thread pool
4646

4747
## Current Release v0.8.0 (25 January 2015)
4848

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ This library contains a variety of concurrency abstractions at high and low leve
8383
* [AtomicFixnum](http://ruby-concurrency.github.io/concurrent-ruby/Concurrent/AtomicFixnum.html)
8484
* [AtomicReference](http://ruby-concurrency.github.io/concurrent-ruby/Concurrent/MutexAtomic.html)
8585
* [Delay](http://ruby-concurrency.github.io/concurrent-ruby/Concurrent/Delay.html)
86-
* [LazyReference](http://ruby-concurrency.github.io/concurrent-ruby/Concurrent/LazyReference.html)
8786
* [LazyRegister](http://ruby-concurrency.github.io/concurrent-ruby/Concurrent/LazyRegister.html)
8887
* [I-Structures](http://ruby-concurrency.github.io/concurrent-ruby/Concurrent/IVar.html) (IVar)
8988
* [M-Structures](http://ruby-concurrency.github.io/concurrent-ruby/Concurrent/MVar.html) (MVar)
@@ -123,7 +122,6 @@ require 'concurrent/exchanger' # Concurrent::Exchanger
123122
require 'concurrent/future' # Concurrent::Future
124123
require 'concurrent/ivar' # Concurrent::IVar
125124
require 'concurrent/lazy_register' # Concurrent::LazyRegister
126-
require 'concurrent/lazy_reference' # Concurrent::LazyReference
127125
require 'concurrent/mvar' # Concurrent::MVar
128126
require 'concurrent/promise' # Concurrent::Promise
129127
require 'concurrent/scheduled_task' # Concurrent::ScheduledTask

examples/lazy_and_delay.rb

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,36 @@
33
$: << File.expand_path('../../lib', __FILE__)
44

55
require 'benchmark'
6-
7-
require 'concurrent/delay'
8-
require 'concurrent/lazy_reference'
6+
require 'thread'
7+
require 'concurrent'
98

109
n = 500_000
1110

11+
class LazyReference
12+
def initialize(&block)
13+
raise ArgumentError.new('no block given') unless block_given?
14+
@task = block
15+
@mutex = Mutex.new
16+
@value = nil
17+
@fulfilled = false
18+
end
19+
20+
def value
21+
@mutex.synchronize do
22+
unless @fulfilled
23+
begin
24+
@value = @task.call
25+
ensure
26+
@fulfilled = true
27+
end
28+
end
29+
end
30+
return @value
31+
end
32+
end
33+
1234
delay = Concurrent::Delay.new{ nil }
13-
lazy = Concurrent::LazyReference.new{ nil }
35+
lazy = LazyReference.new{ nil }
1436

1537
delay.value
1638
lazy.value

lib/concurrent.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
require 'concurrent/exchanger'
2020
require 'concurrent/future'
2121
require 'concurrent/ivar'
22-
require 'concurrent/lazy_reference'
2322
require 'concurrent/lazy_register'
2423
require 'concurrent/mvar'
2524
require 'concurrent/promise'

lib/concurrent/actor.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def self.current
4040
Thread.current[:__current_actor__]
4141
end
4242

43-
@root = Delay.new(executor: :immediate) do
43+
@root = Delay.new do
4444
Core.new(parent: nil, name: '/', class: Root, initialized: ivar = IVar.new).reference.tap do
4545
ivar.no_error!
4646
end

lib/concurrent/async.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
require 'thread'
22
require 'concurrent/configuration'
33
require 'concurrent/delay'
4-
require 'concurrent/lazy_reference'
54
require 'concurrent/errors'
65
require 'concurrent/ivar'
76
require 'concurrent/executor/immediate_executor'
@@ -197,15 +196,15 @@ def init_mutex
197196
@__async_initialized__ = true
198197
serializer = Concurrent::SerializedExecution.new
199198

200-
@__async_executor__ = Delay.new(executor: :immediate) {
199+
@__async_executor__ = Delay.new {
201200
Concurrent.global_io_executor
202201
}
203202

204-
@__await_delegator__ = Delay.new(executor: :immediate) {
205-
AsyncDelegator.new(self, LazyReference.new{ Concurrent::ImmediateExecutor.new }, serializer, true)
203+
@__await_delegator__ = Delay.new {
204+
AsyncDelegator.new(self, Delay.new{ Concurrent::ImmediateExecutor.new }, serializer, true)
206205
}
207206

208-
@__async_delegator__ = Delay.new(executor: :immediate) {
207+
@__async_delegator__ = Delay.new {
209208
AsyncDelegator.new(self, @__async_executor__, serializer, false)
210209
}
211210
end

lib/concurrent/configuration.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require 'thread'
2-
require 'concurrent/lazy_reference'
32
require 'concurrent/atomics'
43
require 'concurrent/errors'
54
require 'concurrent/executors'
@@ -25,21 +24,21 @@ module Concurrent
2524
private_constant :AUTO_TERMINATE_ALL_EXECUTORS
2625

2726
# @!visibility private
28-
GLOBAL_FAST_EXECUTOR = LazyReference.new do
27+
GLOBAL_FAST_EXECUTOR = Delay.new do
2928
Concurrent.new_fast_executor(
3029
stop_on_exit: AUTO_TERMINATE_GLOBAL_EXECUTORS.value)
3130
end
3231
private_constant :GLOBAL_FAST_EXECUTOR
3332

3433
# @!visibility private
35-
GLOBAL_IO_EXECUTOR = LazyReference.new do
34+
GLOBAL_IO_EXECUTOR = Delay.new do
3635
Concurrent.new_io_executor(
3736
stop_on_exit: AUTO_TERMINATE_GLOBAL_EXECUTORS.value)
3837
end
3938
private_constant :GLOBAL_IO_EXECUTOR
4039

4140
# @!visibility private
42-
GLOBAL_TIMER_SET = LazyReference.new do
41+
GLOBAL_TIMER_SET = Delay.new do
4342
TimerSet.new(stop_on_exit: AUTO_TERMINATE_GLOBAL_EXECUTORS.value)
4443
end
4544
private_constant :GLOBAL_TIMER_SET

lib/concurrent/delay.rb

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,7 @@ module Concurrent
3636
# constructor option. This will cause the delayed operation to be
3737
# execute on the given executor, allowing the call to timeout.
3838
#
39-
# Because of its simplicity `LazyReference` is much faster than `Delay`:
40-
#
41-
# Rehearsal -------------------------------------------------------
42-
# Delay#value 0.210000 0.000000 0.210000 ( 0.208207)
43-
# Delay#value! 0.240000 0.000000 0.240000 ( 0.247136)
44-
# LazyReference#value 0.160000 0.000000 0.160000 ( 0.158399)
45-
# ---------------------------------------------- total: 0.610000sec
46-
#
47-
# user system total real
48-
# Delay#value 0.200000 0.000000 0.200000 ( 0.203602)
49-
# Delay#value! 0.250000 0.000000 0.250000 ( 0.252535)
50-
# LazyReference#value 0.150000 0.000000 0.150000 ( 0.154053)
51-
#
5239
# @see Concurrent::Dereferenceable
53-
# @see Concurrent::LazyReference
5440
class Delay
5541
include Obligation
5642
include ExecutorOptions
@@ -66,14 +52,12 @@ def initialize(opts = {}, &block)
6652
raise ArgumentError.new('no block given') unless block_given?
6753

6854
init_obligation
55+
set_deref_options(opts)
56+
@task_executor = get_executor_from(opts)
6957

70-
@state = :pending
7158
@task = block
59+
@state = :pending
7260
@computing = false
73-
74-
set_deref_options(opts)
75-
76-
@task_executor = get_executor_from(opts)
7761
end
7862

7963
# Return the value this object represents after applying the options

lib/concurrent/lazy_reference.rb

Lines changed: 0 additions & 58 deletions
This file was deleted.

lib/concurrent/utility/processor_count.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
require 'rbconfig'
2-
require 'concurrent/lazy_reference'
2+
require 'concurrent/delay'
33

44
module Concurrent
55

66
class ProcessorCounter
77
def initialize
8-
@processor_count = LazyReference.new { compute_processor_count }
9-
@physical_processor_count = LazyReference.new { compute_physical_processor_count }
8+
@processor_count = Delay.new { compute_processor_count }
9+
@physical_processor_count = Delay.new { compute_physical_processor_count }
1010
end
1111

1212
# Number of processors seen by the OS and used for process scheduling. For

0 commit comments

Comments
 (0)