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

Reduce memory allocations in decode_cef processor #16587

Merged
merged 5 commits into from
Feb 26, 2020

Conversation

andrewkroh
Copy link
Member

@andrewkroh andrewkroh commented Feb 26, 2020

What does this PR do?

Change the input parameter of the cef parser to accept a string instead of []byte to avoid unnecessary copies. Data is still copied from the input message when it contains escape sequences, but otherwise a slice of the original message is used.

Another minor improvement is to allocate the map for the CEF extensions up front based on the
estimated number of extension fields.

Results from go test -run none -bench . -benchtime 5s -benchmem . before and after:

$ benchcmp before.txt after.txt
benchmark                              old ns/op     new ns/op     delta
BenchmarkProcessorRun/short_msg-12     4833          4684          -3.08%
BenchmarkProcessorRun/long_msg-12      55724         52493         -5.80%

benchmark                              old allocs     new allocs     delta
BenchmarkProcessorRun/short_msg-12     55             41             -25.45%
BenchmarkProcessorRun/long_msg-12      349            219            -37.25%

benchmark                              old bytes     new bytes     delta
BenchmarkProcessorRun/short_msg-12     3728          3424          -8.15%
BenchmarkProcessorRun/long_msg-12      26929         21173         -21.37%

(On my Macbook Intel Core i9 2.9 GHz, that's 213.4K EPS for short messages and 19.0K EPS for long messages.)

Why is it important?

For short CEF messages the processor is fast, but with longer messages with lots of extensions it starts to slow down.

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have made corresponding change to the default configuration files
  • I have added tests that prove my fix is effective or that my feature works

Change the input of the cef parser to accept a string instead of []byte to avoid unnecessary copies.
Data is still copied from the input message when it contains escape sequences.

Another minor improvement is to allocate the map for the CEF extensions up front based on the
estimated number of extension fields.

Results from `go test -run none -bench . -benchtime 5s -benchmem .` before and after:

$ benchcmp before.txt after.txt
benchmark                              old ns/op     new ns/op     delta
BenchmarkProcessorRun/short_msg-12     4833          4684          -3.08%
BenchmarkProcessorRun/long_msg-12      55724         52493         -5.80%

benchmark                              old allocs     new allocs     delta
BenchmarkProcessorRun/short_msg-12     55             41             -25.45%
BenchmarkProcessorRun/long_msg-12      349            219            -37.25%

benchmark                              old bytes     new bytes     delta
BenchmarkProcessorRun/short_msg-12     3728          3424          -8.15%
BenchmarkProcessorRun/long_msg-12      26929         21173         -21.37%
@andrewkroh andrewkroh force-pushed the feature/libbeat/decode-cef-perf branch from 30e4324 to 6f62fff Compare February 26, 2020 02:26
@andrewkroh andrewkroh added the needs_backport PR is waiting to be backported to other branches. label Feb 26, 2020
@andrewkroh andrewkroh merged commit 7e7bc2e into elastic:master Feb 26, 2020
andrewkroh added a commit to andrewkroh/beats that referenced this pull request Mar 11, 2020
* Add decode_cef processor benchmarks

* Take advantage of string slicing to avoid allocations

Change the input of the cef parser to accept a string instead of []byte to avoid unnecessary copies.
Data is still copied from the input message when it contains escape sequences.

Another minor improvement is to allocate the map for the CEF extensions up front based on the
estimated number of extension fields.

Results from `go test -run none -bench . -benchtime 5s -benchmem .` before and after:

$ benchcmp before.txt after.txt
benchmark                              old ns/op     new ns/op     delta
BenchmarkProcessorRun/short_msg-12     4833          4684          -3.08%
BenchmarkProcessorRun/long_msg-12      55724         52493         -5.80%

benchmark                              old allocs     new allocs     delta
BenchmarkProcessorRun/short_msg-12     55             41             -25.45%
BenchmarkProcessorRun/long_msg-12      349            219            -37.25%

benchmark                              old bytes     new bytes     delta
BenchmarkProcessorRun/short_msg-12     3728          3424          -8.15%
BenchmarkProcessorRun/long_msg-12      26929         21173         -21.37%

(cherry picked from commit 7e7bc2e)
@andrewkroh andrewkroh added v7.7.0 and removed needs_backport PR is waiting to be backported to other branches. labels Mar 11, 2020
andrewkroh added a commit that referenced this pull request Mar 11, 2020
* Add decode_cef processor benchmarks

* Take advantage of string slicing to avoid allocations

Change the input of the cef parser to accept a string instead of []byte to avoid unnecessary copies.
Data is still copied from the input message when it contains escape sequences.

Another minor improvement is to allocate the map for the CEF extensions up front based on the
estimated number of extension fields.

Results from `go test -run none -bench . -benchtime 5s -benchmem .` before and after:

$ benchcmp before.txt after.txt
benchmark                              old ns/op     new ns/op     delta
BenchmarkProcessorRun/short_msg-12     4833          4684          -3.08%
BenchmarkProcessorRun/long_msg-12      55724         52493         -5.80%

benchmark                              old allocs     new allocs     delta
BenchmarkProcessorRun/short_msg-12     55             41             -25.45%
BenchmarkProcessorRun/long_msg-12      349            219            -37.25%

benchmark                              old bytes     new bytes     delta
BenchmarkProcessorRun/short_msg-12     3728          3424          -8.15%
BenchmarkProcessorRun/long_msg-12      26929         21173         -21.37%

(cherry picked from commit 7e7bc2e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants