Skip to content

Commit 6e61496

Browse files
authored
Pre-allocate IO.select result arrays based on input size (ruby#15850)
io.c: pre-allocate IO.select result arrays based on input size The ternary (rp?rb_ary_new():rb_ary_new2(0)) became pointless after commit a51f30c (Variable Width Allocation, Mar 2022) made both rb_ary_new() and rb_ary_new2(0) equivalent. Instead of just removing the dead code, improve on the original intent by pre-allocating based on the actual input array size. This avoids reallocations when many FDs are ready. Benchmark (100 ready FDs): ~8% improvement (5.59 -> 5.11 us/op)
1 parent 65a4845 commit 6e61496

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

io.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10758,9 +10758,9 @@ select_internal(VALUE read, VALUE write, VALUE except, struct timeval *tp, rb_fd
1075810758
if (!pending && n == 0) return Qnil; /* returns nil on timeout */
1075910759

1076010760
res = rb_ary_new2(3);
10761-
rb_ary_push(res, rp?rb_ary_new():rb_ary_new2(0));
10762-
rb_ary_push(res, wp?rb_ary_new():rb_ary_new2(0));
10763-
rb_ary_push(res, ep?rb_ary_new():rb_ary_new2(0));
10761+
rb_ary_push(res, rp ? rb_ary_new_capa(RARRAY_LEN(read)) : rb_ary_new());
10762+
rb_ary_push(res, wp ? rb_ary_new_capa(RARRAY_LEN(write)) : rb_ary_new());
10763+
rb_ary_push(res, ep ? rb_ary_new_capa(RARRAY_LEN(except)) : rb_ary_new());
1076410764

1076510765
if (rp) {
1076610766
list = RARRAY_AREF(res, 0);

0 commit comments

Comments
 (0)