ruby-changes:60085
From: Masataka <ko1@a...>
Date: Sun, 16 Feb 2020 02:37:33 +0900 (JST)
Subject: [ruby-changes:60085] 5278294230 (master): Reduce begin-less/end-less range allocation
https://git.ruby-lang.org/ruby.git/commit/?id=5278294230 From 527829423088f09cf2f708be12bb4337d640dc69 Mon Sep 17 00:00:00 2001 From: Masataka Pocke Kuwabara <kuwabara@p...> Date: Sun, 16 Feb 2020 00:09:39 +0900 Subject: Reduce begin-less/end-less range allocation ``` $ cat test.yaml prelude: | def endless 1.. end def beginless ..1 end def endless_substr(str) str[1..] end benchmark: endless: endless beginless: beginless endless_substr: "endless_substr('foo')" $ RBENV_VERSION=trunk ruby -v ruby 2.8.0dev (2020-02-15T12:52:03Z master 961630126b) [x86_64-linux] $ RBENV_VERSION=patched ruby -v ruby 2.8.0dev (2020-02-15T12:52:03Z origin/master 961630126b) [x86_64-linux] $ benchmark-driver test.yaml --rbenv 'patched;trunk' Warming up -------------------------------------- endless 45.948M i/s - 46.076M times in 1.002782s (21.76ns/i, 26clocks/i) beginless 49.986M i/s - 50.237M times in 1.005037s (20.01ns/i, 24clocks/i) endless_substr 8.067M i/s - 8.187M times in 1.014936s (123.96ns/i, 148clocks/i) Calculating ------------------------------------- patched trunk endless 115.679M 21.500M i/s - 137.843M times in 1.191597s 6.411398s beginless 112.599M 22.060M i/s - 149.957M times in 1.331778s 6.797768s endless_substr 8.888M 6.760M i/s - 24.201M times in 2.722995s 3.580038s Comparison: endless patched: 115679391.9 i/s trunk: 21499711.2 i/s - 5.38x slower beginless patched: 112598731.5 i/s trunk: 22059673.0 i/s - 5.10x slower endless_substr patched: 8887513.1 i/s trunk: 6759886.2 i/s - 1.31x slower ``` diff --git a/compile.c b/compile.c index e9ce106..5800a6e 100644 --- a/compile.c +++ b/compile.c @@ -5178,9 +5178,9 @@ compile_named_capture_assign(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE https://github.com/ruby/ruby/blob/trunk/compile.c#L5178 } static int -number_literal_p(const NODE *n) +optimizable_range_item_p(const NODE *n) { - return (n && nd_type(n) == NODE_LIT && RB_INTEGER_TYPE_P(n->nd_lit)); + return (n && nd_type(n) == NODE_LIT && RB_INTEGER_TYPE_P(n->nd_lit)) || (n && nd_type(n) == NODE_NIL); } static int @@ -8307,10 +8307,12 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, in https://github.com/ruby/ruby/blob/trunk/compile.c#L8307 VALUE flag = INT2FIX(excl); const NODE *b = node->nd_beg; const NODE *e = node->nd_end; - if (number_literal_p(b) && number_literal_p(e)) { + if (optimizable_range_item_p(b) && optimizable_range_item_p(e)) { if (!popped) { - VALUE val = rb_range_new(b->nd_lit, e->nd_lit, excl); - ADD_INSN1(ret, line, putobject, val); + VALUE bv = nd_type(b) == NODE_LIT ? b->nd_lit : Qnil; + VALUE ev = nd_type(e) == NODE_LIT ? e->nd_lit : Qnil; + VALUE val = rb_range_new(bv, ev, excl); + ADD_INSN1(ret, line, putobject, val); RB_OBJ_WRITTEN(iseq, Qundef, val); } } -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/