OpenSSL causes Rails to load very slowly on Windows

I have a problem with Ruby on Rails, which is very slow. I am using Ruby 2.1.3p242 and Rails 4.2.1 on a computer running Windows 8.

Whenever I run everything that requires loading rails (including tests), it takes a lot of time to start and run. I put some calls in Benchmark in config / environment.rb on a clean rails setup:

require File.expand_path('../application', __FILE__) User cpu System Cpu Total Cpu elapsed time 0.000000 0.000000 0.000000 (0.000000) Rails.application.initialize! 15.282000 2.891000 18.173000 ( 18.201173) 

Clearly, Rails.application.initialize takes an absurdly long time, considering it a clean install.

Thank you in advance for your help.

Edit-1: I work on a dual-core i3 4010u@1.7GHZ with 4 GB of RAM. I donโ€™t think my car is too bad, as it works very well.

Edit-2: I ran ruby-prof on Rails.application.initialize and found the culprit. The process took 85% of the execution time:

 <Module::SecureRandom>#random_bytes <Module::OpenSSL::Random>#random_bytes 

This apparently happens in Ruby21/lib/ruby/2.1.0/securerandom.rb#62 I looked at line 62 in this file, and here is what I found:

 return OpenSSL::Random.random_bytes(n) 

So does anyone know what that means?

+5
source share
3 answers

Edit-2: I ran ruby-prof on Rails.application.initialize and found the culprit. The process took 85% of the execution time:

 <Module::SecureRandom>#random_bytes <Module::OpenSSL::Random>#random_bytes 

Yes, OpenSSL code for seeding a random number generator is problematic on Windows. See Random Windows Issues and Issues on the OpenSSL Wiki.


 return OpenSSL::Random.random_bytes(n) 

So does anyone know what that means?

Ruby returns random numbers. In this case, OpenSSL will log in prior to reconfiguring the random number using RAND_poll , since no other seed was provided.


Ruby should not call RAND_poll or allow it to be implicitly called by the library. If the random number generator has not been seeded, the library is automatically seeded, calling RAND_poll internally.

Rather, Ruby should read bytes from the OS using CryptGenRandom and then call OpenSSL RAND_seed . This avoids calling RAND_poll .

+4
source

Put this in my config/application.rb ( before require 'rails/all' ), speeding up rails s by 10-15 seconds in windows.

 require 'securerandom' SecureRandom.hex(16) 
+1
source

I have been Windows rails dev for some time now. I have never solved the problem of running rails. Running the Rspec microsecond test takes 24 seconds to load on my PC. When I (temporarily) comment out a line in securerandom.rb (all ruby โ€‹โ€‹versions) and replace it with hardcoded return, the startup time will be reduced to 10 seconds.

 #return OpenSSL::Random.random_bytes(n) return "\xD3\x04F\f0\xD6{G\xB9\x81" 
0
source

All Articles