Skip to content

Commit 7ddfa93

Browse files
committed
Stop using Concurrent.monotonic_time
Unless you are on a Ruby older than 2.3 (or some very old JRuby) `Concurrent.monotonic_time` is just `Process.clock_gettime(Process::CLOCK_MONOTONIC)`. So might as well skip the extra method call, and more importantly it allows in many cases to pass the scale as second argument and save some floating point multiplication.
1 parent 0919aa9 commit 7ddfa93

File tree

10 files changed

+23
-23
lines changed

10 files changed

+23
-23
lines changed

activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -527,13 +527,13 @@ def attempt_to_checkout_all_existing_connections(raise_on_acquisition_timeout =
527527
end
528528

529529
newly_checked_out = []
530-
timeout_time = Concurrent.monotonic_time + (@checkout_timeout * 2)
530+
timeout_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) + (@checkout_timeout * 2)
531531

532532
@available.with_a_bias_for(Thread.current) do
533533
loop do
534534
synchronize do
535535
return if collected_conns.size == @connections.size && @now_connecting == 0
536-
remaining_timeout = timeout_time - Concurrent.monotonic_time
536+
remaining_timeout = timeout_time - Process.clock_gettime(Process::CLOCK_MONOTONIC)
537537
remaining_timeout = 0 if remaining_timeout < 0
538538
conn = checkout_for_exclusive_access(remaining_timeout)
539539
collected_conns << conn

activerecord/lib/active_record/connection_adapters/abstract/connection_pool/queue.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def no_wait_poll
110110
def wait_poll(timeout)
111111
@num_waiting += 1
112112

113-
t0 = Concurrent.monotonic_time
113+
t0 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
114114
elapsed = 0
115115
loop do
116116
ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
@@ -119,7 +119,7 @@ def wait_poll(timeout)
119119

120120
return remove if any?
121121

122-
elapsed = Concurrent.monotonic_time - t0
122+
elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - t0
123123
if elapsed >= timeout
124124
msg = "could not obtain a connection from the pool within %0.3f seconds (waited %0.3f seconds); all pooled connections were in use" %
125125
[timeout, elapsed]

activerecord/lib/active_record/connection_adapters/abstract_adapter.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def initialize(connection, logger = nil, config = {}) # :nodoc:
8888
@logger = logger
8989
@config = config
9090
@pool = ActiveRecord::ConnectionAdapters::NullPool.new
91-
@idle_since = Concurrent.monotonic_time
91+
@idle_since = Process.clock_gettime(Process::CLOCK_MONOTONIC)
9292
@visitor = arel_visitor
9393
@statements = build_statement_pool
9494
@lock = ActiveSupport::Concurrency::LoadInterlockAwareMonitor.new
@@ -242,7 +242,7 @@ def expire
242242
"Current thread: #{Thread.current}."
243243
end
244244

245-
@idle_since = Concurrent.monotonic_time
245+
@idle_since = Process.clock_gettime(Process::CLOCK_MONOTONIC)
246246
@owner = nil
247247
else
248248
raise ActiveRecordError, "Cannot expire connection, it is not currently leased."
@@ -265,7 +265,7 @@ def steal! # :nodoc:
265265
# Seconds since this connection was returned to the pool
266266
def seconds_idle # :nodoc:
267267
return 0 if in_use?
268-
Concurrent.monotonic_time - @idle_since
268+
Process.clock_gettime(Process::CLOCK_MONOTONIC) - @idle_since
269269
end
270270

271271
def unprepared_statement

activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ def write_query?(sql) # :nodoc:
3030

3131
def explain(arel, binds = [])
3232
sql = "EXPLAIN #{to_sql(arel, binds)}"
33-
start = Concurrent.monotonic_time
33+
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
3434
result = exec_query(sql, "EXPLAIN", binds)
35-
elapsed = Concurrent.monotonic_time - start
35+
elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
3636

3737
MySQL::ExplainPrettyPrinter.new.pp(result, elapsed)
3838
end

activerecord/lib/active_record/future_result.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ def canceled?
102102

103103
def execute_or_wait
104104
if pending?
105-
start = Concurrent.monotonic_time
105+
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
106106
@mutex.synchronize do
107107
if pending?
108108
execute_query(@pool.connection)
109109
else
110-
@lock_wait = (Concurrent.monotonic_time - start) * 1_000
110+
@lock_wait = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start) * 1_000
111111
end
112112
end
113113
else

