aws/aws-sdk-ruby — pull request #478 by kierarad (2014-02-27 19:42:41+00:00)
Title: Fix a performance issue when calling CredentialProviders::DefaultProvider#credentials

In our EC2 instance (using instance profile) we found that it takes about 4 milliseconds to call this method. We did some profiling and found that the DefaultProvider uses raise and rescue MissingCredentialsError to detect which individual provider provides credentials. The Kernel#raise incurs a large performance cost. So we changed the flow to use a simple nil check, and found a big performance difference when the stack level is reasonably deep.

Benchmark code:
```
def simulate_stack_depth(times, &block)
if times > 0
simulate_stack_depth(times - 1, &block)
else
yield
end
end

provider = AWS::Core::CredentialProviders::DefaultProvider.new
10000.times { provider.credentials } #warm up
simulate_stack_depth(400) do
puts Benchmark.measure { 10000.times { provider.credentials } }
end
```

On a m3.large EC2 instance with instance profile:
```
ruby-1.9.3
exception flow: 7.820000 0.070000 7.890000 ( 7.888295)
nil check flow: 0.160000 0.020000 0.180000 ( 0.181731)

jruby-1.7.9
exception flow: 40.240000 0.040000 40.280000 ( 39.859000)
nil check flow: 0.700000 0.060000 0.760000 ( 0.424000)
```