perf: optimize resolvedEnv parsing in buildScalers#7374
perf: optimize resolvedEnv parsing in buildScalers#7374forrestIsRunning wants to merge 5 commits intokedacore:mainfrom
Conversation
|
Thank you for your contribution! 🙏 Please understand that we will do our best to review your PR and give you feedback as soon as possible, but please bear with us if it takes a little longer as expected. While you are waiting, make sure to:
Once the initial tests are successful, a KEDA member will ensure that the e2e tests are run. Once the e2e tests have been successfully completed, the PR may be merged at a later date. Please be patient. Learn more about our contribution guide. |
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
|
Could you fix DCO check? all the commits need to be signed |
|
/run-e2e internal |
Cache resolvedEnv parsing result at function start instead of parsing it for each trigger. This reduces Kubernetes API calls from N to 1 where N is the number of triggers, significantly improving performance for ScaledObjects with multiple triggers. Signed-off-by: yansaitao <yansaitao@qq.com>
3eb7e4a to
9af9a43
Compare
|
Done. All commits have now been signed and the DCO check should be passing.
|
|
/run-e2e internal |
|
That's because the checklist hasn't been fully checked off yet. Check off everything that's necessary and remove any items that don't apply. |
Signed-off-by: yansaitao <yansaitao@qq.com>
ab9910a to
5b56a9d
Compare
Signed-off-by: yansaitao <yansaitao@qq.com>
|
/run-e2e internal |
|
I have a little concern about this change. The Factory closure is reused later by refreshScaler when scalers need to be rebuilt due to errors or empty metric specs. |
This is a really nice point and you're 100% right. If we do the closure there, we will lock the pod spec forever till the scaledobject is updated, any change that happens on the workload will be ignored. @forrestIsRunning , have you see the improvement of the API calls? we use a cached client, so almost all of them should happen just once (although it doesn't mean that they are free for KEDA, accessing the cached items requires deep copies which aren't free) |
|
Thanks for the excellent catch! You're absolutely right - moving I've reverted the change and moved Regarding performance: as you mentioned, since we use a cached client and Thanks again for the thorough review! |
|
Is there any change in addition to the comment? I think that it's useful to avoid this problem in the future, but the changelog entry can be removed IMHO |
Signed-off-by: yansaitao <yansaitao@qq.com>
|
Good point! The code logic is now the same as the original -
I have removed the changelog entry since this is just fixing a mistaken optimization attempt, not a user-facing improvement. |
|
Yeah, ok, the change is fine but doesn't actually fix or improve anything imho. What do you think @JorTurFer, is this merge worthy? I'm not in favor of this change. |
|
The benefit that I see is for future maintenance, as it clarifies that we have to recover it on each case, but from functional pov, nothing has changed. I'm not in, I'm not against tbh |
Understood. The functional change is indeed minimal - just a comment addition. The main value is preventing future mistakes when someone might try a similar optimization. If you think it's not worth a separate PR, I'm happy to close this and perhaps include the comment in a future PR that touches this area. Or we can keep it as a small documentation improvement - either way works for me. |

Cache resolvedEnv parsing result at function start instead of parsing it for each trigger. This reduces Kubernetes API calls from N to 1 where N is the number of triggers, significantly improving performance for ScaledObjects with multiple triggers.
Description
This PR optimizes the
buildScalersfunction by movingresolvedEnvparsing outside the trigger loop. Previously,ResolveContainerEnvwas called once per trigger in the factory function, resulting in N Kubernetes API calls for N triggers. Now it's resolved once at the beginning of the function and shared across all triggers.Changes:
resolvedEnvonce before the trigger loopresolvedEnvresult across all trigger factoriesBenefits:
Checklist
Fixes #
Relates to #