-
Notifications
You must be signed in to change notification settings - Fork 91
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
linux: optimize parseKeyValue #186
Conversation
💚 CLA has been signed |
💔 Tests Failed
Expand to view the summary
Build stats
Test stats 🧪
Test errorsExpand to view the tests failures
|
The CLA check is failing because the author email in the commit does not match the email used to sign the CLA. |
A changelog entry will also be required. See https://github.com/elastic/go-sysinfo/blob/main/CONTRIBUTING.md. |
@andrewkroh signed the CLA with the correct email, also provided a changelog file. lmk if the description of the change is ok :) |
/test |
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.
Needs some changes because it can panic.
You're right ! I fixed the panic and included your fuzz test |
CI is failing on a missing license header. If you run |
Tests are still failing:
|
fixed |
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 the logic of the parser could be made a fair bit simpler by using bytes.Cut
.
providers/linux/util_test.go
Outdated
parseKeyValue(testProcStatus, ':', func(key, value []byte) error { | ||
return nil | ||
}) |
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 can potentially be optimised away.
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.
added an _
assignment to ensure evaluation
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'm afraid I do not believe that that is necessarily safe. To avoid the potential issue, there needs to be a side effect.
providers/linux/util.go
Outdated
if len(parts) != 2 { | ||
func parseKeyValue(content []byte, separator byte, callback func(key, value []byte) error) error { | ||
for len(content) > 0 { | ||
sepIdx := bytes.IndexByte(content, separator) |
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 we tolerate empty lines? The previous code did.
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.
added a test specifically for empty lines
You're right, I updated my change to use
This new version seems slightly slower than the one using |
I compared the benchmark results from e4ac65c to f3a5024. Looks good.
|
@efd6 Can you please take another look. |
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.
Still LGTM. Stylistic nit that can be ignored.
Co-authored-by: Dan Kortschak <[email protected]>
As seen in this
alloc_objects
profile from auditbeatlinux.parseKeyValue()
accounts for a significant amount of the program allocations:This PR adds tests on the current implem to validate the expected behavior and proposes a new implementation for this function that removes all allocs. As a nice side effect, it also cuts down cpu time by ~90%: