2009-06-18 2 views
10

active_support.rb에서 사용중인로드 프로세스를 이해하려고했습니다. 이 도구는 세 가지로드 방법, 즉 load_all!, autoloadrequire을 사용합니다. 동일한 파일에서 세 가지 다른로드 방법을 사용하는 이유는 무엇입니까?왜 autoload, load_all입니까! active_support.rb에서 모두 사용해야합니까?

module ActiveSupport 
    def self.load_all! 
    [Dependencies, Deprecation, Gzip, MessageVerifier, Multibyte, SecureRandom, TimeWithZone] 
    end 

    autoload :BacktraceCleaner, 'active_support/backtrace_cleaner' 
    autoload :Base64, 'active_support/base64' 
    autoload :BasicObject, 'active_support/basic_object' 
    autoload :BufferedLogger, 'active_support/buffered_logger' 
    autoload :Cache, 'active_support/cache' 
    autoload :Callbacks, 'active_support/callbacks' 
    autoload :Deprecation, 'active_support/deprecation' 
    autoload :Duration, 'active_support/duration' 
    autoload :Gzip, 'active_support/gzip' 
    autoload :Inflector, 'active_support/inflector' 
    autoload :Memoizable, 'active_support/memoizable' 
    autoload :MessageEncryptor, 'active_support/message_encryptor' 
    autoload :MessageVerifier, 'active_support/message_verifier' 
    autoload :Multibyte, 'active_support/multibyte' 
    autoload :OptionMerger, 'active_support/option_merger' 
    autoload :OrderedHash, 'active_support/ordered_hash' 
    autoload :OrderedOptions, 'active_support/ordered_options' 
    autoload :Rescuable, 'active_support/rescuable' 
    autoload :SecureRandom, 'active_support/secure_random' 
    autoload :StringInquirer, 'active_support/string_inquirer' 
    autoload :TimeWithZone, 'active_support/time_with_zone' 
    autoload :TimeZone, 'active_support/values/time_zone' 
    autoload :XmlMini, 'active_support/xml_mini' 
end 

require 'active_support/vendor' 
require 'active_support/core_ext' 
require 'active_support/dependencies' 
require 'active_support/json' 

I18n.load_path << "#{File.dirname(__FILE__)}/active_support/locale/en.yml" 

답변

16

레일스가 세 가지로드 방법 (실제로는 두 가지 - 아래 참조)을 사용하는 이유를 정확히 알지 못합니다. 그러나 나는 일반적으로 누군가가 왜 그런지를 안다.

Require은 "지금 당장로드"를 의미합니다. autoload은 "필요할 때로드하십시오"를 의미합니다. 두 가지 모두를 사용하는 일반적인 이유는 모든 프로그램 호출에 꽤 많이 사용되는 파일을 가지고 있다는 것입니다. 기타 선택 사항입니다. 예를 들어, 더 이상 사용되지 않는 메소드를 사용하지 않는 Rails 응용 프로그램에서는 Deprecation이 필요하지 않습니다. 그렇다면 왜 파일을로드하여 초기 설정을 느리게합니까?

다른 경우에는 프로그램 실행 초기에 필요한 파일과 대기 할 수있는 파일을 구분할 수 있습니다. 예를 들어 첫 번째 요청이 들어올 때까지 Gzip이 필요하지 않을 수 있습니다. 따라서 자동로드를 사용하면 첫 번째 요청에 약간의 시간이 걸리지 만 초기 설정에서 약간의 시간을 줄일 수 있습니다.

당신은 모든 것을 위해 autoload을 사용하지 않는 것이 좋습니다. 왜 절대적으로 필요하기 전에 을로드해야합니까? 한 가지 이유는 자동로드가 상수에만 작동한다는 것입니다. 예를 들어, active_support/core_ext은 Numeric에 많은 메소드를 추가하므로 3.days, 6.minutes16.seconds.ago과 같은 코드를 작성할 수 있습니다. 3.days에는 상수가 없으므로 해당 식에 자동로드를 트리거 할 수 없습니다. (기본 클래스가 이미로드되었으므로 Numeric을 자동로드 할 수 없습니다. 추가하려는 확장 기능 일뿐입니다.)

마지막으로이 클래스는 실제로 세 가지로드 메소드를 사용하지 않습니다. 그것은 두 가지를 사용하고 하나 (일종의)를 제공합니다. load_all!은 내가 세부 사항을 모르는

# Preload all frameworks specified by the Configuration#frameworks. 
# Used by Passenger to ensure everything's loaded before forking and 
# to avoid autoload race conditions in JRuby. 

Rails::Initializer에 의해 사용되며, 이러한 특정 모듈 (그리고 다른 사람을) 미리로드하는 이유를 모르겠어요. 그러나 이것은 특정 환경을 지원하기위한 것이므로 기본 로딩 메커니즘과 별도로 코드가 필요한 이유를 알 수 있습니다.

1

autoload 루비 방법은 상수를 처음 언급 할 때로드 될 파일 이름과 연관시키는 데 사용할 수 있습니다. 이렇게하면 시작할 때 전체 프레임 워크를로드하지 않아도됩니다.

load_all! 메서드가 레일 initializer.rb에서 호출 된 것처럼 보이며 미리로드되도록 구성된 모든 프레임 워크를 미리로드하는 데 사용됩니다. 이것은 각 프레임 워크 load_all! 메서드를 호출하여 작동합니다.이 메서드는 단순히 상수 배열을 참조하며 ... 자동로드를 트리거합니다. preload_frameworks에 대한 initializer.rb의 주석에 따르면

...

# Preload all frameworks specified by the Configuration#frameworks. 
# Used by Passenger to ensure everything's loaded before forking and 
# to avoid autoload race conditions in JRuby. 

require은 특정 프레임 워크의 핵심에게 필요한 파일을로드하는 것입니다.

관련 문제