Project

General

Profile

Actions

Bug #17772

open

Performance issue with NameError message in context with large instance variables (not caused by did_you_mean)

Added by Confusion (Ivo Wever) 3 months ago. Updated 3 months ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-linux]
[ruby-core:103158]

Description

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?

Actions

Also available in: Atom PDF