From: rafbmasson@... Date: 2015-08-16T06:46:06+00:00 Subject: [ruby-core:70403] [Ruby trunk - Bug #11448] [Open] Requiring a given library may take 23x longer depending on `require` context Issue #11448 has been reported by Rafa��l Blais Masson. ---------------------------------------- Bug #11448: Requiring a given library may take 23x longer depending on `require` context https://bugs.ruby-lang.org/issues/11448 * Author: Rafa��l Blais Masson * Status: Open * Priority: Normal * Assignee: * ruby -v: ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin14] * Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN ---------------------------------------- I was trying to narrow down the longest things to require during Rails boot when I stumbled on this. Add two lines in `securerandom.rb` in order to output the time taken to `require 'openssl'`: ~~~ # -*- coding: us-ascii -*- begin + start = Time.now require 'openssl' + puts Time.now - start rescue LoadError end # == Secure random number generator interface. # ... ~~~ Now run a script containing just this: ~~~ require 'securerandom' ~~~ Average output: `0.038829` But run a similar script that requires a higher-level library, like: ~~~ require 'action_mailer' ~~~ Average output: `0.907959` This still measures the time taken to `require 'openssl'` only. Why is it 23x times slower? Because it is nested within other `require` calls? When requiring `action_mailer`, this is the tree of files required to get to `securerandom`: ~~~ require 'action_mailer' require 'abstract_controller' require 'active_support/rails' require 'active_support/deprecation' require 'active_support/deprecation/behaviors' require 'active_support/notifications' require 'active_support/notifications/instrumenter' require 'securerandom' ~~~ When starting one level down, time to `require 'openssl'` is cut in half: ~~~ require 'abstract_controller' ~~~ Average output: `0.465274` -- https://bugs.ruby-lang.org/