Skip to content

Instantly share code, notes, and snippets.

@osyoyu
Created October 12, 2024 05:46
Show Gist options
  • Select an option

  • Save osyoyu/553a84fa035dd40405119fb71962344c to your computer and use it in GitHub Desktop.

Select an option

Save osyoyu/553a84fa035dd40405119fb71962344c to your computer and use it in GitHub Desktop.
Ruby の GVL をなくすと遅くなる
GC.disable
def sieve_of_eratosthenes(limit)
is_prime = Array.new(limit + 1, true)
is_prime[0] = is_prime[1] = false # 0 と 1 は素数ではない
(2..Math.sqrt(limit)).each do |i|
if is_prime[i]
(i * i..limit).step(i) do |j|
is_prime[j] = false
end
end
end
is_prime.each_with_index.map { |prime, index| index if prime }.compact
end
ths = []
10.times do
ths << Thread.new {
sieve_of_eratosthenes(1000000)
}
end
ths.each(&:join)
diff --git a/thread.c b/thread.c
index 2a937ca278..684efea3bc 100644
--- a/thread.c
+++ b/thread.c
@@ -1445,6 +1445,7 @@ rb_thread_sleep(int sec)
static void
rb_thread_schedule_limits(uint32_t limits_us)
{
+ return;
if (!rb_thread_alone()) {
rb_thread_t *th = GET_THREAD();
RUBY_DEBUG_LOG("us:%u", (unsigned int)limits_us);
diff --git a/thread_pthread.c b/thread_pthread.c
index c92fd52a66..8f39530581 100644
--- a/thread_pthread.c
+++ b/thread_pthread.c
@@ -384,6 +384,8 @@ ractor_sched_dump_(const char *file, int line, rb_vm_t *vm)
static void
thread_sched_lock_(struct rb_thread_sched *sched, rb_thread_t *th, const char *file, int line)
{
+ // printf("lo: th:%p\n", th);
+ return;
rb_native_mutex_lock(&sched->lock_);
#if VM_CHECK_MODE
@@ -398,6 +400,8 @@ thread_sched_lock_(struct rb_thread_sched *sched, rb_thread_t *th, const char *f
static void
thread_sched_unlock_(struct rb_thread_sched *sched, rb_thread_t *th, const char *file, int line)
{
+ // printf("un: th:%p\n", th);
+ return;
RUBY_DEBUG_LOG2(file, line, "th:%u", rb_th_serial(th));
#if VM_CHECK_MODE
% time /home/osyoyu/.rbenv/versions/master/bin/ruby ~/Development/bench.rb
/home/osyoyu/.rbenv/versions/master/bin/ruby ~/Development/bench.rb 0.99s user 0.07s system 99% cpu 1.058 total
% time ./miniruby ~/Development/bench.rb
./miniruby ~/Development/bench.rb 17.68s user 0.11s system 904% cpu 1.969 total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment