Skip to content

Conversation

@byroot
Copy link
Member

@byroot byroot commented Dec 13, 2025

[Feature #21788]

It allows monitor to access internal routines and remove some overhead.

Before:

ruby 4.0.0dev (2025-12-13T04:52:13Z master 71dd272506) +YJIT +PRISM [arm64-darwin25]
Warming up --------------------------------------
               Mutex     2.111M i/100ms
             Monitor     1.736M i/100ms
Calculating -------------------------------------
               Mutex     25.050M (± 0.4%) i/s   (39.92 ns/i) -    126.631M in   5.055208s
             Monitor     19.809M (± 0.1%) i/s   (50.48 ns/i) -    100.672M in   5.082015s

After:

ruby 4.0.0dev (2025-12-13T06:49:18Z core-monitor 6fabf389fd) +YJIT +PRISM [arm64-darwin25]
Warming up --------------------------------------
               Mutex     2.144M i/100ms
             Monitor     1.859M i/100ms
Calculating -------------------------------------
               Mutex     24.771M (± 0.4%) i/s   (40.37 ns/i) -    124.342M in   5.019716s
             Monitor     23.722M (± 0.4%) i/s   (42.15 ns/i) -    118.998M in   5.016361s

Bench:

require 'bundler/inline'

gemfile do
  gem "benchmark-ips"
end

mutex = Mutex.new
require "monitor"
monitor = Monitor.new

Benchmark.ips do |x|
  x.report("Mutex") { mutex.synchronize { } }
  x.report("Monitor") { monitor.synchronize { } }
end

@launchable-app

This comment has been minimized.

@byroot byroot force-pushed the core-monitor branch 6 times, most recently from f5013f6 to 634545f Compare December 14, 2025 11:35
@nobu
Copy link
Member

nobu commented Dec 16, 2025

You might want to move lib/monitor.rb from ext/monitor/lib?

@byroot
Copy link
Member Author

byroot commented Dec 16, 2025

@nobu I did, no?

@eregon
Copy link
Member

eregon commented Dec 16, 2025

ext/monitor/lib/monitor.rb still exists in your PR

@byroot
Copy link
Member Author

byroot commented Dec 16, 2025

Oh woops.

@byroot byroot force-pushed the core-monitor branch 4 times, most recently from ad78b6e to 32815a1 Compare December 17, 2025 07:21
[Feature #21788]

It allows monitor to access internal routines and remove some overhead.

Before:

```
ruby 4.0.0dev (2025-12-13T04:52:13Z master 71dd272) +YJIT +PRISM [arm64-darwin25]
Warming up --------------------------------------
               Mutex     2.111M i/100ms
             Monitor     1.736M i/100ms
Calculating -------------------------------------
               Mutex     25.050M (± 0.4%) i/s   (39.92 ns/i) -    126.631M in   5.055208s
             Monitor     19.809M (± 0.1%) i/s   (50.48 ns/i) -    100.672M in   5.082015s
```

After:

```
ruby 4.0.0dev (2025-12-13T06:49:18Z core-monitor 6fabf38) +YJIT +PRISM [arm64-darwin25]
Warming up --------------------------------------
               Mutex     2.144M i/100ms
             Monitor     1.859M i/100ms
Calculating -------------------------------------
               Mutex     24.771M (± 0.4%) i/s   (40.37 ns/i) -    124.342M in   5.019716s
             Monitor     23.722M (± 0.4%) i/s   (42.15 ns/i) -    118.998M in   5.016361s
```

Bench:

```ruby
require 'bundler/inline'

gemfile do
  gem "benchmark-ips"
end

mutex = Mutex.new
require "monitor"
monitor = Monitor.new

Benchmark.ips do |x|
  x.report("Mutex") { mutex.synchronize { } }
  x.report("Monitor") { monitor.synchronize { } }
end
```
@byroot byroot marked this pull request as ready for review December 17, 2025 07:39

# Ignore CRuby internals
features -= %w[encdb transdb windows_1252 windows_31j]
features -= %w[encdb transdb windows_1252 windows_31 monitor]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

monitor is not CRuby internal (it's a long-time stdlib/well known feature name), so let's add it under a ruby_version_is "4.1" do guard like pathname below

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i.e. the point is that other Ruby implementations should also to rb_provide() or equivalent so require "monitor" keeps working.

#
# Copyright (C) 2001 Shugo Maeda <shugo@ruby-lang.org>
#
# This library is distributed under the terms of the Ruby license.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not move this whole file in thread_sync.rb or /monitor.rb?

# # exclusive access
# end
#
class Monitor
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this file should define any method on Monitor if Monitor is core.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants