Skip to content
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

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Princemachiavelli
Copy link

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
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

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