laserlemon / figaro

Simple Rails app configuration

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Figaro::ENV#get_value method is not very efficient

steventen opened this issue · comments

Hi, there

I've noticed that the Figaro::ENV#get_value method uses a loop to find the variable every time.

This is very slow, especially when the size of ENV is very large. The ENV is a hash-like variable that allows us to do a O(1) lookup.

I think we can change the method into something this:

def get_value(key)
  ::ENV[key] || ::ENV[key.upcase]
end

I also noticed #189 mentioned the possible memory leak of current code. If we change to the code above, it should avoid the issue too.

As mentioned in #189, this breaks true case insensitivity for ENV variable lookup. Your example implementation of get_value would not find an ENV variable named Foo_Bar when calling Figaro.env.foo_bar. This may be a reasonable change but it is a breaking one so we'll need to consider the consequences.

Sorry, I didn't get it. If you send foo_bar, the code ::ENV[key.upcase] will be able to find the env variable Foo_Bar right?

No, in that case it would look for ::ENV["FOO_BAR"] instead of finding ::ENV["Foo_Bar"].

ahh, I see. How did I miss the difference ....

@steventen I'm looking over current issues and I'm very concerned about this issue. Thank you for uncovering it. I'd love if you could try to take the reins on what the best solution would be in this situation. An efficient and backwards-compatible solution would be best but I'm open to a breaking change if that's what's needed to fix the issue.

Hi @laserlemon

Thanks for considering this issue. I think it ends up with a decision on either we want better flexibility or better performance.

For us, we always use all uppercase for environment variable names. And I think most of people should be either using all uppercase or all lowercase. If my assumption is correct, then I think it's better to use my solution to better serve the 99% of people.