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

Avoid unnecessary copies during config generation #6184

Merged
merged 3 commits into from
Dec 9, 2024

Conversation

swiatekm
Copy link
Contributor

@swiatekm swiatekm commented Dec 2, 2024

What does this PR do?

Remove some unnecessary copies when generating component configuration.

The first copy avoided is done because we want to replace the inputs key with the rendered inputs. A shallow copy is sufficient for this. I did find and fix a bug in Dict shallow copies while at it.

The rest of the copies have to do with applying vars to each AST node. We currently make a copy of each Node we do this for, but that's not necessary, as Apply creates a new Node with all the data recursively copied anyway. I've added a test to verify that Apply actually works this way.

Benchstat result vs main, using the benchmark from #6180:

goos: linux
goarch: amd64
pkg: github.com/elastic/elastic-agent/internal/pkg/agent/application/coordinator
cpu: 13th Gen Intel(R) Core(TM) i7-13700H
                                      │ bench_main.txt │          bench_noclone.txt          │
                                      │     sec/op     │   sec/op     vs base                │
Coordinator_generateComponentModel-20     37.99m ± 24%   32.36m ± 4%  -14.82% (p=0.000 n=10)

                                      │ bench_main.txt │          bench_noclone.txt           │
                                      │      B/op      │     B/op      vs base                │
Coordinator_generateComponentModel-20     34.22Mi ± 0%   25.38Mi ± 0%  -25.84% (p=0.000 n=10)

                                      │ bench_main.txt │          bench_noclone.txt          │
                                      │   allocs/op    │  allocs/op   vs base                │
Coordinator_generateComponentModel-20      810.7k ± 0%   580.4k ± 0%  -28.40% (p=0.000 n=10)

Why is it important?

More performance is good! More broadly, this is an attempt to incrementally improve the problem from #5991.

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

Copy link
Contributor

mergify bot commented Dec 2, 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 Dec 2, 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 Dec 2, 2024
@swiatekm swiatekm added backport-8.16 Automated backport with mergify backport-8.17 Automated backport with mergify Team:Elastic-Agent-Control-Plane Label for the Agent Control Plane team labels Dec 2, 2024
@swiatekm swiatekm force-pushed the feat/config-gen-remove-copies branch from 3e175a0 to 969aac9 Compare December 2, 2024 16:50
# Conflicts:
#	internal/pkg/agent/transpiler/ast.go
@swiatekm swiatekm force-pushed the feat/config-gen-remove-copies branch from 969aac9 to f0bf34d Compare December 2, 2024 17:00
@swiatekm swiatekm added the enhancement New feature or request label Dec 2, 2024
@swiatekm swiatekm marked this pull request as ready for review December 2, 2024 17:14
@swiatekm swiatekm requested a review from a team as a code owner December 2, 2024 17:14
@elasticmachine
Copy link
Contributor

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

Copy link
Contributor

@kaanyalti kaanyalti left a comment

Choose a reason for hiding this comment

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

Code changes look good to me, but I don't have much context here. It would be better for someone else to look at it as well.

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.

Nice improvement! Looks good.

if !ok {
continue
}
hadStreams := false
if streams := getStreams(dict); streams != nil {
hadStreams = true
}
// Apply creates a new Node with a deep copy of all the values
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the comment here, helps with understanding that Apply is going to return a copy.

@swiatekm swiatekm merged commit 398f322 into main Dec 9, 2024
15 checks passed
@swiatekm swiatekm deleted the feat/config-gen-remove-copies branch December 9, 2024 16:06
mergify bot pushed a commit that referenced this pull request Dec 9, 2024
* Fix a bug in AST dictionary shallow cloning

* Avoid unnecessary deepcopy during config generation

* Don't unnecessarily copy AST when rendering inputs

# Conflicts:
#	internal/pkg/agent/transpiler/ast.go

(cherry picked from commit 398f322)
mergify bot pushed a commit that referenced this pull request Dec 9, 2024
* Fix a bug in AST dictionary shallow cloning

* Avoid unnecessary deepcopy during config generation

* Don't unnecessarily copy AST when rendering inputs

# Conflicts:
#	internal/pkg/agent/transpiler/ast.go

(cherry picked from commit 398f322)

# Conflicts:
#	internal/pkg/agent/transpiler/ast.go
mergify bot pushed a commit that referenced this pull request Dec 9, 2024
* Fix a bug in AST dictionary shallow cloning

* Avoid unnecessary deepcopy during config generation

* Don't unnecessarily copy AST when rendering inputs

# Conflicts:
#	internal/pkg/agent/transpiler/ast.go

(cherry picked from commit 398f322)

# Conflicts:
#	internal/pkg/agent/transpiler/ast.go
#	internal/pkg/agent/transpiler/ast_test.go
swiatekm added a commit that referenced this pull request Dec 9, 2024
* Fix a bug in AST dictionary shallow cloning

* Avoid unnecessary deepcopy during config generation

* Don't unnecessarily copy AST when rendering inputs

# Conflicts:
#	internal/pkg/agent/transpiler/ast.go

(cherry picked from commit 398f322)

# Conflicts:
#	internal/pkg/agent/transpiler/ast.go
swiatekm added a commit that referenced this pull request Dec 9, 2024
* Fix a bug in AST dictionary shallow cloning

* Avoid unnecessary deepcopy during config generation

* Don't unnecessarily copy AST when rendering inputs

# Conflicts:
#	internal/pkg/agent/transpiler/ast.go

(cherry picked from commit 398f322)

Co-authored-by: Mikołaj Świątek <mail@mikolajswiatek.com>
swiatekm added a commit that referenced this pull request Dec 9, 2024
* Fix a bug in AST dictionary shallow cloning

* Avoid unnecessary deepcopy during config generation

* Don't unnecessarily copy AST when rendering inputs

# Conflicts:
#	internal/pkg/agent/transpiler/ast.go

(cherry picked from commit 398f322)

# Conflicts:
#	internal/pkg/agent/transpiler/ast.go

Co-authored-by: Mikołaj Świątek <mail@mikolajswiatek.com>
swiatekm added a commit that referenced this pull request Dec 13, 2024
* Fix a bug in AST dictionary shallow cloning

* Avoid unnecessary deepcopy during config generation

* Don't unnecessarily copy AST when rendering inputs

# Conflicts:
#	internal/pkg/agent/transpiler/ast.go

(cherry picked from commit 398f322)

# Conflicts:
#	internal/pkg/agent/transpiler/ast.go
#	internal/pkg/agent/transpiler/ast_test.go

# Conflicts:
#	internal/pkg/agent/transpiler/ast.go
#	internal/pkg/agent/transpiler/ast_test.go
swiatekm added a commit that referenced this pull request Dec 13, 2024
* Fix a bug in AST dictionary shallow cloning

* Avoid unnecessary deepcopy during config generation

* Don't unnecessarily copy AST when rendering inputs

# Conflicts:
#	internal/pkg/agent/transpiler/ast.go

(cherry picked from commit 398f322)

# Conflicts:
#	internal/pkg/agent/transpiler/ast.go
#	internal/pkg/agent/transpiler/ast_test.go

# Conflicts:
#	internal/pkg/agent/transpiler/ast.go
#	internal/pkg/agent/transpiler/ast_test.go
swiatekm added a commit that referenced this pull request Dec 13, 2024
* Fix a bug in AST dictionary shallow cloning

* Avoid unnecessary deepcopy during config generation

* Don't unnecessarily copy AST when rendering inputs

# Conflicts:
#	internal/pkg/agent/transpiler/ast.go

(cherry picked from commit 398f322)

# Conflicts:
#	internal/pkg/agent/transpiler/ast.go
#	internal/pkg/agent/transpiler/ast_test.go

# Conflicts:
#	internal/pkg/agent/transpiler/ast.go
#	internal/pkg/agent/transpiler/ast_test.go

Co-authored-by: Mikołaj Świątek <mail@mikolajswiatek.com>
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 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.

4 participants