activerecord/test/cases/connection_pool_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,15 +211,15 @@ def test_idle_timeout_configuration
211211

212212
idle_conn.instance_variable_set(
213213
:@idle_since,
214-
Concurrent.monotonic_time - 0.01
214+
Process.clock_gettime(Process::CLOCK_MONOTONIC) - 0.01
215215
)
216216

217217
@pool.flush
218218
assert_equal 1, @pool.connections.length
219219

220220
idle_conn.instance_variable_set(
221221
:@idle_since,
222-
Concurrent.monotonic_time - 0.02
222+
Process.clock_gettime(Process::CLOCK_MONOTONIC) - 0.02
223223
)
224224

225225
@pool.flush
@@ -238,7 +238,7 @@ def test_disable_flush
238238

239239
idle_conn.instance_variable_set(
240240
:@idle_since,
241-
Concurrent.monotonic_time - 1
241+
Process.clock_gettime(Process::CLOCK_MONOTONIC) - 1
242242
)
243243

244244
@pool.flush
@@ -257,7 +257,7 @@ def test_flush
257257

258258
idle_conn.instance_variable_set(
259259
:@idle_since,
260-
Concurrent.monotonic_time - 1000
260+
Process.clock_gettime(Process::CLOCK_MONOTONIC) - 1000
261261
)
262262

263263
@pool.flush(30)

activesupport/lib/active_support/cache/memory_store.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ def prune(target_size, max_time = nil)
8989
return if pruning?
9090
@pruning = true
9191
begin
92-
start_time = Concurrent.monotonic_time
92+
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
9393
cleanup
9494
instrument(:prune, target_size, from: @cache_size) do
9595
keys = synchronize { @data.keys }
9696
keys.each do |key|
9797
delete_entry(key, **options)
98-
return if @cache_size <= target_size || (max_time && Concurrent.monotonic_time - start_time > max_time)
98+
return if @cache_size <= target_size || (max_time && Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time > max_time)
9999
end
100100
end
101101
ensure

activesupport/lib/active_support/notifications/fanout.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,13 @@ def publish(name, *args)
236236

237237
def start(name, id, payload)
238238
timestack = Thread.current[:_timestack_monotonic] ||= []
239-
timestack.push Concurrent.monotonic_time
239+
timestack.push Process.clock_gettime(Process::CLOCK_MONOTONIC)
240240
end
241241

242242
def finish(name, id, payload)
243243
timestack = Thread.current[:_timestack_monotonic]
244244
started = timestack.pop
245-
@delegate.call(name, started, Concurrent.monotonic_time, id, payload)
245+
@delegate.call(name, started, Process.clock_gettime(Process::CLOCK_MONOTONIC), id, payload)
246246
end
247247
end
248248

activesupport/lib/active_support/notifications/instrumenter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def parent_of?(event)
143143

144144
private
145145
def now
146-
Concurrent.monotonic_time
146+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
147147
end
148148

149149
begin

activesupport/test/notifications_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -450,14 +450,14 @@ def test_event_cpu_time_does_not_raise_error_when_start_or_finished_not_called
450450
end
451451

452452
def test_events_consumes_information_given_as_payload
453-
event = event(:foo, Concurrent.monotonic_time, Concurrent.monotonic_time + 1, random_id, payload: :bar)
453+
event = event(:foo, Process.clock_gettime(Process::CLOCK_MONOTONIC), Process.clock_gettime(Process::CLOCK_MONOTONIC) + 1, random_id, payload: :bar)
454454
assert_equal Hash[payload: :bar], event.payload
455455
end
456456

457457
def test_event_is_parent_based_on_children
458-
time = Concurrent.monotonic_time
458+
time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
459459

460-
parent = event(:foo, Concurrent.monotonic_time, Concurrent.monotonic_time + 100, random_id, {})
460+
parent = event(:foo, Process.clock_gettime(Process::CLOCK_MONOTONIC), Process.clock_gettime(Process::CLOCK_MONOTONIC) + 100, random_id, {})
461461
child = event(:foo, time, time + 10, random_id, {})
462462
not_child = event(:foo, time, time + 100, random_id, {})
463463

0 commit comments

Comments
 (0)