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

Don't deepcopy AST when generating vars #6058

Merged
merged 1 commit into from
Nov 21, 2024
Merged

Don't deepcopy AST when generating vars #6058

merged 1 commit into from
Nov 21, 2024

Conversation

swiatekm
Copy link
Contributor

@swiatekm swiatekm commented Nov 18, 2024

What does this PR do?

We generate vars from mappings from both context providers and dynamic providers. Dynamic providers can produce multiple var entries - the Kubernetes provider will do so for each Pod, for example. When that happens, we currently make a deep copy of the context provider mapping, and add the dynamic provider mapping as a key.

As we don't modify the mappings, this deep copy can be replaced by a shallow copy. This PR introduces shallow copying to the AST and uses it when generating vars, leading to a major performance improvement.

Why is it important?

All these deep copies can be quite expensive when there's a lot of mappings from the dynamic provider. See the benchstat report below:

goos: linux
goarch: amd64
pkg: github.com/elastic/elastic-agent/internal/pkg/composable
cpu: 13th Gen Intel(R) Core(TM) i7-13700H
                       │ bench_main.txt │          bench_shallow.txt          │
                       │     sec/op     │   sec/op     vs base                │
GenerateVars100Pods-20    15247.8µ ± 4%   778.8µ ± 7%  -94.89% (p=0.000 n=10)

                       │ bench_main.txt │          bench_shallow.txt           │
                       │      B/op      │     B/op      vs base                │
GenerateVars100Pods-20   10273.4Ki ± 0%   779.1Ki ± 0%  -92.42% (p=0.000 n=10)

                       │ bench_main.txt │          bench_shallow.txt          │
                       │   allocs/op    │  allocs/op   vs base                │
GenerateVars100Pods-20     201.28k ± 0%   22.13k ± 0%  -89.00% (p=0.000 n=10)

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • I have added tests that prove my fix is effective or that my feature works
  • I have added an entry in ./changelog/fragments using the changelog tool

Related issues

@swiatekm swiatekm added the enhancement New feature or request label Nov 18, 2024
@swiatekm swiatekm requested a review from a team as a code owner November 18, 2024 14:56
Copy link
Contributor

mergify bot commented Nov 18, 2024

This pull request does not have a backport label. Could you fix it @swiatekm? 🙏
To fixup this pull request, you need to add the backport labels for the needed
branches, such as:

  • backport-./d./d is the label to automatically backport to the 8./d branch. /d is the digit

Copy link
Contributor

mergify bot commented Nov 18, 2024

backport-v8.x has been added to help with the transition to the new branch 8.x.
If you don't need it please use backport-skip label and remove the backport-8.x label.

@mergify mergify bot added the backport-8.x Automated backport to the 8.x branch with mergify label Nov 18, 2024
@swiatekm swiatekm added backport-8.16 Automated backport with mergify backport-8.15 Automated backport to the 8.15 branch with mergify skip-changelog labels Nov 18, 2024
@pierrehilbert pierrehilbert added the Team:Elastic-Agent-Control-Plane Label for the Agent Control Plane team label Nov 18, 2024
@elasticmachine
Copy link
Contributor

Pinging @elastic/elastic-agent-control-plane (Team:Elastic-Agent-Control-Plane)

Copy link
Contributor

@blakerouse blakerouse left a comment

Choose a reason for hiding this comment

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

Really nice! Looks good.

@amitkanfer
Copy link
Contributor

🚀

@swiatekm swiatekm merged commit db6fbe2 into main Nov 21, 2024
14 checks passed
@swiatekm swiatekm deleted the fix/coordinator-ast branch November 21, 2024 11:18
mergify bot pushed a commit that referenced this pull request Nov 21, 2024
(cherry picked from commit db6fbe2)

# Conflicts:
#	internal/pkg/composable/controller.go
mergify bot pushed a commit that referenced this pull request Nov 21, 2024
(cherry picked from commit db6fbe2)

# Conflicts:
#	internal/pkg/composable/controller.go
mergify bot pushed a commit that referenced this pull request Nov 21, 2024
@pierrehilbert
Copy link
Contributor

@swiatekm I think we want to backport this to 8.17 also. WDYT?

@swiatekm swiatekm added the backport-8.17 Automated backport with mergify label Nov 21, 2024
mergify bot pushed a commit that referenced this pull request Nov 21, 2024
@swiatekm swiatekm mentioned this pull request Nov 21, 2024
2 tasks
@amitkanfer
Copy link
Contributor

@swiatekm I think we want to backport this to 8.17 also. WDYT?

Are we 100% sure there is no hidden bug here?

@pierrehilbert
Copy link
Contributor

My two cents:

  • We don't have the first BC for 8.17 yet
  • We are backporting this to 8.15 and 8.16 so I hope we don't have an hidden bug here as the timeframe to test for those will be shorter than the 8.17 branch.

@swiatekm
Copy link
Contributor Author

If anything, maybe we should skip the 8.15 backport? It's pretty close, and this isn't that critical of a fix. For 8.17, I think we should backport either way, but I could wait until 8.17.0 is out before merging the PR.

@swiatekm I think we want to backport this to 8.17 also. WDYT?

Are we 100% sure there is no hidden bug here?

I'm never going to claim 100% for a codebase I haven't been working on for at least a year. I am as sure as I can reasonably be, I think. In practice, the change only has an effect when one of the dynamic variable providers is used. For these:

  • The local dynamic provider is used in unit tests
  • I've done fairly extensive manual e2e testing on the kubernetes provider

I could also manually test the docker provider, but I can't think of any reason it would be affected when the others aren't.

pierrehilbert pushed a commit that referenced this pull request Nov 21, 2024
(cherry picked from commit db6fbe2)

Co-authored-by: Mikołaj Świątek <[email protected]>
swiatekm added a commit that referenced this pull request Nov 21, 2024
(cherry picked from commit db6fbe2)

# Conflicts:
#	internal/pkg/composable/controller.go
swiatekm added a commit that referenced this pull request Nov 21, 2024
swiatekm added a commit that referenced this pull request Nov 22, 2024
@swiatekm swiatekm removed the backport-8.15 Automated backport to the 8.15 branch with mergify label Nov 22, 2024
swiatekm added a commit that referenced this pull request Nov 23, 2024
(cherry picked from commit db6fbe2)

Co-authored-by: Mikołaj Świątek <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-8.x Automated backport to the 8.x branch with mergify backport-8.16 Automated backport with mergify backport-8.17 Automated backport with mergify enhancement New feature or request skip-changelog Team:Elastic-Agent-Control-Plane Label for the Agent Control Plane team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants