Skip to content

Enhance the performance of the frontend JSON codec #6816

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 2 commits into from
Jun 19, 2025

Conversation

damnever
Copy link
Contributor

@damnever damnever commented Jun 13, 2025

What this PR does:

Stream json encoding using existing functions, reducing CPU usage ~50%.

Which issue(s) this PR fixes:

After upgrading from v1.16 to v1.19, the frontend's CPU usage increased significantly. The primary cause appears to be related to JSON codec, the use of encoding/json and the read-and-parse process. This patch addresses both issues. However, despite applying the patch, CPU usage remains higher than it was prior to the upgrade. The exact reason for this is unclear, maybe it is due to Go 1.24 or something else, but profiling results still show that decoding labels continues to consume a substantial amount of processing time (profile below).

Clipboard_Screenshot_1749810703

Clipboard_Screenshot_1749810067

Checklist

  • Tests updated
  • Documentation added
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]

Copy link
Contributor

@yeya24 yeya24 left a comment

Choose a reason for hiding this comment

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

Thanks. I think this change makes sense!
Maybe @alanprot can also take a look

@@ -25,6 +25,7 @@ import (
"github.com/prometheus/prometheus/util/jsonutil"
"github.com/weaveworks/common/httpgrpc"

_ "github.com/cortexproject/cortex/pkg/chunk" // Register jsoniter type: labels.Labels.
Copy link
Contributor

Choose a reason for hiding this comment

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

I am wondering if we have a better package to import? In case we remove some code from that chunk package.

Copy link
Member

Choose a reason for hiding this comment

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

maybe we can just something like this?

var _ = labels.Labels{}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

maybe we can reimplement the codec for cortexpb.LabelAdapter and use it directly

Copy link
Contributor

Choose a reason for hiding this comment

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

I am worried that at sometime we just remove that code path from the package without noticing and we have the regression.

We don't have to do it in this PR but let's at least add some TODO or notes for re-implementing the codec or something similar. Creating an issue also works

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can export the legacy functions directly and use them without relying on global registration.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

@alanprot alanprot left a comment

Choose a reason for hiding this comment

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

Thanks!

@@ -25,6 +25,7 @@ import (
"github.com/prometheus/prometheus/util/jsonutil"
"github.com/weaveworks/common/httpgrpc"

_ "github.com/cortexproject/cortex/pkg/chunk" // Register jsoniter type: labels.Labels.
Copy link
Member

Choose a reason for hiding this comment

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

maybe we can just something like this?

var _ = labels.Labels{}

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Jun 17, 2025
@alanprot
Copy link
Member

Just out of curiosity, can you also show the impact in memory on this?

@damnever
Copy link
Contributor Author

@alanprot
Clipboard_Screenshot_1750214442

Signed-off-by: Xiaochao Dong (@damnever) <the.xcdong@gmail.com>
@damnever damnever force-pushed the perf/frontendcodec branch from 6c36bd4 to 5840e00 Compare June 18, 2025 02:44
@pull-request-size pull-request-size bot added size/M and removed size/S labels Jun 18, 2025
Signed-off-by: Xiaochao Dong (@damnever) <the.xcdong@gmail.com>
@damnever damnever force-pushed the perf/frontendcodec branch from 08c7800 to da7343d Compare June 18, 2025 09:46
@yeya24
Copy link
Contributor

yeya24 commented Jun 18, 2025

@damnever For the graph you shared, when was the change deployed and how much it impacted memory?

@damnever
Copy link
Contributor Author

@yeya24 the middle of 06/13 12:00 - 06/14 00:00, peak memory decreased significantly, possibly by 70%

@yeya24 yeya24 merged commit fbc197f into cortexproject:master Jun 19, 2025
31 of 32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/query-frontend lgtm This PR has been approved by a maintainer size/M type/performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants