monotime 0.8.1 → 0.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 207159be0c76004c8676ac860106cd133344a29ca0021decbc14bc7448a17fab
4
- data.tar.gz: 89c97583a40bfe82dd7c822cfd9a165be19c796cdf32396ee49ef861bbf7fb32
3
+ metadata.gz: fa145221981b5ff0adb6d1cd3aa77a7e9e9a32942ab9a849a2f83a0d434e245d
4
+ data.tar.gz: c86e6a4ca11e2e9569089574662e96d2cf0ef1b2216ee638832099a5570cd5da
5
5
  SHA512:
6
- metadata.gz: c3aa917f832adedaa98acca61c58858d9e4ce57922d3ba457921b2e2c8adc023875a328c308258555f38dbb482168da73f7488c6306bd9751089b3ef21f5e7b0
7
- data.tar.gz: b340008fa59637177c6c3caad1d4f5816ba8626a533993d3cc8de615b4c12899b3939e4338f6dae04d66fa09c90e6d7de06667cd02af915c2717bef7fca9aa25
6
+ metadata.gz: 78a3a8dc8817cc0f99b67d2839d341e01259815c1fefce78ad0d619f2a4c2739414896a98322a2dd620d105b3b1636f6d355d2250deb71920e3a444d970bc0c4
7
+ data.tar.gz: 5408bc28e13df38a04228eedd325371f7136c036c6ab57c0775c7adc401d4e516acae375502bf16d1f5e3945fe2aaf9b60e6e8549d57570a16d4ec59b10a5b99
data/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.8.2] - 2023-09-22
4
+
5
+ ### Added
6
+
7
+ - `Instant.clock_name` is back and now tracks `clock_id` using reflection.
8
+ - Explicit minimum Ruby version in gemspec (2.7.0).
9
+
10
+ ### Changed
11
+
12
+ - Clock auto-selection redux. We choose the first available from:
13
+
14
+ 1. `CLOCK_UPTIME_RAW` on macOS, faster and higher resolution, also used by Rust
15
+ 2. `CLOCK_MONOTONIC`
16
+ 3. `CLOCK_REALTIME`, a non-monotonic fallback that issues a warning on startup
17
+
18
+ - Slight performance bump for `Duration.measure` on Ruby <= 3.1
19
+
3
20
  ## [0.8.1] - 2023-09-18
4
21
 
5
22
  ### Changed
@@ -179,6 +196,7 @@
179
196
  [0.7.1]: https://github.com/Freaky/monotime/commits/v0.7.0
180
197
  [0.8.0]: https://github.com/Freaky/monotime/commits/v0.8.0
181
198
  [0.8.1]: https://github.com/Freaky/monotime/commits/v0.8.1
199
+ [0.8.2]: https://github.com/Freaky/monotime/commits/v0.8.2
182
200
  [issue #1]: https://github.com/Freaky/monotime/issues/1
183
201
  [Ruby #16740]: https://bugs.ruby-lang.org/issues/16740
184
202
  [@celsworth]: https://github.com/celsworth
data/README.md CHANGED
@@ -171,11 +171,11 @@ The gem is available as open source under the terms of the [MIT License](https:/
171
171
  ### Core Ruby
172
172
 
173
173
  For a zero-dependency alternative upon which `monotime` is based, see
174
- [`Process.clock_gettime`](https://ruby-doc.org/core-2.6.3/Process.html#method-c-clock_gettime).
174
+ [`Process.clock_gettime`](https://www.rubydoc.info/stdlib/core/Process:clock_gettime).
175
175
 
176
- `Process::CLOCK_MONOTONIC` is a safe default, but other options may offer better
177
- behaviour in face of NTP frequency skew or suspend/resume and should be evaluated
178
- carefully.
176
+ `Process::CLOCK_MONOTONIC` is a safe default, but other options may offer higher
177
+ resolution or alternative behaviour in light of system suspend/resume or NTP
178
+ frequency skew.
179
179
 
180
180
  ### Other Gems
181
181
 
@@ -106,7 +106,9 @@ module Monotime
106
106
  #
107
107
  # @return [Duration]
108
108
  def measure
109
- Instant.now.tap { yield }.elapsed
109
+ start = Instant.now
110
+ yield
111
+ start.elapsed
110
112
  end
111
113
 
112
114
  # Return the result of the yielded block alongside a +Duration+.
@@ -31,17 +31,24 @@ module Monotime
31
31
  # * +Process::CLOCK_MONOTONIC_PRECISE+
32
32
  # * +Process::CLOCK_MONOTONIC_FAST+
33
33
  # * +Process::CLOCK_MONOTONIC+
34
+ # * +:MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC+
35
+ # * +:TIMES_BASED_CLOCK_MONOTONIC+
34
36
  #
35
- # These are platform-dependant and may vary in resolution, performance,
36
- # and behaviour from NTP frequency skew and system suspend/resume, and
37
- # should be selected with care.
37
+ # These are platform-dependant and may vary in resolution, accuracy,
38
+ # performance, and behaviour in light of system suspend/resume and NTP
39
+ # frequency skew. They should be selected carefully based on your specific
40
+ # needs and environment.
38
41
  #
39
42
  # It is possible to set non-monotonic clock sources here. You probably
40
43
  # shouldn't.
41
44
  #
42
- # Defaults to +Process::CLOCK_MONOTONIC+.
45
+ # Defaults to auto-selection from whatever is available from:
43
46
  #
44
- # @param id [Numeric]
47
+ # * +CLOCK_UPTIME_RAW+ (if running under macOS)
48
+ # * +CLOCK_MONOTONIC+
49
+ # * +CLOCK_REALTIME+ (non-monotonic fallback, issues a run-time warning)
50
+ #
51
+ # @param id [Numeric, Symbol]
45
52
  attr_accessor :clock_id
46
53
 
47
54
  # The function used to create +Instant+ instances.
@@ -61,16 +68,48 @@ module Monotime
61
68
  # Note per Ruby issue #16740, the practical usability of this method is
62
69
  # dubious and non-portable.
63
70
  #
64
- # @param clock [Numeric] Optional clock id instead of default.
71
+ # @param clock [Numeric, Symbol] Optional clock id instead of default.
65
72
  def clock_getres(clock = clock_id)
66
73
  Duration.from_nanos(Process.clock_getres(clock, :nanosecond))
67
74
  rescue SystemCallError
68
75
  # suppress errors
69
76
  end
77
+
78
+ # The symbolic name of the currently-selected +clock_id+, if available.
79
+ #
80
+ # @return [Symbol, nil]
81
+ def clock_name
82
+ return clock_id if clock_id.is_a? Symbol
83
+
84
+ Process.constants.find do |c|
85
+ c.to_s.start_with?('CLOCK_') && Process.const_get(c) == clock_id
86
+ end
87
+ end
88
+
89
+ private
90
+
91
+ def select_clock_id
92
+ if RUBY_PLATFORM.include?('darwin') && Process.const_defined?(:CLOCK_UPTIME_RAW)
93
+ # Offers nanosecond resolution and appears to be slightly faster on two
94
+ # different Macs (M1 and x64)
95
+ #
96
+ # There is also :MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC which calls
97
+ # mach_absolute_time() directly, but documentation for that recommends
98
+ # CLOCK_UPTIME_RAW, and the performance difference is minimal.
99
+ Process::CLOCK_UPTIME_RAW
100
+ elsif Process.const_defined?(:CLOCK_MONOTONIC)
101
+ Process::CLOCK_MONOTONIC
102
+ else
103
+ # There is also :TIMES_BASED_CLOCK_MONOTONIC, but having seen it just return
104
+ # 0 instead of an error on a MSVC build this may be the safer option.
105
+ warn 'No monotonic clock source detected, falling back to CLOCK_REALTIME'
106
+ Process::CLOCK_REALTIME
107
+ end
108
+ end
70
109
  end
71
110
 
72
111
  self.monotonic_function = -> { Process.clock_gettime(clock_id, :nanosecond) }
73
- self.clock_id = Process::CLOCK_MONOTONIC
112
+ self.clock_id = select_clock_id
74
113
 
75
114
  # Create a new +Instant+ from an optional nanosecond measurement.
76
115
  #
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Monotime
4
4
  # Version of the `monotime` gem
5
- MONOTIME_VERSION = '0.8.1'
5
+ MONOTIME_VERSION = '0.8.2'
6
6
  end
data/monotime.gemspec CHANGED
@@ -13,6 +13,8 @@ Gem::Specification.new do |spec|
13
13
  spec.homepage = "https://github.com/Freaky/monotime"
14
14
  spec.license = "MIT"
15
15
 
16
+ spec.required_ruby_version = '>= 2.7.0'
17
+
16
18
  # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
19
  # to allow pushing to a single host or delete this section to allow pushing to any host.
18
20
  if spec.respond_to?(:metadata)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: monotime
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.1
4
+ version: 0.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Hurst
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-09-18 00:00:00.000000000 Z
11
+ date: 2023-09-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -102,7 +102,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
102
102
  requirements:
103
103
  - - ">="
104
104
  - !ruby/object:Gem::Version
105
- version: '0'
105
+ version: 2.7.0
106
106
  required_rubygems_version: !ruby/object:Gem::Requirement
107
107
  requirements:
108
108
  - - ">="