Skip to content

[Optimization]: 🔨 add thread-safe wrapper around rand.Shuffle and improve performance. #1335

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

Closed

Conversation

yafengio
Copy link
Contributor

@yafengio yafengio commented Aug 8, 2025

This PR is aimed at enhancing performance and code maintainability while ensuring concurrent safety.

Here are the simple performance test results:

Benchmark_SafeRandShuffle/new_rand
Benchmark_SafeRandShuffle/new_rand-12         	   68043	     19829 ns/op	    5376 B/op	       1 allocs/op

Benchmark_SafeRandShuffle/safe_rand
Benchmark_SafeRandShuffle/safe_rand-12        	64563002	        16.04 ns/op	       0 B/op	       0 allocs/op

ref: #1314

cc @nirrozenbaum @ahg-g

@k8s-ci-robot k8s-ci-robot requested review from ahg-g and kfswain August 8, 2025 15:52
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: yafengio
Once this PR has been reviewed and has the lgtm label, please assign terrytangyuan for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Aug 8, 2025
Copy link

netlify bot commented Aug 8, 2025

Deploy Preview for gateway-api-inference-extension ready!

Name Link
🔨 Latest commit 21e9283
🔍 Latest deploy log https://app.netlify.com/projects/gateway-api-inference-extension/deploys/68971e8c2397a40008aaa99d
😎 Deploy Preview https://deploy-preview-1335--gateway-api-inference-extension.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Aug 8, 2025
@k8s-ci-robot
Copy link
Contributor

Hi @yafengio. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Aug 8, 2025
@kfswain
Copy link
Collaborator

kfswain commented Aug 8, 2025

Have we validated the concurrency limits here? In scale testing, simply creating a new rand object performed fine. I wasn't sure how the lock contention would perform in high concurrency situations

@yafengio yafengio closed this Aug 8, 2025
@yafengio yafengio reopened this Aug 8, 2025
@yafengio
Copy link
Contributor Author

yafengio commented Aug 8, 2025

Have we validated the concurrency limits here? In scale testing, simply creating a new rand object performed fine. I wasn't sure how the lock contention would perform in high concurrency situations

Yes, I did a benchmark test. Compared with creating a new instance each time, the overhead of the lock is very small.

@yafengio
Copy link
Contributor Author

yafengio commented Aug 9, 2025

Hi @kfswain , optimized with sync.Pool to reduce lock overhead and added benchmark tests. Please review. Thx

@yafengio yafengio changed the title refactor: 🔨 add thread-safe wrapper around rand.Rand and improve performance. [Optimization]: 🔨 add thread-safe wrapper around rand.Shuffle and improve performance. Aug 9, 2025
@yafengio
Copy link
Contributor Author

yafengio commented Aug 12, 2025

hi @kfswain , do you have any other questions? If you think pr is unnecessary, I'll close it.

cc @nirrozenbaum @ahg-g

@nirrozenbaum
Copy link
Contributor

Benchmark_SafeRandShuffle/new_rand
Benchmark_SafeRandShuffle/new_rand-12         	   68043	     19829 ns/op	    5376 B/op	       1 allocs/op

Benchmark_SafeRandShuffle/safe_rand
Benchmark_SafeRandShuffle/safe_rand-12        	64563002	        16.04 ns/op	       0 B/op	       0 allocs/op

can you explain what are the numbers we see here? what is 68043 vs 64563002 for example?
if I got it right, the second column is nanosecond per Pick call, third column is bytes allocated per Pick call and fourth is allocation of new variable per call?

assuming that's the case - personally I would keep the simple version of creating a new variable which seems to be simplest and easiest to maintain. looking at the numbers, Pick with new rand generator creation takes less than 0.2 millisecond, while we're targeting to complete the whole scheduling in less than 10 ms.
it feels to me like a premature optimization.

@yafengio
Copy link
Contributor Author

Benchmark_SafeRandShuffle/new_rand
Benchmark_SafeRandShuffle/new_rand-12         	   68043	     19829 ns/op	    5376 B/op	       1 allocs/op

Benchmark_SafeRandShuffle/safe_rand
Benchmark_SafeRandShuffle/safe_rand-12        	64563002	        16.04 ns/op	       0 B/op	       0 allocs/op

can you explain what are the numbers we see here? what is 68043 vs 64563002 for example? if I got it right, the second column is nanosecond per Pick call, third column is bytes allocated per Pick call and fourth is allocation of new variable per call?

Yes, what you understand is correct.

assuming that's the case - personally I would keep the simple version of creating a new variable which seems to be simplest and easiest to maintain. looking at the numbers, Pick with new rand generator creation takes less than 0.2 millisecond, while we're targeting to complete the whole scheduling in less than 10 ms. it feels to me like a premature optimization.

OK. Thank you for your reply. I'll close this PR.

@yafengio yafengio closed this Aug 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants