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

Save memory on histograms: slightly faster with less code #536

Merged
merged 3 commits into from
Feb 11, 2019
Merged

Save memory on histograms: slightly faster with less code #536

merged 3 commits into from
Feb 11, 2019

Conversation

pascaldekloe
Copy link
Contributor

  • one allocation for duo instantiations
  • use index overflow instead of redundant last state field

name old time/op new time/op delta
HistogramWithLabelValues-12 204ns ± 0% 204ns ± 0% ~ (all equal)
HistogramNoLabels-12 41.1ns ± 0% 40.5ns ± 0% ~ (p=1.000 n=1+1)
HistogramObserve1-12 33.1ns ± 0% 33.9ns ± 0% ~ (p=1.000 n=1+1)
HistogramObserve2-12 132ns ± 0% 122ns ± 0% ~ (p=1.000 n=1+1)
HistogramObserve4-12 377ns ± 0% 364ns ± 0% ~ (p=1.000 n=1+1)
HistogramObserve8-12 1.00µs ± 0% 0.96µs ± 0% ~ (p=1.000 n=1+1)
HistogramWrite1-12 1.80µs ± 0% 1.76µs ± 0% ~ (p=1.000 n=1+1)
HistogramWrite2-12 3.28µs ± 0% 3.25µs ± 0% ~ (p=1.000 n=1+1)
HistogramWrite4-12 6.66µs ± 0% 6.66µs ± 0% ~ (p=1.000 n=1+1)
HistogramWrite8-12 13.7µs ± 0% 13.6µs ± 0% ~ (p=1.000 n=1+1)

name old alloc/op new alloc/op delta
HistogramWithLabelValues-12 48.0B ± 0% 48.0B ± 0% ~ (all equal)
HistogramNoLabels-12 0.00B 0.00B ~ (all equal)

name old allocs/op new allocs/op delta
HistogramWithLabelValues-12 1.00 ± 0% 1.00 ± 0% ~ (all equal)
HistogramNoLabels-12 0.00 0.00 ~ (all equal)

* one allocation for duo instantiations
* use index overflow instead of redundant last state field

name                         old time/op    new time/op    delta
HistogramWithLabelValues-12     204ns ± 0%     204ns ± 0%   ~     (all equal)
HistogramNoLabels-12           41.1ns ± 0%    40.5ns ± 0%   ~     (p=1.000 n=1+1)
HistogramObserve1-12           33.1ns ± 0%    33.9ns ± 0%   ~     (p=1.000 n=1+1)
HistogramObserve2-12            132ns ± 0%     122ns ± 0%   ~     (p=1.000 n=1+1)
HistogramObserve4-12            377ns ± 0%     364ns ± 0%   ~     (p=1.000 n=1+1)
HistogramObserve8-12           1.00µs ± 0%    0.96µs ± 0%   ~     (p=1.000 n=1+1)
HistogramWrite1-12             1.80µs ± 0%    1.76µs ± 0%   ~     (p=1.000 n=1+1)
HistogramWrite2-12             3.28µs ± 0%    3.25µs ± 0%   ~     (p=1.000 n=1+1)
HistogramWrite4-12             6.66µs ± 0%    6.66µs ± 0%   ~     (p=1.000 n=1+1)
HistogramWrite8-12             13.7µs ± 0%    13.6µs ± 0%   ~     (p=1.000 n=1+1)

name                         old alloc/op   new alloc/op   delta
HistogramWithLabelValues-12     48.0B ± 0%     48.0B ± 0%   ~     (all equal)
HistogramNoLabels-12            0.00B          0.00B        ~     (all equal)

name                         old allocs/op  new allocs/op  delta
HistogramWithLabelValues-12      1.00 ± 0%      1.00 ± 0%   ~     (all equal)
HistogramNoLabels-12             0.00           0.00        ~     (all equal)

Signed-off-by: Pascal S. de Kloe <[email protected]>
@beorn7
Copy link
Member

beorn7 commented Feb 10, 2019

Note: The test failure is unrelated to this PR. It's caused by a Travis change, which I'll take care of separately.

Copy link
Member

@beorn7 beorn7 left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution. Making the most significant bit the index bit simplifies things a lot. I really like the idea. The actual performance improvements appear very close to noise, but the code becomes cleaner (if "clean" can ever be an appropriate attribute if doing this kind of bit mangling).

On the other hand, I'm not convinced about the allocation optimizations in the constructor function. It's not really in the hot path, and it makes the code less clean.

prometheus/histogram.go Outdated Show resolved Hide resolved
prometheus/histogram.go Outdated Show resolved Hide resolved
prometheus/histogram.go Outdated Show resolved Hide resolved
prometheus/histogram.go Show resolved Hide resolved
@beorn7
Copy link
Member

beorn7 commented Feb 11, 2019

If you rebase this on master, it should make the test green.

Copy link
Member

@beorn7 beorn7 left a comment

Choose a reason for hiding this comment

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

Thanks again.

@beorn7 beorn7 merged commit 19ff277 into prometheus:master Feb 11, 2019
@pascaldekloe
Copy link
Contributor Author

I've borrowed some of your thinking so this is fair-trade. 😁

beorn7 pushed a commit that referenced this pull request Feb 11, 2019
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.

2 participants