Issue #18042 has been updated by Hanmac (Hans Mackowiak).


motoroller (Iskandar Gohar) wrote:
>  ```ruby 
>  # before  
>  array.map(&:method1).map(&:method2) 
> 
>  # if I know for sure that map is not overridden and calls from Enumerable I can rebuild code like this 
>  array.map do 
>    _1.method1 
>    _1.method2 
>  end 
>  ``` 

there are differences with the code:
* first lets say that array is `[a, b]`
* the first example would call x = a.method1, y = b.method1 and then `x.method2, y.method2`
* the second one would call `a.method1, a.method2` then `b.method1, b.method2`

there is a difference in the order of the method calls, first one would first call all method1 then method2, the second one would call method1 and method2 in sequence

----------------------------------------
Feature #18042: YARV code optimization
https://bugs.ruby-lang.org/issues/18042#change-93007

* Author: motoroller (Iskandar Gohar)
* Status: Feedback
* Priority: Normal
----------------------------------------
Hi! Long period of time I think about programmatically code optimization for YARV. In compiled languages like C/C++ the compiler can do whatever it wants with the code and does for performance optimization. Firstly, ruby developers think about code readability, secondary about performance. Because ruby translates .rb file into bytecode we can do whit this bytecode anything to win in performance and do not lose in the expressiveness of the code. 

 But I came to the conclusion that a static bytecode optimizer is not possible, because in translation stage we don't know about which object/class we use. So, did someone think about runtime code analyzing? If we have some type of statistic we can dynamically transform bytecode to optimized version, use optimized version of C functions. Also I thought if we have statistic we can reduce some GC overhead  

 ```ruby 
 # before  
 array.map(&:method1).map(&:method2) 

 # if I know for sure that map is not overridden and calls from Enumerable I can rebuild code like this 
 array.map do 
   _1.method1 
   _1.method2 
 end 
 ``` 

 In this example 2 `map` calls generate one redundant array which will be destructed by GC, so we can transform it to seconds version. It can be applied not only for `map/select` functions, but many other.  

 What do you think about it? 



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>