Skip to content

Commit ba2e06a

Browse files
committed
Use precompute optimization for Rails.env.local?
Ref: 09e0372 This makes #local? behave the same as the predefined environment predicates (they immediately return a precomputed instance variable). Benchmark: ```ruby dev = ActiveSupport::EnvironmentInquirer.new("development") test = ActiveSupport::EnvironmentInquirer.new("test") prod = ActiveSupport::EnvironmentInquirer.new("production") Benchmark.ips do |x| x.report("dev local?") { dev.local? } x.report("test local?") { test.local? } x.report("prod local?") { prod.local? } x.compare! end ``` Before: ``` Warming up -------------------------------------- dev local? 676.645k i/100ms test local? 627.687k i/100ms prod local? 671.078k i/100ms Calculating ------------------------------------- dev local? 6.785M (± 1.8%) i/s - 34.509M in 5.087679s test local? 6.284M (± 2.0%) i/s - 32.012M in 5.096338s prod local? 6.759M (± 1.8%) i/s - 34.225M in 5.065240s Comparison: dev local?: 6785134.0 i/s prod local?: 6759023.6 i/s - same-ish: difference falls within error test local?: 6283910.1 i/s - 1.08x (± 0.00) slower ``` After: ``` Warming up -------------------------------------- dev local? 1.076M i/100ms test local? 1.049M i/100ms prod local? 1.028M i/100ms Calculating ------------------------------------- dev local? 10.586M (± 2.3%) i/s - 53.799M in 5.084729s test local? 10.350M (± 2.5%) i/s - 52.457M in 5.071399s prod local? 10.396M (± 2.2%) i/s - 52.422M in 5.045193s Comparison: dev local?: 10586400.1 i/s prod local?: 10395758.6 i/s - same-ish: difference falls within error test local?: 10350328.8 i/s - same-ish: difference falls within error ```
1 parent 85bc6ca commit ba2e06a

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

activesupport/lib/active_support/environment_inquirer.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ def initialize(env)
2020
DEFAULT_ENVIRONMENTS.each do |default|
2121
instance_variable_set :"@#{default}", env == default
2222
end
23+
24+
@local = in? LOCAL_ENVIRONMENTS
2325
end
2426

2527
DEFAULT_ENVIRONMENTS.each do |env|
@@ -28,7 +30,7 @@ def initialize(env)
2830

2931
# Returns true if we're in the development or test environment.
3032
def local?
31-
in? LOCAL_ENVIRONMENTS
33+
@local
3234
end
3335
end
3436
end

0 commit comments

Comments
 (0)