ruby-changes:63330
From: tompng <ko1@a...>
Date: Mon, 12 Oct 2020 14:21:20 +0900 (JST)
Subject: [ruby-changes:63330] e8d03c9a2a (master): change rb_ractor_queue to ring buffer
https://git.ruby-lang.org/ruby.git/commit/?id=e8d03c9a2a From e8d03c9a2a58054221393e39f511fa76fe9b9770 Mon Sep 17 00:00:00 2001 From: tompng <tomoyapenguin@g...> Date: Wed, 7 Oct 2020 17:51:19 +0900 Subject: change rb_ractor_queue to ring buffer diff --git a/ractor.c b/ractor.c index a8b12c7..5bc65a0 100644 --- a/ractor.c +++ b/ractor.c @@ -159,8 +159,9 @@ static void https://github.com/ruby/ruby/blob/trunk/ractor.c#L159 ractor_queue_mark(struct rb_ractor_queue *rq) { for (int i=0; i<rq->cnt; i++) { - rb_gc_mark(rq->baskets[i].v); - rb_gc_mark(rq->baskets[i].sender); + int idx = (rq->start + i) % rq->size; + rb_gc_mark(rq->baskets[idx].v); + rb_gc_mark(rq->baskets[idx].sender); } } @@ -293,6 +294,7 @@ ractor_queue_setup(struct rb_ractor_queue *rq) https://github.com/ruby/ruby/blob/trunk/ractor.c#L294 { rq->size = 2; rq->cnt = 0; + rq->start = 0; rq->baskets = malloc(sizeof(struct rb_ractor_basket) * rq->size); } @@ -311,12 +313,9 @@ ractor_queue_deq(rb_ractor_t *r, struct rb_ractor_queue *rq, struct rb_ractor_ba https://github.com/ruby/ruby/blob/trunk/ractor.c#L313 RACTOR_LOCK(r); { if (!ractor_queue_empty_p(r, rq)) { - // TODO: use good Queue data structure - *basket = rq->baskets[0]; + *basket = rq->baskets[rq->start]; rq->cnt--; - for (int i=0; i<rq->cnt; i++) { - rq->baskets[i] = rq->baskets[i+1]; - } + rq->start = (rq->start + 1) % rq->size; b = true; } else { @@ -334,10 +333,13 @@ ractor_queue_enq(rb_ractor_t *r, struct rb_ractor_queue *rq, struct rb_ractor_ba https://github.com/ruby/ruby/blob/trunk/ractor.c#L333 ASSERT_ractor_locking(r); if (rq->size <= rq->cnt) { + rq->baskets = realloc(rq->baskets, sizeof(struct rb_ractor_basket) * rq->size * 2); + for (int i=rq->size - rq->start; i<rq->cnt; i++) { + rq->baskets[i + rq->start] = rq->baskets[i + rq->start - rq->size]; + } rq->size *= 2; - rq->baskets = realloc(rq->baskets, sizeof(struct rb_ractor_basket) * rq->size); } - rq->baskets[rq->cnt++] = *basket; + rq->baskets[(rq->start + rq->cnt++) % rq->size] = *basket; // fprintf(stderr, "%s %p->cnt:%d\n", __func__, rq, rq->cnt); } diff --git a/ractor.h b/ractor.h index e9acf82..4cd8952 100644 --- a/ractor.h +++ b/ractor.h @@ -25,6 +25,7 @@ struct rb_ractor_basket { https://github.com/ruby/ruby/blob/trunk/ractor.h#L25 struct rb_ractor_queue { struct rb_ractor_basket *baskets; + int start; int cnt; int size; }; -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/