Skip to content

MINOR: Refactor snapshottableSize method for better readability and simplicity #20368

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: trunk
Choose a base branch
from

Conversation

chanbinme
Copy link

Motivation

The snapshottableSize(long epoch) method previously contained unnecessary nested conditions and multiple redundant return baseSize(); statements. This refactor aims to simplify the method's control flow and improve code readability.

Changes

  • Changed the condition from epoch == LATEST_EPOCH to epoch != LATEST_EPOCH to avoid unnecessary else block usage.
  • Consolidated the redundant return baseSize(); statements into a single return at the end of the method.
  • Ensured the iterator logic executes only when necessary, resulting in clearer control flow.

Before

int snapshottableSize(long epoch) {
    if (epoch == LATEST_EPOCH) {
        return baseSize();
    } else {
        Iterator<Snapshot> iterator = snapshotRegistry.iterator(epoch);
        while(iterator.hasNext()) {
            Snapshot snapshot = iterator.next();
            HashTier<T> tier = snapshot.getDelta(SnapshottableHashTable.this);
            if (tier != null) {
                return tier.size;
            }
        }
        return baseSize();
    }
}

After

int snapshottableSize(long epoch) {
    if (epoch != LATEST_EPOCH) {
        Iterator<Snapshot> iterator = snapshotRegistry.iterator(epoch);
        while(iterator.hasNext()) {
            Snapshot snapshot = iterator.next();
            HashTier<T> tier = snapshot.getDelta(SnapshottableHashTable.this);
            if (tier != null) {
                return tier.size;
            }
        }
    }
    return baseSize();
}

Please let me know if any further adjustments are needed.
Thank you for your review!

@github-actions github-actions bot added triage PRs from the community core Kafka Broker small Small PRs labels Aug 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Kafka Broker small Small PRs triage PRs from the community
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant