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

Labels: reduce allocations when creating from TSDB WAL #13044

Merged
merged 2 commits into from
Nov 14, 2023

Conversation

bboreham
Copy link
Member

This is used when reading the WAL; by passing references into the buffer we can avoid copying strings under -tags stringlabels. It's even better with #12304.

I had to bump the number of iterations to get stable results on this benchmark:

% go test -tags stringlabels -benchtime 100x -run xxx -bench LoadWLs/samplesPerSeries=50,exemplarsPerSeries=0 -benchmem ./tsdb

Before:

               100         272656568 ns/op        218812154 B/op   2202642 allocs/op

After:

               100         269984012 ns/op        206498400 B/op   1203049 allocs/op

When reading the WAL, by passing references into the buffer we can avoid
copying strings under `-tags stringlabels`.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
Copy link
Member

@bwplotka bwplotka left a comment

Choose a reason for hiding this comment

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

Thanks!

Looks like nice improvement, but the benchmark results have surprisingly not show the reduction in mem allocated. There are less allocs so less pressure on GC, but I would expect less space allocated too? 🤔

@@ -617,6 +617,12 @@ func (b *ScratchBuilder) Add(name, value string) {
b.add = append(b.add, Label{Name: name, Value: value})
}

// Add a name/value pair, using []byte instead of string.
// Intended for '-tags stringlabels'; this version purely so the old way still compiles.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// Intended for '-tags stringlabels'; this version purely so the old way still compiles.
// Intended for '-tags stringlabels'; this version has the same same (safe) semantics as Add.

Hm - this is bit weird - unsafe is sometimes safe sometimes unsafe. Why not making this use yolo string as well?

Copy link
Member

Choose a reason for hiding this comment

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

Like it feels we need to ensure liveness of those bytes on caller side of this no matter what tag (labels implementation) is used.

Copy link
Member Author

Choose a reason for hiding this comment

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

We have to copy some time. stringlabels makes one block of memory with all the values in, so that's a copy. Previously all versions copied in UvarintStr().

I don't think it's a great idea to make the documentation say it's safe.

Copy link
Member Author

Choose a reason for hiding this comment

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

I pushed a commit which I hope makes the description clearer. Let me know what you think.

@bboreham
Copy link
Member Author

the benchmark results have surprisingly not show the reduction in mem allocated

I see a reduction from 218812154 B/op to 206498400 B/op.
Did you mean it's not such a big percentage? There's a lot of other stuff going on in the benchmark.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
@bboreham bboreham merged commit 1bfb3ed into prometheus:main Nov 14, 2023
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants