Skip to content

refactor: Use default register position in PrefixTree; Add RegisterHandler constructor for multiple registers; Add register_id_t type alias.#88

Merged
davidlion merged 5 commits intoy-scope:mainfrom
SharafMohamed:register-handler-cleanup
Feb 24, 2025
Merged

refactor: Use default register position in PrefixTree; Add RegisterHandler constructor for multiple registers; Add register_id_t type alias.#88
davidlion merged 5 commits intoy-scope:mainfrom
SharafMohamed:register-handler-cleanup

Conversation

@SharafMohamed
Copy link
Contributor

@SharafMohamed SharafMohamed commented Feb 3, 2025

Description

  • Instead of the user arbitrarily providing a starting ID when creating registers, prefix tree now uses a default id.
  • Register handler updated to use new prefix tree constructor, this also allows register handler to create several registers at once.
  • Introduce register_id_t instead of using size_t.

Validation performed

  • Update test-register-handler.cpp.

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Introduced the ability to add multiple registers at once, streamlining register management.
  • Refactor

    • Enhanced register management with improved type safety and updated method signatures.
    • Added a default position constant for improved clarity in position-related operations.
  • Tests

    • Revised the initialization testing to accurately handle register creation scenarios.

@coderabbitai
Copy link

coderabbitai bot commented Feb 3, 2025

Walkthrough

This pull request introduces changes across the finite automata module and its tests. In the PrefixTree class, a new static constant cDefaultPos is added to serve as a default position value. In the RegisterHandler class, a new method add_registers has been implemented, and several existing method signatures have been updated to use the register_id_t type instead of size_t. Additionally, the inclusion of <cstddef> has been replaced with <cstdint>. In the tests, the initialization logic within handler_init has been modified to handle the first register differently.

Changes

File(s) Change Summary
src/.../PrefixTree.hpp Added static constant cDefaultPos (of type position_t with a value of 0) to the PrefixTree class.
src/.../RegisterHandler.hpp Added new method add_registers; updated add_register, set_register, copy_register, append_position, and get_reversed_positions to use register_id_t instead of size_t; replaced <cstddef> with <cstdint>.
tests/.../test-register-handler.cpp Modified handler_init to update register initialization logic—calling add_register without parameters for index 0 and with the index for other registers—and updated the function signature to use a const qualifier.

Sequence Diagram(s)

sequenceDiagram
    participant T as Test (handler_init)
    participant RH as RegisterHandler

    T->>RH: add_registers(num_registers)
    loop For each register index i
        alt i == 0
            RH->>RH: add_register()  (default register add)
        else
            RH->>RH: add_register(prefix_tree_parent_node_id = i)
        end
    end
    RH-->>T: vector of register IDs
Loading

Possibly related PRs

Suggested Reviewers

  • LinZhihao-723
  • davidlion

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 89c2bbc and 27ab84d.

📒 Files selected for processing (1)
  • tests/test-register-handler.cpp (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/test-register-handler.cpp
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: build (ubuntu-22.04, Release)
  • GitHub Check: build (ubuntu-22.04, Debug)
  • GitHub Check: build (macos-latest, Release)
  • GitHub Check: build (macos-latest, Debug)

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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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.

@SharafMohamed SharafMohamed force-pushed the register-handler-cleanup branch from fe20c94 to ed2378a Compare February 16, 2025 17:55
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: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6ed113a and ed2378a.

📒 Files selected for processing (3)
  • src/log_surgeon/finite_automata/PrefixTree.hpp (1 hunks)
  • src/log_surgeon/finite_automata/RegisterHandler.hpp (2 hunks)
  • tests/test-register-handler.cpp (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
`**/*.{cpp,hpp,java,js,jsx,ts,tsx}`: - Prefer `false ==

**/*.{cpp,hpp,java,js,jsx,ts,tsx}: - Prefer false == <expression> rather than !<expression>.

  • src/log_surgeon/finite_automata/PrefixTree.hpp
  • tests/test-register-handler.cpp
  • src/log_surgeon/finite_automata/RegisterHandler.hpp
🧠 Learnings (1)
src/log_surgeon/finite_automata/RegisterHandler.hpp (2)
Learnt from: SharafMohamed
PR: y-scope/log-surgeon#56
File: src/log_surgeon/finite_automata/RegisterHandler.hpp:0-0
Timestamp: 2024-11-27T22:25:35.608Z
Learning: In the `RegisterHandler` class in `src/log_surgeon/finite_automata/RegisterHandler.hpp`, the methods `add_register` and `append_position` rely on `emplace_back` and `m_prefix_tree.insert` to handle exceptions correctly and maintain consistent state without requiring additional exception handling.
Learnt from: SharafMohamed
PR: y-scope/log-surgeon#56
File: src/log_surgeon/finite_automata/RegisterHandler.hpp:0-0
Timestamp: 2024-11-27T21:56:13.425Z
Learning: In the `log_surgeon` project, header guards in C++ header files should include `_HPP` at the end to match the filename. For example, in `RegisterHandler.hpp`, the header guard should be `LOG_SURGEON_FINITE_AUTOMATA_REGISTER_HANDLER_HPP`.
🪛 GitHub Actions: Build
tests/test-register-handler.cpp

[error] 8-8: 'log_surgeon/finite_automata/Register.hpp' file not found.

🔇 Additional comments (8)
src/log_surgeon/finite_automata/RegisterHandler.hpp (6)

21-27: LGTM! Efficient implementation for adding multiple registers.

The method efficiently uses emplace_back and maintains type consistency with uint32_t.


29-43: LGTM! Well-designed overloads for register creation.

Both overloads maintain consistent state by using emplace_back and m_prefix_tree.insert, following the established pattern from previous implementations.


45-47: LGTM! Enhanced type safety with register_id_t.

The change from size_t to register_id_t improves type safety while maintaining the same functionality.


49-51: LGTM! Enhanced type safety with register_id_t.

The change from size_t to register_id_t improves type safety while maintaining the same functionality.


53-57: LGTM! Enhanced type safety with register_id_t.

The change from size_t to register_id_t improves type safety while maintaining the same functionality.


59-62: LGTM! Enhanced type safety with register_id_t.

The change from size_t to register_id_t improves type safety while maintaining the same functionality.

src/log_surgeon/finite_automata/PrefixTree.hpp (1)

27-27: LGTM! Added default position constant.

The cDefaultPos constant provides a clear, centralized default value for position initialization.

tests/test-register-handler.cpp (1)

23-27:

Details

✅ Verification successful

Verify register hierarchy changes.

The initialization logic has been modified to create a different hierarchy:

  • First register (i=0) is added at the root
  • Subsequent registers use their index as the parent node ID

Please verify if this change in hierarchy is intentional and aligns with the expected register relationships tested in the "Register relationships are maintained" section.


🏁 Script executed:

#!/bin/bash
# Description: Check if the test expectations match the new hierarchy

# Search for tests verifying register relationships
rg -A 10 "Register relationships are maintained" tests/

Length of output: 903


Register hierarchy behaviour verified.

The updated logic—adding the first register as a root (i.e. without a parent node) and adding subsequent registers with their index as the parent node ID—has been validated against the "Register relationships are maintained" test in tests/test-register-handler.cpp. The test’s expectations (e.g. the reversed positions order) confirm that the new hierarchy is working as intended.

#include <vector>

#include <log_surgeon/finite_automata/PrefixTree.hpp>
#include <log_surgeon/finite_automata/Register.hpp>
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix missing header file.

The pipeline is failing because 'log_surgeon/finite_automata/Register.hpp' cannot be found.

Please ensure that the Register.hpp file is created and properly placed in the finite_automata directory.

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: 0

🧹 Nitpick comments (4)
src/log_surgeon/finite_automata/RegisterHandler.hpp (4)

10-10: Add documentation for the type alias.

Consider adding a brief documentation comment explaining the purpose and constraints of register_id_t.

Add this documentation:

+/**
+ * Type alias for register identifiers.
+ * Uses uint32_t to ensure sufficient capacity for register indices while maintaining
+ * memory efficiency.
+ */
 using register_id_t = uint32_t;

22-28: Add input validation and documentation.

The method implementation looks good, but consider these improvements:

  1. Add input validation for num_reg_to_add
  2. Add documentation explaining the method's purpose and return value

Apply these changes:

+/**
+ * Adds multiple registers to the handler.
+ * @param num_reg_to_add Number of registers to add
+ * @return Vector containing the IDs of the newly added registers
+ * @throws std::invalid_argument if num_reg_to_add is 0
+ */
 auto add_registers(uint32_t const num_reg_to_add) -> std::vector<uint32_t> {
+    if (0 == num_reg_to_add) {
+        throw std::invalid_argument("Number of registers to add must be greater than 0");
+    }
     std::vector<uint32_t> added_registers;
+    added_registers.reserve(num_reg_to_add);  // Optimize for known size
     for (uint32_t i{0}; i < num_reg_to_add; ++i) {
         added_registers.emplace_back(add_register());
     }
     return added_registers;
 }

30-36: Add documentation for the method.

Add documentation to explain the method's purpose and return value.

Add this documentation:

+/**
+ * Adds a new register at the root of the prefix tree.
+ * @return ID of the newly added register
+ */
 auto add_register() -> register_id_t {

38-44: Add documentation for the overloaded method.

Add documentation to explain the method's purpose, parameter, and return value.

Add this documentation:

+/**
+ * Adds a new register with a specific parent node in the prefix tree.
+ * @param prefix_tree_parent_node_id ID of the parent node in the prefix tree
+ * @return ID of the newly added register
+ */
 auto add_register(PrefixTree::id_t const prefix_tree_parent_node_id) -> register_id_t {
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0e1ad00 and ce05abc.

📒 Files selected for processing (1)
  • src/log_surgeon/finite_automata/RegisterHandler.hpp (2 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
`**/*.{cpp,hpp,java,js,jsx,tpp,ts,tsx}`: - Prefer `false == ...

**/*.{cpp,hpp,java,js,jsx,tpp,ts,tsx}: - Prefer false == <expression> rather than !<expression>.

  • src/log_surgeon/finite_automata/RegisterHandler.hpp
🧠 Learnings (1)
src/log_surgeon/finite_automata/RegisterHandler.hpp (2)
Learnt from: SharafMohamed
PR: y-scope/log-surgeon#56
File: src/log_surgeon/finite_automata/RegisterHandler.hpp:0-0
Timestamp: 2024-11-27T22:25:35.608Z
Learning: In the `RegisterHandler` class in `src/log_surgeon/finite_automata/RegisterHandler.hpp`, the methods `add_register` and `append_position` rely on `emplace_back` and `m_prefix_tree.insert` to handle exceptions correctly and maintain consistent state without requiring additional exception handling.
Learnt from: SharafMohamed
PR: y-scope/log-surgeon#56
File: src/log_surgeon/finite_automata/RegisterHandler.hpp:0-0
Timestamp: 2024-11-27T21:56:13.425Z
Learning: In the `log_surgeon` project, header guards in C++ header files should include `_HPP` at the end to match the filename. For example, in `RegisterHandler.hpp`, the header guard should be `LOG_SURGEON_FINITE_AUTOMATA_REGISTER_HANDLER_HPP`.
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: build (ubuntu-22.04, Release)
  • GitHub Check: build (ubuntu-22.04, Debug)
  • GitHub Check: build (macos-latest, Release)
  • GitHub Check: build (macos-latest, Debug)
🔇 Additional comments (2)
src/log_surgeon/finite_automata/RegisterHandler.hpp (2)

4-4: LGTM! Good choice using fixed-width integer types.

The change to use <cstdint> instead of <cstddef> aligns well with the introduction of fixed-width types for register IDs.


46-63: LGTM! Consistent use of register_id_t type.

The changes to use register_id_t instead of size_t improve type safety and maintain consistency throughout the class.

@davidlion davidlion changed the title refactor: Prefix tree creates default register positions; register handler can add multiple registers; Add register_id_t. refactor: Use default register position in PrefixTree; Add RegisterHandler constructor for multiple registers; Add register_id_t type alias. Feb 24, 2025
@davidlion davidlion merged commit 9cfdf2e into y-scope:main Feb 24, 2025
9 checks passed
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