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

Small optimization to PrometheusResponse marshalling #3964

Conversation

pracucci
Copy link
Contributor

What this PR does:
I took a quick look at #3963 to see if there was any low-hanging fruit. In this PR I'm reducing the memory allocations when deserializing the PrometheusResponse. Not a big win, but still worthwhile.

name                               old time/op    new time/op    delta
PrometheusCodec_DecodeResponse-12     246ms ± 2%     241ms ± 0%   -1.95%  (p=0.008 n=5+5)
PrometheusCodec_EncodeResponse-12     314ms ±10%     308ms ± 2%     ~     (p=1.000 n=5+5)

name                               old alloc/op   new alloc/op   delta
PrometheusCodec_DecodeResponse-12     134MB ± 0%      96MB ± 0%  -28.31%  (p=0.008 n=5+5)
PrometheusCodec_EncodeResponse-12     229MB ±26%     248MB ±32%     ~     (p=0.690 n=5+5)

name                               old allocs/op  new allocs/op  delta
PrometheusCodec_DecodeResponse-12     31.1k ± 0%     31.0k ± 0%   -0.05%  (p=0.008 n=5+5)
PrometheusCodec_EncodeResponse-12     2.01M ± 0%     2.01M ± 0%     ~     (p=0.651 n=5+5)

I've manually tested it both with query-frontend and query-scheduler, adding a temporarily log to see if ContentLength is valued correctly in prometheusCodec.DecodeResponse() and it is.

Which issue(s) this PR fixes:
N/A

Checklist

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

Signed-off-by: Marco Pracucci <[email protected]>
Copy link
Contributor

@bboreham bboreham left a comment

Choose a reason for hiding this comment

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

Good stuff!

matrix = model.ValMatrix.String()
json = jsoniter.Config{
EscapeHTML: false, // No HTML in our responses.
SortMapKeys: true,
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this required?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think it's required, but I wasn't able to figure out if we may introduce any regression disabling it (the default config has it enabled). Any thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

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

I expect some clients are relying on it whether they realise it or not.
I.e. the ordering of series here maps directly to what you see in your dashboard, in at least some cases.

However we should try disabling it (in a separate change) and see if anyone notices.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Opened an issue to follow up about it:
#3997

@pracucci pracucci merged commit 5b7c076 into cortexproject:master Mar 23, 2021
@pracucci pracucci deleted the optimize-prometheus-response-marshalling branch March 23, 2021 08:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants