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

use allocation free converstion from byte slice to string #116

Merged
merged 2 commits into from Jul 29, 2022
Merged

use allocation free converstion from byte slice to string #116

merged 2 commits into from Jul 29, 2022

Conversation

florianl
Copy link
Member

@florianl florianl commented Jul 8, 2022

Signed-off-by: Florian Lehner florian.lehner@elastic.co

Reduce the number of allocations for converting a slice of bytes to a string.
Less allocation results in less work for GC.

@elasticmachine
Copy link
Collaborator

elasticmachine commented Jul 8, 2022

💚 Build Succeeded

the below badges are clickable and redirect to their specific view in the CI or DOCS
Pipeline View Test View Changes Artifacts preview preview

Expand to view the summary

Build stats

  • Start Time: 2022-07-20T11:59:21.336+0000

  • Duration: 2 min 30 sec

Test stats 🧪

Test Results
Failed 0
Passed 397
Skipped 40
Total 437

Copy link
Member

@andrewkroh andrewkroh left a comment

Choose a reason for hiding this comment

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

edit: updated based on 23fbe24

% benchstat before.txt after.txt 
name                                   old time/op    new time/op    delta
pkg:github.com/elastic/go-libaudit/v2/aucoalesce goos:darwin goarch:amd64
CoalesceMessages-10                      3.11µs ± 0%    3.24µs ± 0%   ~     (p=1.000 n=1+1)
pkg:github.com/elastic/go-libaudit/v2/auparse goos:darwin goarch:amd64
ExtractKeyValuePairs/bpf_message-10      7.81µs ± 0%    7.85µs ± 0%   ~     (p=1.000 n=1+1)
ExtractKeyValuePairs/short_message-10    1.27µs ± 0%    1.28µs ± 0%   ~     (p=1.000 n=1+1)
AuditMessageData/syscall-10              21.9µs ± 0%    21.6µs ± 0%   ~     (p=1.000 n=1+1)
AuditMessageData/user_cmd-10             14.9µs ± 0%    15.0µs ± 0%   ~     (p=1.000 n=1+1)
ParseAuditHeader-10                       147ns ± 0%      98ns ± 0%   ~     (p=1.000 n=1+1)
ParseAuditHeaderRegex-10                  545ns ± 0%     551ns ± 0%   ~     (p=1.000 n=1+1)
ParseLogLine-10                           360ns ± 0%     230ns ± 0%   ~     (p=1.000 n=1+1)

name                                   old alloc/op   new alloc/op   delta
pkg:github.com/elastic/go-libaudit/v2/aucoalesce goos:darwin goarch:amd64
CoalesceMessages-10                      2.72kB ± 0%    2.72kB ± 0%   ~     (all equal)
pkg:github.com/elastic/go-libaudit/v2/auparse goos:darwin goarch:amd64
ExtractKeyValuePairs/bpf_message-10      3.08kB ± 0%    3.08kB ± 0%   ~     (all equal)
ExtractKeyValuePairs/short_message-10    1.00kB ± 0%    1.00kB ± 0%   ~     (p=1.000 n=1+1)
AuditMessageData/syscall-10              15.3kB ± 0%    15.3kB ± 0%   ~     (p=1.000 n=1+1)
AuditMessageData/user_cmd-10             3.68kB ± 0%    3.68kB ± 0%   ~     (p=1.000 n=1+1)
ParseAuditHeader-10                       21.0B ± 0%      0.0B        ~     (p=1.000 n=1+1)
ParseAuditHeaderRegex-10                   128B ± 0%      128B ± 0%   ~     (all equal)
ParseLogLine-10                            358B ± 0%      112B ± 0%   ~     (p=1.000 n=1+1)

name                                   old allocs/op  new allocs/op  delta
pkg:github.com/elastic/go-libaudit/v2/aucoalesce goos:darwin goarch:amd64
CoalesceMessages-10                        13.0 ± 0%      13.0 ± 0%   ~     (all equal)
pkg:github.com/elastic/go-libaudit/v2/auparse goos:darwin goarch:amd64
ExtractKeyValuePairs/bpf_message-10        44.0 ± 0%      44.0 ± 0%   ~     (all equal)
ExtractKeyValuePairs/short_message-10      11.0 ± 0%      11.0 ± 0%   ~     (all equal)
AuditMessageData/syscall-10                 130 ± 0%       130 ± 0%   ~     (all equal)
AuditMessageData/user_cmd-10               64.0 ± 0%      64.0 ± 0%   ~     (all equal)
ParseAuditHeader-10                        3.00 ± 0%      0.00        ~     (p=1.000 n=1+1)
ParseAuditHeaderRegex-10                   2.00 ± 0%      2.00 ± 0%   ~     (all equal)
ParseLogLine-10                            4.00 ± 0%      1.00 ± 0%   ~     (p=1.000 n=1+1)

internal/helper.go Outdated Show resolved Hide resolved
@andrewkroh andrewkroh requested a review from a team July 8, 2022 18:27
@andrewkroh andrewkroh added the Team:Security-External Integrations Label for the Security External Integrations team label Jul 8, 2022
Copy link
Contributor

@efd6 efd6 left a comment

Choose a reason for hiding this comment

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

This should really have a regression test.

internal/helper.go Outdated Show resolved Hide resolved
Copy link
Contributor

@efd6 efd6 left a comment

Choose a reason for hiding this comment

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

I'm thinking that the only place where the unsafe conversion is actually needed is in auparse/hex.go and it could be a local function in that file.

cmd/audit/audit.go Outdated Show resolved Hide resolved
reassembler.go Outdated Show resolved Hide resolved
rule/rule.go Outdated Show resolved Hide resolved
@florianl
Copy link
Member Author

friendly ping @andrewkroh && @efd6

@efd6
Copy link
Contributor

efd6 commented Jul 14, 2022

After a test is added for the new function I am OK with this. It doesn't need to be much, just that the unsafe string of []byte of x is x.

@florianl
Copy link
Member Author

Added tests for UnsafeByteSlice2String.

internal/helper_test.go Outdated Show resolved Hide resolved
internal/helper_test.go Outdated Show resolved Hide resolved
florianl and others added 2 commits July 20, 2022 13:58
Signed-off-by: Florian Lehner <florian.lehner@elastic.co>
Co-authored-by: Dan Kortschak <90160302+efd6@users.noreply.github.com>
Signed-off-by: Florian Lehner <florian.lehner@elastic.co>
@florianl
Copy link
Member Author

friendly ping @andrewkroh && @efd6

I don't have the permissions to merge something. Therefore, please advice on who to continue.

@florianl
Copy link
Member Author

friendly ping @andrewkroh && @efd6 && @adriansr

I don't have the permissions to merge something. Therefore, please advice on who to continue.

@andrewkroh andrewkroh merged commit 1a53ce9 into elastic:main Jul 29, 2022
andrewkroh added a commit that referenced this pull request Jul 29, 2022
andrewkroh added a commit to andrewkroh/beats that referenced this pull request Jul 29, 2022
Reduce allocations when converting bytes to strings for received messages. [elastic#116](elastic/go-libaudit#116)
@florianl
Copy link
Member Author

Thanks 🙏

mergify bot pushed a commit to elastic/beats that referenced this pull request Aug 5, 2022
Reduce allocations when converting bytes to strings for received messages. [#116](elastic/go-libaudit#116)

(cherry picked from commit 7a469fd)
bearer-pipeline-test pushed a commit to BearerPipelineTest/beats that referenced this pull request Aug 5, 2022
Reduce allocations when converting bytes to strings for received messages. [elastic#116](elastic/go-libaudit#116)
andrewkroh added a commit to andrewkroh/go-libaudit that referenced this pull request Aug 24, 2022
Reassembler.Push has always made a copy of the provided byte slice.

It elastic#116 it was modified to not copy the slice. But this was a breaking change
to its behavior and causes bugs for existing users. The typical use of this
function is to parse data from AuditClient.Receive which explicitly documents
that caller must make a copy of the data if they are going to retain it beyond
the next Receive call.

Relates elastic#116
andrewkroh added a commit that referenced this pull request Aug 24, 2022
Reassembler.Push has always made a copy of the provided byte slice.

It #116 it was modified to not copy the slice. But this was a breaking change
to its behavior and causes bugs for existing users. The typical use of this
function is to parse data from AuditClient.Receive which explicitly documents
that caller must make a copy of the data if they are going to retain it beyond
the next Receive call.

Relates #116
@v1v v1v mentioned this pull request Aug 25, 2022
chrisberkhout pushed a commit to elastic/beats that referenced this pull request Jun 1, 2023
Reduce allocations when converting bytes to strings for received messages. [#116](elastic/go-libaudit#116)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Team:Security-External Integrations Label for the Security External Integrations team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants