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

Use gsl::narrow in heap.cpp #713

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Use gsl::narrow in heap.cpp #713

wants to merge 2 commits into from

Conversation

elazarg
Copy link
Collaborator

@elazarg elazarg commented Oct 2, 2024

Summary by CodeRabbit

  • New Features

    • Enhanced type safety in heap operations with updated methods.
    • Simplified type references in CrabStats for easier readability.
  • Bug Fixes

    • Improved handling of thread-local state reset in CrabStats.

Copy link

coderabbitai bot commented Oct 2, 2024

Walkthrough

The pull request introduces modifications to the Heap class in src/crab_utils/heap.hpp, changing the template parameter name from Comp to Comparator and enhancing type safety by utilizing gsl::narrow_cast and gsl::narrow<int> for type conversions. Additionally, the CrabStats and Stopwatch classes in src/crab_utils/stats.cpp have been updated to remove the crab:: namespace prefix from the lazy_allocator type, simplifying type references. The clear_thread_local_state method in CrabStats was also modified to ensure proper state reset.

Changes

File Change Summary
src/crab_utils/heap.hpp - Changed template parameter from Comp to Comparator.
- Updated inHeap and operator[] methods to use gsl::narrow_cast.
- Modified insert method to use gsl::narrow<int> for size conversion.
- Adjusted logic for resizing indices vector.
src/crab_utils/stats.cpp - Removed crab:: namespace prefix from lazy_allocator type in counters and sw declarations.
- Updated clear_thread_local_state to call clear() on both counters and sw.

Possibly related PRs

  • Modernize heap.hpp #700: The changes in this PR directly modify the Heap class in src/crab_utils/heap.hpp, including the same template parameter name change from Comp to Comparator and updates to the inHeap and operator[] methods, which align closely with the modifications in the main PR.

Poem

In the heap where rabbits play,
Comparators lead the way.
With types so safe, we hop along,
Clear states and stats, we sing our song!
No more prefixes, just pure delight,
In our code, everything feels right! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

📥 Commits

Files that changed from the base of the PR and between 23e8382 and 044e682.

📒 Files selected for processing (2)
  • src/crab_utils/heap.hpp (3 hunks)
  • src/crab_utils/stats.cpp (1 hunks)
🔇 Additional comments (8)
src/crab_utils/stats.cpp (3)

16-16: LGTM! Consistent with previous change.

This change aligns with the modification in the previous line, removing the crab:: namespace prefix from lazy_allocator. The same considerations mentioned in the previous comment apply here as well.


15-16: Summary: Changes improve code readability.

The modifications to remove the crab:: namespace prefix from lazy_allocator are consistent and improve code readability. These changes don't alter the functionality of the CrabStats class.

As a final step, please ensure that all necessary headers are included and that the code compiles successfully after these changes.

Run the following script to verify successful compilation:

#!/bin/bash
# Description: Verify successful compilation

# Test: Attempt to compile the file
echo "Attempting to compile src/crab_utils/stats.cpp:"
g++ -c src/crab_utils/stats.cpp -o /dev/null

15-15: LGTM! Verify lazy_allocator inclusion.

The removal of the crab:: namespace prefix simplifies the code. However, please ensure that lazy_allocator is properly included or defined in the current scope.

Run the following script to verify the lazy_allocator definition:

src/crab_utils/heap.hpp (5)

26-27: LGTM: Necessary inclusion for safe integer conversions.

The addition of #include <gsl/narrow> is appropriate as it provides access to gsl::narrow and gsl::narrow_cast functions, which are used later in the code for safe integer conversions. This change aligns well with the PR objective of using gsl::narrow.


91-92: LGTM: Improved type safety in bounds checking.

The use of gsl::narrow_cast here enhances type safety by providing a checked conversion from int to size_t. This is an appropriate use of narrow_cast as it's used in a bounds check where throwing an exception for a failed narrowing would be undesirable.


94-95: LGTM: Enhanced type safety in index bounds checking.

The use of gsl::narrow_cast here improves type safety by providing a checked conversion from int to size_t. This is an appropriate use of narrow_cast as it's used in an assertion where throwing an exception for a failed narrowing would be redundant.


111-113: LGTM: Improved logic in insert method.

The adjustment in the order of operations is correct. By moving the indices[n] = size; line after the size check and resize operation, it ensures that indices is properly resized before accessing indices[n]. This change prevents potential out-of-bounds access and improves the overall robustness of the insert method.


Line range hint 1-132: Summary: Excellent improvements in type safety and code clarity.

This PR successfully achieves its objective of incorporating gsl::narrow and gsl::narrow_cast to enhance type safety in integer conversions. The changes made to the Heap class implementation are well-thought-out and contribute to a more robust and safer codebase. Key improvements include:

  1. Proper inclusion of the necessary header for GSL narrow functions.
  2. More descriptive template parameter naming.
  3. Safe integer conversions in various methods using appropriate GSL narrow functions.
  4. Improved logic in the insert method to prevent potential out-of-bounds access.

These modifications align well with best practices for C++ programming and demonstrate a commitment to code quality and safety.

src/crab_utils/heap.hpp Show resolved Hide resolved
src/crab_utils/heap.hpp Outdated Show resolved Hide resolved
@coveralls
Copy link

coveralls commented Oct 2, 2024

Coverage Status

coverage: 90.292% (+0.001%) from 90.291%
when pulling 166ab80 on gsl-narrow-heap
into 257897c on main.

Signed-off-by: Elazar Gershuni <[email protected]>
Signed-off-by: Elazar Gershuni <[email protected]>
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 5

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

📥 Commits

Files that changed from the base of the PR and between 044e682 and 166ab80.

📒 Files selected for processing (2)
  • src/crab_utils/heap.hpp (3 hunks)
  • src/crab_utils/stats.cpp (1 hunks)
🔇 Additional comments (3)
src/crab_utils/stats.cpp (1)

16-16: LGTM! Consistent with previous change.

This change is consistent with the removal of the crab:: namespace prefix in the previous line, maintaining code style consistency throughout the file.

src/crab_utils/heap.hpp (2)

85-90: Appropriate use of [[nodiscard]] attribute on size() and empty() methods

Applying the [[nodiscard]] attribute to the size() and empty() methods is a good practice as it encourages callers to consider the return values, which are critical for controlling flow and ensuring correct behavior.


114-116: Ensure consistency after resizing indices

After correcting the condition in the insert method, verify that indices is properly resized and that all subsequent operations on indices[n] are valid. This ensures that the heap maintains its integrity and that all indices are correctly tracked.

Run the following script to confirm that indices has sufficient size:

src/crab_utils/stats.cpp Show resolved Hide resolved
src/crab_utils/heap.hpp Show resolved Hide resolved
src/crab_utils/heap.hpp Show resolved Hide resolved
src/crab_utils/heap.hpp Show resolved Hide resolved
src/crab_utils/heap.hpp Show resolved Hide resolved
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.

2 participants