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
[x] My code follows the style guidelines of this project
[x] I have commented my code, particularly in hard-to-understand areas
[x] 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
Relates #5835
Relates #5991
This is an automatic backport of pull request #6058 done by Mergify.
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:
Checklist
./changelog/fragments
using the changelog toolRelated issues
This is an automatic backport of pull request #6058 done by Mergify.