Skip to content
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

Readability and branching Improvements #264

Merged

Conversation

kokroo
Copy link

@kokroo kokroo commented Nov 26, 2024

Description

There are several commits in this PR that improve code readability and reduce branching done by the CPU.
I would suggest going through each commit individually and then looking at the entire file as a whole.

Local test steps

I have run the test suite using "pytest tests" and they all pass. I have not altered the functionality of any code.

PR Acceptance Checklist

  • Unit tests passing.
  • Integration testing
  • CHANGELOG.md updated
  • Documentation updated (if needed).

@danielfromearth I read the contributor guidelines thoroughly and I am not sure if I actually need to add new tests or modify the changelog. I am using the standard test suite since I am not changing the input/output of the code. If anything should be added to the changelog, I'd be happy to do it :)


📚 Documentation preview 📚: https://ncompare--264.org.readthedocs.build/en/264/

@danielfromearth
Copy link
Collaborator

danielfromearth commented Dec 5, 2024

Hi @kokroo, this looks great!

Adding to the changelog would be helpful, like this:

- Readability and branching improvements ([#264](https://github.com/nasa/ncompare/pull/264)) ([**@kokroo**](https://github.com/kokroo))

@danielfromearth danielfromearth added the enhancement New feature or request label Dec 5, 2024
@danielfromearth danielfromearth self-assigned this Dec 10, 2024
@danielfromearth danielfromearth added the good first issue Good for newcomers label Dec 11, 2024
@danielfromearth danielfromearth self-requested a review December 12, 2024 21:55
@danielfromearth
Copy link
Collaborator

danielfromearth commented Dec 13, 2024

Hi @kokroo, if you can update the changelog soon, then we can merge this and get it in the next, 1.12.0, release!

Or would you like me to update the changelog?

@kokroo
Copy link
Author

kokroo commented Dec 21, 2024

@danielfromearth Hey Daniel,
I am sorry I did not respond sooner. Somehow GitHub notifications don't get to my e-mail reliably.

I added a line in the changelog under "changes". The tests are still passing even after merging the latest changes.
I have another PR in the works :)

Cheers and happy holidays!

The Path() constructor already handles both str and Path types effectively.
Added a check to ensure the provided suffix starts with a dot (.), which is standard for file extensions.
This prevents the creation of invalid file paths with malformed suffixes (e.g., .txt vs txt).

Path(should_be_path).with_suffix(suffix) is directly applied, reducing the number of steps.
Combined checks for str, int, and tuple into a single isinstance call using a tuple of types.
This reduces redundancy, improves clarity and performance . The TypeError is preserved to handle unsupported types.

Added the correct return type (str).
This function is no longer needed.
The intersection() method can deal with arbitrary objects but it is not needed here since we coerce everything into a string.

The & operator works at a lower level and is faster and sufficient for strings
Improvements:

Avoid unnecessary double conversions to sets:
Previously, set(a_sorted) and set(b_sorted) were resorted. The optimized code directly converts sequence_a and sequence_b into sets for fast lookups, creates a union using the low level "|" operator and then creates a sorted set. Sorting is performed only once on the union of the sets (a_set | b_set), rather than sorting a_sorted, b_sorted, and their combined union separately.

Simplified membership checking:
The if conditions now directly check the presence of an item in the sets instead of redundant checks.

Removed unreachable error check:
The raise ValueError condition is unnecessary since all items in all_items are guaranteed to come from either a_set or b_set.

The input-output behavior remains consistent while achieving faster execution.
@kokroo kokroo force-pushed the code-improvements branch 2 times, most recently from c4e6177 to 1abedb8 Compare December 21, 2024 16:59
@danielfromearth danielfromearth changed the base branch from develop to feature/kokroo-code-improvements December 23, 2024 15:04
@danielfromearth danielfromearth merged commit 947bf47 into nasa:feature/kokroo-code-improvements Dec 23, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants