Skip to content

mapstr: optimyze Clone#64

Merged
cmacknz merged 1 commit intoelastic:mainfrom
florianl:flo-mapstr-clone
Jun 28, 2022
Merged

mapstr: optimyze Clone#64
cmacknz merged 1 commit intoelastic:mainfrom
florianl:flo-mapstr-clone

Conversation

@florianl
Copy link
Copy Markdown
Member

@florianl florianl commented Jun 28, 2022

Reduce the ammount of allocations when using Clone().

name     old time/op    new time/op    delta
Clone-8    8.95µs ± 0%    5.51µs ± 0%   ~     (p=1.000 n=1+1)

name     old alloc/op   new alloc/op   delta
Clone-8    4.99kB ± 0%    2.50kB ± 0%   ~     (p=1.000 n=1+1)

name     old allocs/op  new allocs/op  delta
Clone-8      64.0 ± 0%      33.0 ± 0%   ~     (p=1.000 n=1+1)

Type of change: Enhancement

What does this PR do?

When profiling filebeat I have noticed heavy use of MapStr.Clone() operations that resulted in many runtime.mallocgc(). Looking at the code, I did see that the resulting M was not sized properly even if its resulting size is known in advance.

As the added benchmark demonstrates, there is a noticeable reduction of memory allocation even for small MapStr when using Clone() now.

Why is it important?

Incremental resizing of MapStr, as it is done when MapStr is not properly sized in advance, will give the garbage collector more work to do. So this change does not only reduce the number of allocations but also reduces the work that needs to be done by the garbage collector.

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
    As there is only a minimal code change, I have skipped this one. Please let me know, if you want more comments.
  • I have added tests benchmarks that prove my fix is effective or that my feature works
  • I have added an entry in CHANGELOG.md
    For users of this package there is no "visible" change. So I did not add something.

Reduce the ammount of allocations when using Clone().

name     old time/op    new time/op    delta
Clone-8    8.95µs ± 0%    5.51µs ± 0%   ~     (p=1.000 n=1+1)

name     old alloc/op   new alloc/op   delta
Clone-8    4.99kB ± 0%    2.50kB ± 0%   ~     (p=1.000 n=1+1)

name     old allocs/op  new allocs/op  delta
Clone-8      64.0 ± 0%      33.0 ± 0%   ~     (p=1.000 n=1+1)
@florianl florianl requested a review from a team as a code owner June 28, 2022 12:56
@florianl florianl requested review from belimawr and fearful-symmetry and removed request for a team June 28, 2022 12:56
@cmacknz cmacknz requested a review from faec June 28, 2022 13:12
@cmacknz cmacknz added the Team:Elastic-Agent-Data-Plane Label for the Agent Data Plane team label Jun 28, 2022
@elasticmachine
Copy link
Copy Markdown
Collaborator

💚 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-06-28T12:56:42.447+0000

  • Duration: 20 min 46 sec

Test stats 🧪

Test Results
Failed 0
Passed 1076
Skipped 6
Total 1082

🤖 GitHub comments

To re-run your PR in the CI, just comment with:

  • /test : Re-trigger the build.

Copy link
Copy Markdown
Member

@cmacknz cmacknz left a comment

Choose a reason for hiding this comment

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

Nice! There will need to be a follow up PR in beats to pick up this change, I can tag it after we merge this.

@cmacknz cmacknz merged commit 5bdd25e into elastic:main Jun 28, 2022
@cmacknz
Copy link
Copy Markdown
Member

cmacknz commented Jun 28, 2022

@cmacknz
Copy link
Copy Markdown
Member

cmacknz commented Jun 29, 2022

Beats PR incorporating this change: elastic/beats#32161

cmacknz added a commit to cmacknz/elastic-agent-libs that referenced this pull request Jun 30, 2022
The bug was introduced by
elastic#64.

Add a test to catch the bug that slipped through.
cmacknz added a commit that referenced this pull request Jun 30, 2022
* Fix bug introduced by mapstr optimization.

The bug was introduced by
#64.

Add a test to catch the bug that slipped through.

* Update changelog for 0.2.7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Team:Elastic-Agent-Data-Plane Label for the Agent Data Plane team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants