From 2ba7bc94460304d3cf114e26537d58640f4a7941 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Wed, 5 Jun 2024 18:54:13 +0100 Subject: [PATCH] Labels: further optimisation for dedupelabels Inline (by copy-paste) the fast path of `decodeVarint` in various places where it gets called a lot. Signed-off-by: Bryan Boreham --- model/labels/labels_dedupelabels.go | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/model/labels/labels_dedupelabels.go b/model/labels/labels_dedupelabels.go index c0d84d02f..0e5bb048b 100644 --- a/model/labels/labels_dedupelabels.go +++ b/model/labels/labels_dedupelabels.go @@ -113,7 +113,10 @@ func decodeVarint(data string, index int) (int, int) { if b < 0x8000 { return b, index } + return decodeVarintRest(b, data, index) +} +func decodeVarintRest(b int, data string, index int) (int, int) { value := int(b & 0x7FFF) b = int(data[index]) index++ @@ -128,8 +131,12 @@ func decodeVarint(data string, index int) (int, int) { } func decodeString(t *nameTable, data string, index int) (string, int) { - var num int - num, index = decodeVarint(data, index) + // Copy decodeVarint here, because the Go compiler says it's too big to inline. + num := int(data[index]) + int(data[index+1])<<8 + index += 2 + if num >= 0x8000 { + num, index = decodeVarintRest(num, data, index) + } return t.ToName(num), index } @@ -323,7 +330,12 @@ func (ls Labels) Get(name string) string { } else if lName[0] > name[0] { // Stop looking if we've gone past. break } - _, i = decodeVarint(ls.data, i) + // Copy decodeVarint here, because the Go compiler says it's too big to inline. + num := int(ls.data[i]) + int(ls.data[i+1])<<8 + i += 2 + if num >= 0x8000 { + _, i = decodeVarintRest(num, ls.data, i) + } } return "" } @@ -341,7 +353,12 @@ func (ls Labels) Has(name string) bool { } else if lName[0] > name[0] { // Stop looking if we've gone past. break } - _, i = decodeVarint(ls.data, i) + // Copy decodeVarint here, because the Go compiler says it's too big to inline. + num := int(ls.data[i]) + int(ls.data[i+1])<<8 + i += 2 + if num >= 0x8000 { + _, i = decodeVarintRest(num, ls.data, i) + } } return false }