Skip to content

Commit 81d0dc9

Browse files
committed
Use Process.clock_gettime unit argument to save some floating point multiplications
1 parent 7ddfa93 commit 81d0dc9

File tree

3 files changed

+14
-15
lines changed

3 files changed

+14
-15
lines changed

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 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
105+
start = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
106106
@mutex.synchronize do
107107
if pending?
108108
execute_query(@pool.connection)
109109
else
110-
@lock_wait = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start) * 1_000
110+
@lock_wait = (Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond) - start)
111111
end
112112
end
113113
else

activesupport/lib/active_support/notifications/instrumenter.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ class Event
6262
def initialize(name, start, ending, transaction_id, payload)
6363
@name = name
6464
@payload = payload.dup
65-
@time = start
65+
@time = start ? start.to_f * 1_000.0 : start
6666
@transaction_id = transaction_id
67-
@end = ending
67+
@end = ending ? ending.to_f * 1_000.0 : ending
6868
@children = []
69-
@cpu_time_start = 0
70-
@cpu_time_finish = 0
69+
@cpu_time_start = 0.0
70+
@cpu_time_finish = 0.0
7171
@allocation_count_start = 0
7272
@allocation_count_finish = 0
7373
end
@@ -102,7 +102,7 @@ def finish!
102102
# Returns the CPU time (in milliseconds) passed since the call to
103103
# +start!+ and the call to +finish!+
104104
def cpu_time
105-
(@cpu_time_finish - @cpu_time_start) * 1000
105+
@cpu_time_finish - @cpu_time_start
106106
end
107107

108108
# Returns the idle time time (in milliseconds) passed since the call to
@@ -130,7 +130,7 @@ def allocations
130130
#
131131
# @event.duration # => 1000.138
132132
def duration
133-
1000.0 * (self.end - time)
133+
self.end - time
134134
end
135135

136136
def <<(event)
@@ -143,18 +143,18 @@ def parent_of?(event)
143143

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

149149
begin
150-
Process.clock_gettime(Process::CLOCK_THREAD_CPUTIME_ID)
150+
Process.clock_gettime(Process::CLOCK_THREAD_CPUTIME_ID, :float_millisecond)
151151

152152
def now_cpu
153-
Process.clock_gettime(Process::CLOCK_THREAD_CPUTIME_ID)
153+
Process.clock_gettime(Process::CLOCK_THREAD_CPUTIME_ID, :float_millisecond)
154154
end
155155
rescue
156156
def now_cpu
157-
0
157+
0.0
158158
end
159159
end
160160

activesupport/test/notifications_test.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -434,12 +434,11 @@ def test_event_is_pushed_even_without_block
434434

435435
class EventTest < TestCase
436436
def test_events_are_initialized_with_details
437-
time = Time.now
437+
time = Time.now.to_f
438438
event = event(:foo, time, time + 0.01, random_id, {})
439439

440440
assert_equal :foo, event.name
441-
assert_equal time, event.time
442-
assert_in_delta 10.0, event.duration, 0.00001
441+
assert_in_delta 10.0, event.duration, 0.0001
443442
end
444443

445444
def test_event_cpu_time_does_not_raise_error_when_start_or_finished_not_called

0 commit comments

Comments
 (0)