Skip to content

Commit 9fed43d

Browse files
committed
(PUP-11348) compare-then-set is actually slower
Always resetting the environment is faster the compare-then-set in cases where the environment was changed and not. ``` $ cat bench.rb require 'benchmark/ips' Benchmark.ips do |x| unchanged = ENV.to_hash changed = ENV.to_hash.merge!("foo" => "bar") x.report("test-set unchanged") { ENV.replace(unchanged) if ENV.to_hash != unchanged } x.report("test-set changed") { ENV.replace(unchanged) if ENV.to_hash != changed } x.report("set unchanged") { ENV.replace(unchanged) } x.report("set changed") { ENV.replace(changed) } x.compare! end $ ruby --version ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux] $ ruby bench.rb Warming up -------------------------------------- test-set unchanged 4.711k i/100ms test-set changed 2.691k i/100ms set unchanged 5.639k i/100ms set changed 5.480k i/100ms Calculating ------------------------------------- test-set unchanged 46.809k (± 1.1%) i/s - 235.550k in 5.032833s test-set changed 26.749k (± 0.6%) i/s - 134.550k in 5.030353s set unchanged 55.451k (± 0.9%) i/s - 281.950k in 5.085048s set changed 54.355k (± 0.8%) i/s - 274.000k in 5.041297s Comparison: set unchanged: 55451.4 i/s set changed: 54354.6 i/s - 1.02x slower test-set unchanged: 46808.7 i/s - 1.18x slower test-set changed: 26748.7 i/s - 2.07x slower ```
1 parent b6d99fb commit 9fed43d

File tree

1 file changed

+2
-7
lines changed

1 file changed

+2
-7
lines changed

lib/puppet/test/test_helper.rb

+2-7
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,8 @@ def self.after_each_test()
182182
end
183183
$saved_indirection_state = nil
184184

185-
# Restore the global process environment. Can't just assign because this
186-
# is a magic variable, sadly, and doesn't do that™. It is sufficiently
187-
# faster to use the compare-then-set model to avoid excessive work that it
188-
# justifies the complexity. --daniel 2012-03-15
189-
if ENV.to_hash != $old_env
190-
ENV.replace($old_env)
191-
end
185+
# Restore the global process environment.
186+
ENV.replace($old_env)
192187

193188
# Clear all environments
194189
Puppet.lookup(:environments).clear_all

0 commit comments

Comments
 (0)