# Code Review: LazyMethods

**URL:** <https://www.ruby-forum.com/t/code-review-lazymethods/169540>\
**Category:** IronRuby\
**Created:** [June 5, 2009, 12:32am UTC](https://www.ruby-forum.com/t/code-review-lazymethods/169540 "2009-06-05T00:32:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![grujildo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/94ad74/32.png) [@grujildo](https://www.ruby-forum.com/u/grujildo)\
**Post date:** [June 5, 2009, 12:32am UTC](https://www.ruby-forum.com/t/code-review-lazymethods/169540/1 "2009-06-05T00:32:57Z")

</div>

tfpt review “/shelveset:LazyMethods;REDMOND\tomat”

A simple change in DLR.

Ruby:  
Implements lazy method transformation. Previously a method was  
transformed to DLR AST as soon as it was defined (def foo; …; end). We  
can postpone the transformation until the method is called for the first  
time. This significantly improves startup time. For example (not  
NGEN’d):

require ‘benchmark’

Benchmark.bm { |x| x.report { require ‘rubygems’ } }

```
    user system total real

```

eager transformation  
1.622410 0.031200 1.653611 ( 1.581316)  
lazy transformation  
1.170008 0.031200 1.201208 ( 1.099220)

Although Ruby methods (unlike block) don’t close over variables we  
still need 2 closure variables: the parent lexical runtime scope and the  
module that the method is declared in (for implementation of super).  
These were previously DLR closure variables. They are live constants  
now. Ruby method pre-compilation would require additional work, so we  
don’t support it for now.

Tomas

---

<div class="post-metadata">

**Author:** ![grujildo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/94ad74/32.png) [@grujildo](https://www.ruby-forum.com/u/grujildo)\
**Post date:** [June 5, 2009, 5:44pm UTC](https://www.ruby-forum.com/t/code-review-lazymethods/169540/2 "2009-06-05T17:44:07Z")

</div>

Looks good overall.

Should RubyContext.CallSiteCreated be ThreadLocal? If not, its use by  
CallSiteTracer looks not-entirely-safe.

The variable name “parantScope” in RubyOps.CreateMethodScope is typoed.

---

<div class="post-metadata">

**Author:** ![grujildo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/94ad74/32.png) [@grujildo](https://www.ruby-forum.com/u/grujildo)\
**Post date:** [June 5, 2009, 7:53pm UTC](https://www.ruby-forum.com/t/code-review-lazymethods/169540/3 "2009-06-05T19:53:51Z")

</div>

Good catch on thread-safety. This delegate is actually experimental  
(demo-ware :)) so it doesn’t matter so much. I’ll add a comment that  
thread-safety needs to be considered.

Tomas
