Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

tszlong: make encoding more compact by conservatively resetting leading/trailing bitcount #2005

Merged
merged 3 commits into from
Dec 2, 2021

Conversation

shanson7
Copy link
Collaborator

While profiling memory usage for Metrictank we found that ~15% of memory usage came from the raw bstream. This wasn't surprisingly high, but we decided to look for possible optimizations.

We found that 67% of those allocations came from this one line. This line is for writing the significant bits when the value isn't exactly the same as the previous, but falls within the leading/trailing zeroes that previously had been written. However, since the leading/trailing is never reset, a single errant value can cause a lot of significant bits to be written each time. This is especially true for floating point values. For example (playground):

math.Float64bits(126.45) ^ math.Float64bits(127.45) only has a difference of 1 bit (17 leading zeroes/46 trailing)
math.Float64bits(127.45) ^ math.Float64bits(128.45) has only 10 leading zeroes and 0 trailing.

So in a lot of cases we could end up writing many bits of extra sig figs (lots of leading/trailing zeroes) when it could be better to just re-encode the leading/trailing zeroes and write fewer sig figs for some amount of time.

I added some more scenarios to the benchmarks to test various data patterns, 120 datapoints each (chosen as that is the recommended chunk size):

Pattern Old Bytes New Bytes % of Old
Mono+reset 993 384 38.67%
Mono 843 162 19.2%
SawTooth 1008 1006 99.8%
SawTooth+Flat 1008 1004 99.6%
Steps 752 703 93.48%
Real World CPU 777 671 86.36%

As seen, this approach performs no worse than the existing in terms of chunk size, however in some cases it is significantly better. I have yet to plug this into a running MT instance and see if the result is true across a large range of real-world data.

@shanson7 shanson7 marked this pull request as ready for review November 2, 2021 16:03
@shanson7
Copy link
Collaborator Author

shanson7 commented Nov 2, 2021

We saw an average 4-byte per chunk reduction across all chunks. This represents only ~3% improvement, but better than nothing.

Copy link
Contributor

@Dieterbe Dieterbe left a comment

Choose a reason for hiding this comment

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

ready to merge. see comment, waiting for your final go ahead.

@shanson7
Copy link
Collaborator Author

shanson7 commented Nov 30, 2021

We have been running this code in production and saw a small average benefit. Many series see no change (example series that just publish integers). I'm good with merging this change as-is.

@Dieterbe Dieterbe changed the title tszlong performance optimizations tszlong: make encoding more compact by conservatively resetting leading/trailing bitcount Dec 2, 2021
@Dieterbe Dieterbe merged commit aeef370 into grafana:master Dec 2, 2021
@shanson7 shanson7 deleted the tsz_perf branch December 3, 2021 09:07
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants