List Info

Thread: Ruby performance improvements




Ruby performance improvements
user name
2006-11-13 05:19:34
Michael Selig wrote:
> ----- Original Message -----
> From: "Charles Oliver Nutter"
<charles.nuttersun.com>
> 
> 
>>> 2) Loops & rb_yield()
>>> Most of Ruby's loops end up calling rb_yield()
for each iteration.
> However
>>> much of the work that rb_yield_0 does each time
can be often be done
> once at
>>> the start of the loop:
>>>     - Pushing frame, block, vars, class etc
>>>     - Freeing/creation of dynamic variables
(instead they can be
> initialized
>>> to NIL each time, unless the context has been
saved)
>>> Also loops done by rb_iterate() (via an IFUNC
stub) can be simplified
>>> further to reduce the overhead of
"double-yield".
>> This one I haven't tried, but it's a great idea. I
could modify the
>> yield logic in our ThreadContext to do loops
natively, likely improving
>> the performance of such loops quite a bit. One
concern would be making
>> sure the looped block doesn't capture additional
scope...you'd
>> negatively effect the resulting capture
> The code I wrote checks for this, and if the scope has
been captured, it
> allocates a new dynamic block. Luckily it was very
simple to implement, as
> ruby sets a flag DVAR_DONT_RECYCLE when the scope is
captured.

Interesting...we could probably do the same in JRuby to
reduce the 
overhead of repeat block invocations.

> One other thing that I think is worth doing is to make
variables created in
> a block into "local" variables rather than
"dynamic" if possible, keeping
> dynamic variables for ones that the parser cannot pick
up (eg: created by
> "eval"). Dynamic variables are accessed
significantly slower than local
> ones, because a linear search is done (in 1.8 anyhow).
This change would
> eliminate the unnecessary (and ugly) initialization of
a variable before a
> loop simply to make it "local" and hence
faster. Furthermore it would
> probably eliminate the use of dynamic variables
completely in most loops,
> which would mean that a new block of dynamic variables
could be created
> lazily for extra efficiency. Does YARV address this at
all?

We've done this in JRuby already; all logic for
"dynamic vars" is the 
same as for "local vars", rather than the linear
search done before. I 
believe YARV also addresses this for dvars, since in the
YARV bytecode 
dvars are specified by two numbers: var index and depth. We
have the 
same logic in JRuby today.

-- 
Charles Oliver Nutter, JRuby Core Developer
Blogging on Ruby and Java  headius.blogspot.com
Help spec out Ruby today!  www.headius.com/rubyspec
headiusheadius.com -- charles.nuttersun.com

[1]

about | contact  Other archives ( Real Estate discussion Medical topics )