phpdotenv icon indicating copy to clipboard operation
phpdotenv copied to clipboard

Performance degradation

Open makstech opened this issue 1 year ago • 3 comments

Some time ago we were performing updates to all our packages, including the dotenv, where we went from v2 to v5. And later we noticed that the CPU usage and all request times have increased substantially, even though we use pretty beefy instances. On 8 core instances, CPU usage increased by >20%.

After some investigation, we found out that the dotenv load times have skyrocketed and memory consumption has gone up quite a lot too. The primary cause of regression was pinned down to this method https://github.com/vlucas/phpdotenv/blob/1a7ea2afc49c3ee6d87061f5a233e3a035d0eae7/src/Parser/EntryParser.php#L46-L60 Where now, to parse one .env line, it needs to create a bunch of classes and then the final entry is at least 2 classes that are kept in memory. Compared to a simple array<string, string> in v2.

Our .env is 2k lines long of which around 400 are empty lines and another 300 are comments.
So we downgraded back to v2 and the difference is huge, especially during peak hours.

For example, in one simple and fast route, during peak hours, the difference between v2 and v5 is around 80ms per request. On a route that has around 65k req/h (1m+ req/day) that makes more than 80 minutes of extra processing time in an hour, just on this route. Here's an example of that route after the downgrade performed on the 8th of Feb. No other changes to the code or infrastructure were done. image

I know, the obvious solution would be some sort of caching. But is there really a need for so much more processing and objectifying?

makstech avatar Feb 14 '23 15:02 makstech