Project

General

Profile

Actions

Misc #18830

closed

Remove MJIT worker thread

Added by k0kubun (Takashi Kokubun) almost 2 years ago. Updated almost 2 years ago.

Status:
Closed
Assignee:
-
[ruby-core:108925]

Description

Proposal

  • Remove MJIT worker thread
    • Compiling ISeq to C should synchronously happen in Ruby threads
    • Unix platforms asynchronously wait for a C compiler process to finish using SIGCHLD, and MSWIN synchronously waits for a C compiler process to finish.

Note: The architecture is similar to what we did for the timer thread [Misc #14937]. Removing the MJIT worker thread using SIGCHLD is what @normalperson (Eric Wong) proposed before too.

Motivation

  • It's hard to maintain a JIT compiler if it can't touch Ruby VM. Many C functions in CRuby rely on the VM behind the scenes.
  • It's hard to maintain a JIT compiler if it can't trigger GC. Not only you cannot use xmalloc, but also you have to fight race conditions with GC and SEGV caused by them.

All of these problems disappear if you run the JIT compiler in the same Ruby thread. YJIT does so too.

Assumptions

  • The bottleneck of MJIT compilation is the time taken to wait for a C compiler process to finish. Thus compiling ISeq to C will not be a bottleneck. Potentially, this cost could be insignificant even if we rewrite the entire ISeq -> C compiler in Ruby.
  • MSWIN users currently don't use MJIT. When they do, we could do something like Process.spawn and asynchronously wait for the process by periodically scheduling interrupt checks.
  • We could also address the problem by running MJIT worker as a Ractor. However, enabling the multi-Ractor mode alone incurs some performance overhead. We actually tried it https://github.com/ruby/ruby/pull/4024, and it was meaningfully slower than the current master and this ticket's patch.

Patch: https://github.com/ruby/ruby/pull/6006

The motivation for this change is not about improving performance by itself, but this makes it slightly faster.

$ benchmark-driver benchmark.yml --rbenv 'before --mjit;after --mjit' -v --repeat-count=12 --output=all --alternate
before --mjit: ruby 3.2.0dev (2022-06-15T05:42:09Z master 46706e7808) +MJIT [x86_64-linux]
after --mjit: ruby 3.2.0dev (2022-06-15T07:21:31Z no-mjit-worker 90433da34d) +MJIT [x86_64-linux]
Calculating -------------------------------------
                                before --mjit          after --mjit
Optcarrot Lan_Master.nes    125.3789474179577     129.6361842567196 fps
                            125.7892049665125     130.1237905211524
                            126.3359252053467     131.3352551857975
                            127.2303480033240     131.5128102697965
                            127.2368143596796     131.9221842055012
                            127.3938219177180     131.9712318537562
                            127.8370994014408     132.0527716303878
                            127.9904178998168     132.4032777876095
                            128.0631040346902     132.4425814311906
                            128.3016029706804     132.5687514040249
                            128.8733290749460     132.7843477576762
                            129.5397101458566     133.4416009274654
Actions #1

Updated by k0kubun (Takashi Kokubun) almost 2 years ago

  • Description updated (diff)

Updated by kaiquekandykoga (Kaíque Koga) almost 2 years ago

I am currently using Ruby with FreeBSD, macOS and GNU/Linux.

In the past I had to run Ruby with https://github.com/rails-sqlserver/tiny_tds in a Windows Server. About 4 years ago.

Updated by k0kubun (Takashi Kokubun) almost 2 years ago

Thank you for sharing your past usage of Ruby on Windows. I'll keep the MSWIN support during the changes of this ticket anyway, but note that you might not have been using MSWIN. If you installed Ruby with RubyInstaller2, it's MinGW. In fact, the AppVeyor CI of tiny_tds repository uses MinGW.

Actions #4

Updated by k0kubun (Takashi Kokubun) almost 2 years ago

  • Description updated (diff)
Actions #5

Updated by k0kubun (Takashi Kokubun) almost 2 years ago

  • Description updated (diff)
Actions #6

Updated by k0kubun (Takashi Kokubun) almost 2 years ago

  • Description updated (diff)
Actions #7

Updated by k0kubun (Takashi Kokubun) almost 2 years ago

  • Status changed from Open to Closed

Applied in changeset git|1162523bae926cfa6128043b635e28c14b732754.


Remove MJIT worker thread (#6006)

[Misc #18830]

Actions #8

Updated by k0kubun (Takashi Kokubun) almost 2 years ago

  • Description updated (diff)
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0