Skip to content

Commit 8542d5b

Browse files
committed
lib/logstorage: add Fields.ClearUpToCapacity() function for clearing all the fields up to Fields.Fields capacity
Use this function in cases when references to the underlying byte slice at Fields.Fields entries must be cleared, so the underlying byte slice could be released by Go GC. This is a follow-up for 63a68ed See also #891
1 parent 2ba1344 commit 8542d5b

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
lines changed

app/vlinsert/loki/loki_json.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,12 @@ func parseJSONRequest(data []byte, lmp insertutil.LogMessageProcessor, msgFields
8282
}
8383

8484
fieldsTmp := logstorage.GetFields()
85-
defer logstorage.PutFields(fieldsTmp)
85+
defer func() {
86+
// Explicitly clear fieldsTmp up to its' capacity in order to free up
87+
// all the references to the original byte slice, so it could be freed by Go GC.
88+
fieldsTmp.ClearUpToCapacity()
89+
logstorage.PutFields(fieldsTmp)
90+
}()
8691

8792
var msgParser *logstorage.JSONParser
8893
if parseMessage {

app/vlinsert/loki/pb.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ func decodeStream(src []byte, pushLogs pushLogsHandler) error {
5050
// }
5151

5252
fs := logstorage.GetFields()
53-
defer logstorage.PutFields(fs)
53+
defer func() {
54+
// Explicitly clear fs up to its' capacity in order to free up
55+
// all the references to the original byte slice, so it could be freed by Go GC.
56+
fs.ClearUpToCapacity()
57+
logstorage.PutFields(fs)
58+
}()
5459

5560
labels, ok, err := easyproto.GetString(src, 1)
5661
if err != nil {

app/vlinsert/opentelemetry/pb.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,9 @@ func decodeResourceLogs(src []byte, pushLogs pushLogsHandler) (err error) {
5555

5656
fs := logstorage.GetFields()
5757
defer func() {
58-
// Manually clear all the fields up to capacity in order to remove references to src and help GC freeing up the used memory.
59-
// The logstorage.PutFields() clears only the references up to len(fs.Fields).
60-
clear(fs.Fields[:cap(fs.Fields)])
61-
fs.Fields = fs.Fields[:0]
62-
58+
// Explicitly clear fs up to its' capacity in order to free up
59+
// all the references to the original byte slice, so it could be freed by Go GC.
60+
fs.ClearUpToCapacity()
6361
logstorage.PutFields(fs)
6462
}()
6563

lib/logstorage/rows.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,15 @@ func (f *Fields) Reset() {
368368
f.Fields = f.Fields[:0]
369369
}
370370

371+
// ClearUpToCapacity clears f.Fields up to its' capacity.
372+
//
373+
// This function is useful in order to make sure f.Fields do not reference underlying byte slices,
374+
// so they could be freed by Go GC.
375+
func (f *Fields) ClearUpToCapacity() {
376+
clear(f.Fields[:cap(f.Fields)])
377+
f.Fields = f.Fields[:0]
378+
}
379+
371380
// Add adds (name, value) field to f.
372381
func (f *Fields) Add(name, value string) {
373382
f.Fields = append(f.Fields, Field{

0 commit comments

Comments
 (0)