Misc #18830
Updated by k0kubun (Takashi Kokubun) almost 3 years ago
## 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 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.
* Nobody really uses MJIT on MSWIN. If the maintenance cost becomes too high because of the MSWIN-specific implementation introduced by this change, it's okay to just remove the support.
---
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
```