Issue #17772 has been reported by Confusion (Ivo Wever).

----------------------------------------
Bug #17772: Performance issue with NameError message in context with large instance variables (not caused by did_you_mean)
https://bugs.ruby-lang.org/issues/17772

* Author: Confusion (Ivo Wever)
* Status: Open
* Priority: Normal
* ruby -v: ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-linux]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: UNKNOWN
----------------------------------------
The following script takes about 2 seconds (tested on 2.6.6 and 2.7.2) to get from 'rescue' to 'ensure' on my machine, when executed as `ruby --disable-did_you_mean script.rb`. The time is taken by getting the `e.message`.

```
class Environment                                                               
  def run                                                                       
    @data = (1..5_000_000).to_h {|x| [x, x]}                                    
    trigger_name_error                                                          
  rescue Exception => e                                                         
    start = Time.now                                                            
    e.message                                                             
  ensure                                                                                                                                                                     
    puts "In ensure after #{Time.now - start}"                                  
  end                                                                           
end                                                                             
                                                                                
Environment.new.run                
```

The time is much lower if you:
* change the instance variable to a local variable
* change #run to a class method 
* trigger something other than a NameError

The time is roughly linear in the size of @data.

The time roughly doubles if you enable `did_you_mean`.


Since I'm regularly operating in such contexts with large instance variables, I noted the slowness of NameErrors and went looking for the cause, whittling it down to the above script. Not sure if this should be considered a bug, but I guess it is probably undesirable?



-- 
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>