Skip to content

(minor improvement): small refactoring in make_token_replica_map() #513

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mykaul
Copy link

@mykaul mykaul commented Aug 7, 2025

While trying to look at some random (flaky?) test (#510 (comment) ) I saw some (minor) improvements that can be made to make_token_replica_map():

  1. Remove some redundant len() calls to outside the loop(s)
  2. Align some variable names, start with num_ ... for them.
  3. Move token_offset and host assignment within the loop to closer to where it's used.
  4. Only add hosts that are in the dc_rf_map to begin with.

All those are probably very very minor improvements, perhaps in a large cluster it'll be noticable.

Pre-review checklist

  • I have split my patch into logically separate commits.
  • All commit messages clearly explain what they change and why.
  • I added relevant tests for new features and bug fixes.
  • All commits compile, pass static checks and pass test.
  • PR description sums up the changes and reasons why they should be introduced.
  • I have provided docstrings for the public items that I want to introduce.
  • I have adjusted the documentation in ./docs/source/.
  • I added appropriate Fixes: annotations to PR description.

@mykaul mykaul requested a review from Copilot August 7, 2025 09:14
@mykaul mykaul marked this pull request as draft August 7, 2025 09:14
Copilot

This comment was marked as outdated.

@dkropachev dkropachev marked this pull request as ready for review August 7, 2025 17:52
@dkropachev
Copy link
Collaborator

@mykaul , let me know when you done, it looks good as is.

@mykaul
Copy link
Author

mykaul commented Aug 7, 2025

@mykaul , let me know when you done, it looks good as is.

I'm not, I'd like to understand the code better. I think it can be further slightly improved.

While trying to look at some random (flaky?) test (scylladb#510 (comment) )
I saw some (minor) improvements that can be made to make_token_replica_map():
1. Remove some redundant len() calls to outside the loop(s)
2. Align some variable names, start with num_ ... for them.
3. Move token_offset and host assignment within the loop to closer to where it's used.
4. Only add DCs and hosts that are in the map

All those are probably very very minor improvements, perhaps in a large cluster it'll be noticable.

Signed-off-by: Yaniv Kaul <[email protected]>
@mykaul mykaul force-pushed the make_token_replica_map branch from cd57c4d to 64c0e38 Compare August 8, 2025 06:23
@mykaul mykaul requested a review from Copilot August 8, 2025 06:25
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors the make_token_replica_map() method in cassandra/metadata.py to improve performance and code clarity through several optimizations:

  • Pre-filter hosts to only process those in datacenters with replication factors
  • Move length calculations outside loops to avoid redundant computations
  • Improve variable naming consistency with num_ prefixes
  • Reorganize code structure for better locality of use

@mykaul
Copy link
Author

mykaul commented Aug 8, 2025

@scylladb/python-driver-maint - please re-review. I hope I did not mess things up - CoPilot is being silly here, I suspect. I'm not sure why the whole CI is broken, doesn't seem to be caused by my changes.
Please review carefully, I don't think we have enough tests for this part, though it seems OK. Perhaps I can just do the removal of the len() stuff outside the loop. It's low impact anyway.

dc_racks[host.datacenter].add(host.rack)
hosts_per_dc[host.datacenter].add(host)
host_dc = host.datacenter
if host_dc in dc_rf_map:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Even better optimization would be to have a set of hosts run this logic over it, instead of running it for all tokens.

Copy link
Author

Choose a reason for hiding this comment

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

Maybe, but out of scope for this small PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants