-
Notifications
You must be signed in to change notification settings - Fork 3.9k
perf: Reduce allocations for JSON and logfmt parser #18637
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
perf: Reduce allocations for JSON and logfmt parser #18637
Conversation
| } | ||
|
|
||
| // TODO: this is slow. | ||
| func (l labelsResult) Labels() labels.Labels { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method should ideally go. It's used only once AFAICT
…tant-alloc' into karsten/parser-constant-alloc
pkg/logql/log/labels.go
Outdated
| if b.add[k] != nil { | ||
| b.add[k].reset() | ||
| } else { | ||
| b.add[k] = newColumnarLabels(0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why initialize with capacity 0? NewBaseLabelsBuilderWithGrouping initializes with capacity 16.
| "slices" | ||
| ) | ||
|
|
||
| type stringColumn struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a comment could help to understand faster how the struct is supposed to work.
pkg/logql/log/parser.go
Outdated
| // TODO: very this is ok. buildJSONPathFromPrefixBuffer uses and unsafe string | ||
| j.lbs.SetJSONPath(sanitizedKey, []string{unsafeGetString(key)}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I remember having a log line corruption bug in this area of the JSON parser. We need to be super careful about changes. The tests however assert that the original log line is not modified.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reset is not resetting BaseLabelsBuilder.jsonPath. So I'd say we cannot use unsafe here. The implementation for JSON path would have to change. Btw it's the biggest impact on resources.
buildJSONPathFromPrefixBuffer uses and unsafe string
I was wrong.buildJSONPathFromPrefixBufferhas an append which copiesjsonPath = append(jsonPath, partStr)
I'll follow up once this lands.
pkg/logql/log/parser.go
Outdated
| res := unsafeGetString(bU) | ||
| if strings.ContainsRune(res, utf8.RuneError) { | ||
| res = strings.Map(removeInvalidUtf, res) | ||
| // TODO: verify that this works |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be removed?
pkg/logql/log/pipeline_test.go
Outdated
| //if resLbs.Parsed().Get("context_file") != "metrics.go" { | ||
| // b.Fatalf("label was not extracted correctly! context_file was %v, expected metrics.go, %+v\n", resLbs.Parsed().Get("context_file"), resLbs.Parsed()) | ||
| //} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the assertion be removed? Or did you want to uncomment it again?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's enabled. I removed it originally because the original resLbs.Labels() was messing with the benchmark results.
| // stringColumn is a columnar representation of a string slice. The bytes of all | ||
| // strings are stored in a single bytes slice `data`. The `offsets` slice points | ||
| // to the start of each string in `data`. The first entry will always be 0. All | ||
| // following entries are the accumulated length of all previous strings. | ||
| // | ||
| // Strings are always accessed via the `indices` slices. It points to the index | ||
| // of the string in the `offsets` slice. This technique allows deleting and | ||
| // sorting without copying the data. E.g. sorting just sorts the indices but not | ||
| // `data` not `offsets`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
chaudum
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
**What this PR does / why we need it**: The JSON and logfmt parsers suffer from a high number of allocations when the parsed labels have a high cardinality. This change reduces the allocations quite a bit by copying the labels bytes to a byte buffer. Note: The benchmarks ran without a validation step that calls `LabelResult.Labels()` as that method uses a lot of allocation but is used only once in production code.
|
@claude hello? |
|
Claude finished @slim-bean's task —— View job Hello @slim-bean! 👋 I'm here and ready to help with any Loki development tasks you need assistance with. This PR looks like it had some great performance improvements for the JSON and logfmt parsers - nice work by @jeschkies on reducing those allocations! Let me know if you need any code reviews, analysis, or implementation work. |
|
@claude can you look at this code for potential off-by-one type or similar errors please |
|
Claude finished @slim-bean's task —— View job Code Review: Potential Off-by-One and Similar ErrorsI've analyzed the key files changed in this performance optimization PR. Here are my findings: ✅ No Critical Off-by-One Errors FoundThe code appears well-implemented with proper bounds checking. Here's what I examined: pkg/logql/log/labels_columnar.go - New columnar storage implementation
pkg/logql/log/parser.go - Buffer management
pkg/logql/log/labels.go - Label handling
|
This reverts commit adb27ff.
This reverts commit adb27ff.
|
@claude can you review this PR for use of global variables and any potential concerns |
|
Claude finished @slim-bean's task —— View job Global Variable Analysis for PR #18637I've completed a thorough analysis of this performance optimization PR for global variable usage and potential concerns. Todo List:
✅ Global Variable Review SummaryGlobal Variables Found:pkg/logql/log/labels.go:
pkg/logql/log/parser.go:
pkg/logql/log/fmt.go:
✅ Assessment: All Global Variables are Safe1. Read-Only Constants & Immutable Data:
2. Thread-Safe Constructs:
3. Proper Initialization:
4. Performance Benefits:
No Concerns Identified:❌ No concurrency issues - All globals are read-only or use thread-safe constructs Recommendation: ✅ APPROVEDThe global variable usage in this PR is exemplary for performance-focused code. All globals serve clear performance purposes and are implemented using Go concurrency best practices. The optimization approach of using pools, pre-compiled regexes, and cached immutable values is standard in high-performance Go applications like Loki. |
This reverts commit adb27ff.
What this PR does / why we need it:
The JSON and logfmt parsers suffer from a high number of allocations when the parsed labels have a high cardinality.
This change reduces the allocations quite a bit by copying the labels bytes to a byte buffer.
Special notes for your reviewer:
Checklist
CONTRIBUTING.mdguide (required)featPRs are unlikely to be accepted unless a case can be made for the feature actually being a bug fix to existing behavior.docs/sources/setup/upgrade/_index.mddeprecated-config.yamlanddeleted-config.yamlfiles respectively in thetools/deprecated-config-checkerdirectory. Example PR