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

Change explicit bit fields to a packed 128-bit int for the short pointers. #58

Closed
wants to merge 4 commits into from

Conversation

ujjwal-shekhar
Copy link
Contributor

@ujjwal-shekhar ujjwal-shekhar commented Oct 9, 2024

  • Removed the 14 short pointers, each of size SHORT_DELTA bits; instead.
  • There are now 2 128-bit integers that have 7 packed SHORT_DELTA bits.
  • Added an unsigned num_occupied to directly get the total occupancy of a chunk.

Summary by CodeRabbit

  • Bug Fixes
    • Adjusted benchmark tests for tree structures to disable certain depth tests (depths 6, 7, and 8).
    • Updated correctness tests for tree structures to modify depth limits, impacting node levels during execution.
  • New Features
    • Enhanced internal representation of tree nodes for improved memory efficiency and management.
    • Added tracking for the number of occupied short delta pointers in tree structures.
  • Documentation
    • Improved output visibility during testing by commenting out console outputs in traversal functions.

…compiler dependent / UB).

- For more details on the bug, check `L100 - L115`. Uncommenting the lines will print the mask value before and after left shifting it. Notice that the mask never shifts
- This causes the last child pointers of the node at position 1 of the chunk, to get overwritten.
- Which finally leads to a preorder traversal mismatch.
Copy link
Contributor

coderabbitai bot commented Oct 9, 2024

Walkthrough

The changes involve modifications to benchmark and correctness tests for tree structures, specifically hhds::tree<int> and lh::tree<int>. Benchmark registrations for depth values 6, 7, and 8 have been commented out, while correctness tests have adjusted tree construction depths. Additionally, significant updates to the Tree_pointers and tree classes in hhds/tree.hpp include changes to constants, data structures, methods, and error handling, enhancing the internal representation and management of tree nodes.

Changes

File(s) Change Summary
hhds/tests/add_append_bench/chip_typical_long_tree_bench.cpp
hhds/tests/traversal_bench/preorder_bench/chip_typical_long_tree_bench.cpp
Commented out benchmark registrations for depths 6, 7, and 8 for both hhds and lh trees.
hhds/tests/chip_typical_correctness.cpp
hhds/tests/chip_typical_long_correctness.cpp
Adjusted loop depths in test_chip_tree function; increased depth in one test and decreased in another. Commented out output of node values in traversal functions.
hhds/tree.hpp Modified constants, updated Tree_pointers class structure, added num_occupied, updated methods and error handling, and refined logic for child pointers.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Benchmark
    participant Tree

    User->>Benchmark: Request benchmark for tree
    Benchmark->>Tree: Execute benchmark for depths 1-5
    Tree->>Benchmark: Return results
    Benchmark->>User: Display benchmark results
Loading

🐇 In the forest, where trees grow tall,
Benchmarks now rest, no longer to call.
Depths six, seven, and eight, we’ve set aside,
For simpler tests, our pride won’t hide.
With branches adjusted, our trees stand bright,
Hop along, dear friend, all feels just 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
Contributor

@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: 10

🧹 Outside diff range and nitpick comments (8)
hhds/tests/chip_typical_correctness.cpp (2)

23-23: Consider using a debug flag instead of commenting out.

While removing debug output is generally acceptable, using a debug flag would provide more flexibility for future testing and debugging.

Consider replacing the commented line with a conditional statement:

-        // std::cout << node << std::endl;
+        if (DEBUG_OUTPUT) {
+            std::cout << node << std::endl;
+        }

This approach allows easy toggling of debug output without code changes.


142-151: Approved: Enhanced result visibility. Consider minor improvement.

The addition of output statements for preorder traversal results improves the visibility of test outcomes, which is beneficial for debugging and verification.

For consistency, consider adding a newline character to the HHDS Preorder output:

 std::cout << "HHDS Preorder: \n";
 for (auto i : hhds_preorder) {
     std::cout << i << " ";
 }
+std::cout << std::endl;

This change would make the output format consistent with the LH Preorder output.

hhds/tree.hpp (6)

76-78: Potential misuse of unsigned short in bitfield

Declaring num_occupied as unsigned short with a bitfield width of CHUNK_SHIFT (3 bits) may be unnecessary. Since the width of the bitfield determines the actual storage size, consider using unsigned to make the intent clearer.

-    unsigned short num_occupied         : CHUNK_SHIFT;
+    unsigned num_occupied               : CHUNK_SHIFT;

394-399: Incomplete information in print_tree method output

In the print_tree method, the output includes several pointer values but may omit important details such as the first_child_s and last_child_s values.

Consider extending the debug output to include num_occupied, first_child_s, and last_child_s to provide a more comprehensive view of the tree's state:

std::cout << "Num Occ: " << pointers_stack[i].get_num_occupied() << std::endl;
std::cout << "First Child Short: " << pointers_stack[i].get_first_child_s_at(0) << std::endl;
std::cout << "Last Child Short: " << pointers_stack[i].get_last_child_s_at(0) << std::endl;

854-858: Redundant comment in get_parent method

The comment above the get_parent method includes an @assert tag mentioning index range checks, but the assertion is already present in the code.

Consider removing the redundant comment or updating it to provide additional useful information.

- * @assert If the index is out of range

Line range hint 957-975: Potential off-by-one error in is_last_child method

In the is_last_child method, the condition:

return pointers_stack[self_chunk_id].get_num_occupied() == self_chunk_offset;

may not accurately determine if the node is the last child due to zero-based indexing.

Consider adjusting the condition to:

-return pointers_stack[self_chunk_id].get_num_occupied() == self_chunk_offset;
+return pointers_stack[self_chunk_id].get_num_occupied() == self_chunk_offset + 1;

This accounts for the zero-based index and ensures correct behavior.


Line range hint 987-999: Clarify logic in is_first_child method

In the is_first_child method, the code checks if self_chunk_offset is zero:

return !self_chunk_offset;

While this works, it may be clearer to explicitly compare against zero for readability.

-return !self_chunk_offset;
+return self_chunk_offset == 0;

Line range hint 1168-1190: Improper initialization in add_root method

In the add_root method, there is a commented-out line setting num_occupied:

// pointers_stack[ROOT].set_num_occupied(0);

This suggests that num_occupied may not be correctly initialized for the root node.

Ensure that num_occupied is properly set when adding the root node:

+    pointers_stack[ROOT_CHUNK_INDEX].set_num_occupied(1);

This sets the num_occupied to 1, reflecting the root node's occupancy.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between b015a42 and 91d692d.

📒 Files selected for processing (5)
  • hhds/tests/add_append_bench/chip_typical_long_tree_bench.cpp (1 hunks)
  • hhds/tests/chip_typical_correctness.cpp (3 hunks)
  • hhds/tests/chip_typical_long_correctness.cpp (1 hunks)
  • hhds/tests/traversal_bench/preorder_bench/chip_typical_long_tree_bench.cpp (1 hunks)
  • hhds/tree.hpp (32 hunks)
✅ Files skipped from review due to trivial changes (2)
  • hhds/tests/add_append_bench/chip_typical_long_tree_bench.cpp
  • hhds/tests/traversal_bench/preorder_bench/chip_typical_long_tree_bench.cpp
🧰 Additional context used
🔇 Additional comments (8)
hhds/tests/chip_typical_long_correctness.cpp (2)

82-82: Verify the impact of reduced tree depth on test coverage

The tree construction depth has been reduced from 7 to 6. While this change might be necessary to accommodate the new packed 128-bit int representation for short pointers (as mentioned in the PR objectives), it's important to ensure that this reduction doesn't compromise the test coverage.

Please confirm that:

  1. The reduced depth is sufficient to test all aspects of the new short pointer implementation.
  2. The test still covers edge cases and potential issues that might arise in deeper trees.

If needed, consider adding additional test cases or modifying other parameters (like the number of children per node) to maintain comprehensive test coverage.


Line range hint 1-150: Verify test coverage for new short pointer implementation

While this test file has minimal changes, the PR objectives mention significant modifications to short pointers and the introduction of num_occupied. It's crucial to ensure that this test still adequately covers the new implementation.

Please confirm that:

  1. The test cases in this file still provide sufficient coverage for the new packed 128-bit int representation of short pointers.
  2. The num_occupied functionality is tested, either in this file or in other test files.
  3. Any new edge cases or potential issues introduced by the changes are adequately tested.

Consider adding new test cases or modifying existing ones if necessary to ensure comprehensive coverage of the new implementation.

If you need help designing additional test cases or modifying existing ones to cover the new implementation, please let me know, and I'd be happy to assist.

hhds/tests/chip_typical_correctness.cpp (3)

104-104: Approved: Increased tree depth. Please clarify the rationale.

The tree depth has been increased from 7 to 8, which will create a more complex tree structure for testing. This change seems beneficial for more thorough testing.

Could you please clarify the reason for increasing the depth? Is this related to the changes in short pointer implementation mentioned in the PR objectives?


132-135: Approved: Improved code readability.

The addition of empty lines improves the code's readability by separating logical blocks.


Line range hint 1-185: Please clarify the relationship between these changes and the PR objectives.

The changes in this file primarily focus on enhancing the test coverage and result visibility. However, they don't directly implement or test the changes to short pointers mentioned in the PR objectives (changing explicit bit fields to a packed 128-bit int).

Could you please explain how these test modifications relate to the short pointer implementation changes? Are there any specific aspects of the new implementation that these enhanced tests are designed to verify?

hhds/tree.hpp (3)

57-58: Ensure that updated CHUNK_BITS and SHORT_DELTA maintain the 512-bit limit

The constants CHUNK_BITS and SHORT_DELTA have been updated to 50 and 18, respectively. According to the notes, the total size of Tree_pointers should not exceed 512 bits. Please verify that these updates keep the total size within this limit.

You can calculate the total bit size using the updated constants:

  • parent: CHUNK_BITS + CHUNK_SHIFT bits
  • next_sibling: CHUNK_BITS bits
  • prev_sibling: CHUNK_BITS bits
  • first_child_l: CHUNK_BITS bits
  • last_child_l: CHUNK_BITS bits
  • num_occupied: CHUNK_SHIFT bits
  • first_child_s: 128 bits
  • last_child_s: 128 bits

Total bits = (50 + 3) + 4 * 50 + 3 + 2 * 128 = 53 + 200 + 3 + 256 = 512 bits.

Since the total is 512 bits, the size limit is maintained.


84-100: Ensure correct bit manipulation in helper functions

The helper functions _get_first_child_s, _set_first_child_s, _get_last_child_s, and _set_last_child_s perform bit manipulation on the __int128 variables. Carefully verify that the shift and mask operations correctly extract and set the desired bits without causing overlaps or data corruption.

To verify the correctness, consider adding unit tests that cover edge cases, such as:

  • Setting and getting values at the minimum and maximum indices.
  • Using the minimum and maximum possible Short_delta values.

Would you like assistance in writing these unit tests or reviewing the bit manipulation logic in detail?


364-365: Use of GI macro without proper context

The GI macro is used in get_data without a clear indication of what it expands to:

GI(_check_idx_exists(idx), data_stack[idx].has_value(),
   "Index out of range or no data at the index");

Confirm that the GI macro is correctly defined and that it appropriately handles multiple conditions. Ensure that the macro provides meaningful error messages and does not suppress necessary checks.

Would you like assistance in reviewing the definition of GI or suggesting alternatives for clearer code?

hhds/tree.hpp Show resolved Hide resolved
Comment on lines +234 to +236
I(_check_idx_exists(first_child), "First child index out of range");
I(_check_idx_exists(new_parent_id), "New parent index out of range");

Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Incomplete or incorrect assertion messages

The assertions in _update_parent_pointer use the I macro with messages that may not provide sufficient context:

I(_check_idx_exists(first_child), "First child index out of range");
I(_check_idx_exists(new_parent_id), "New parent index out of range");

Consider enhancing the assertion messages to include the actual values of first_child and new_parent_id to aid in debugging:

I(_check_idx_exists(first_child), "First child index out of range: " << first_child);
I(_check_idx_exists(new_parent_id), "New parent index out of range: " << new_parent_id);

hhds/tree.hpp Show resolved Hide resolved
hhds/tree.hpp Show resolved Hide resolved
hhds/tree.hpp Show resolved Hide resolved
hhds/tree.hpp Show resolved Hide resolved
hhds/tree.hpp Show resolved Hide resolved
Comment on lines +80 to +81
__int128 first_child_s;
__int128 last_child_s;
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Portability concerns with __int128 type

The use of __int128 for first_child_s and last_child_s may cause portability issues, as __int128 is a compiler-specific extension supported by GCC and Clang on certain architectures. This could limit the code's compatibility with other compilers or platforms.

Consider using a portable alternative, such as an array of uint64_t, to represent the 128-bit integers:

-    __int128 first_child_s;
-    __int128 last_child_s;
+    std::array<uint64_t, 2> first_child_s;
+    std::array<uint64_t, 2> last_child_s;

Update the related methods to handle the new data structure appropriately.

Committable suggestion was skipped due to low confidence.

hhds/tree.hpp Show resolved Hide resolved
@renau renau closed this Oct 15, 2024
@renau
Copy link
Member

renau commented Oct 15, 2024

There are conflicts that need to be solved before merging.

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