Thanks to visit codestin.com
Credit goes to github.com

Skip to content

APCng storage - avoid worst-case memory usage in buildPermutationTree #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 5, 2025

Conversation

Princemachiavelli
Copy link
Contributor

The peak memory usage of the APCng storage adapter while collecting the metrics for display is currently always the worst-case for the cardinality of the metric label values.

This change replaces the buildPermutationTree implementation with a (cartesian product) generator which avoids keeping every metric label value permutation in memory at once. This can reduce peak memory usage by orders of magnitude for metrics with high cardinality but sparse actual usage. This helps avoid exceeding the PHP memory_limit.

Besides the reduced memory usage, I haven't seen much of a difference in how long buildPermutationTree takes to run. (To improve that, only the actually used metric label pairs could be to tracked separately to avoid building the permutations completely.)

@@ -539,10 +530,9 @@ private function getValues(string $type, array $metaData): array /** @phpstan-ig
if (isset($metaData['buckets'])) {
$metaData['buckets'][] = 'sum';
$labels[] = $metaData['buckets'];
$metaData['labelNames'][] = '__histogram_buckets';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since I don't use the number of label names anymore, I removed that parameter from buildPermutationTree and removed this line as it seemed unused. I don't see any change in the output for histograms but if this actually used then I can re-add it.

@mlebrun
Copy link

mlebrun commented Jul 27, 2023

This would be great to land, we are hitting the PHP limit as well.

@xocasdashdash
Copy link

I've tested a copy of this PR and it works well to reduce memory usage with histograms

Copy link
Member

@LKaemmerling LKaemmerling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you a lot! Great change! Sorry for the really long review time. I just fixed a few phpstan issues and will merge it when the tests are green :)!

@LKaemmerling LKaemmerling merged commit 3e4811f into PromPHP:main Mar 5, 2025
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants