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

Conserve 8 bytes on Event #12

Merged
merged 4 commits into from
Dec 4, 2024
Merged

Conserve 8 bytes on Event #12

merged 4 commits into from
Dec 4, 2024

Conversation

maksimdrachov
Copy link
Member

@maksimdrachov maksimdrachov commented Dec 4, 2024

Summary by CodeRabbit

  • New Features

    • Simplified event deadline management by removing optional handling, leading to clearer scheduling and cancellation processes.
  • Bug Fixes

    • Improved test clarity and reliability by updating deadline access methods, reducing potential errors related to optional values.

Copy link

coderabbitai bot commented Dec 4, 2024

Walkthrough

The changes in this pull request focus on modifying the Event class within the olga_scheduler namespace to streamline deadline management. The deadline_ member variable type has been changed from std::optional<TimePoint> to TimePoint, initialized to TimePoint::min(). This adjustment simplifies the logic for checking and resetting deadlines, as well as updating related methods. The test file has also been updated to reflect these changes, removing unnecessary calls to .value() when accessing deadlines.

Changes

File Change Summary
include/olga_scheduler/olga_scheduler.hpp - Changed std::optional<TimePoint> deadline_; to TimePoint deadline_ = TimePoint::min(); in the Event class.
- Updated getDeadline() to return TimePoint directly.
- Adjusted cancel() and destructor logic.
tests/test_olga_scheduler.cpp - Removed .value() calls when accessing deadlines in tests, now directly calling getDeadline().time_since_epoch().
- Updated assertions to reflect the new handling of deadlines.

Possibly related issues

  • Conserve 8 bytes per event #11: The changes directly address the objective of unwrapping std::optional<TimePoint> and using TimePoint::min() to represent inactive events, aligning with the issue's proposal.

Poem

🐇 In the scheduler's den, where events do play,
A change was made to brighten the day.
No more optional fuss, just a clear, bright line,
With deadlines set, everything's fine!
Hop along, dear friends, let the timepoints gleam,
In the world of events, we dance and dream! 🌟


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

🧹 Outside diff range and nitpick comments (1)
include/olga_scheduler/olga_scheduler.hpp (1)

154-154: LGTM! Memory optimization achieved through type change.

The change from std::optional<TimePoint> to TimePoint with TimePoint::min() as sentinel value is an effective optimization that:

  1. Reduces memory usage by 8 bytes per Event instance
  2. Potentially improves cache locality
  3. Eliminates optional-checking overhead

This is a good example of using domain knowledge (TimePoint::min() being a valid sentinel) to optimize memory usage without sacrificing functionality.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 29ffc4c and 6bcb164.

📒 Files selected for processing (2)
  • include/olga_scheduler/olga_scheduler.hpp (5 hunks)
  • tests/test_olga_scheduler.cpp (7 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/test_olga_scheduler.cpp
🔇 Additional comments (6)
include/olga_scheduler/olga_scheduler.hpp (6)

84-91: LGTM! Effective optimization of deadline checking.

The change from std::optional to using TimePoint::min() as a sentinel value is a good optimization that maintains the same semantics while reducing memory usage.


118-118: LGTM! Assertion maintains the same invariant.

The assertion correctly validates that the event is unscheduled upon destruction using the new sentinel value approach.


125-125: LGTM! Move constructor maintains proper semantics.

The move constructor correctly handles the deadline transfer and sets the moved-from object's deadline to the sentinel value.


139-139: LGTM! Comparison maintains correct ordering.

The direct comparison maintains the same ordering semantics while being more efficient by avoiding optional access.


326-326: LGTM! Consistent usage of new getDeadline() return type.

The spin() method correctly uses the new direct access pattern, maintaining the same functionality while being more efficient.


98-98: Verify client code compatibility with new getDeadline() return type.

The return type change from std::optional<TimePoint> to TimePoint might affect client code that expects an optional return value.

✅ Verification successful

Let me analyze the search results to verify the impact of the return type change.


Based on the search results, I can now provide the final response:

Return type change from optional to non-optional is safe

The change from std::optional<TimePoint> to TimePoint is consistent with the codebase usage:

  • All client code already treats the return value as non-optional, as seen in test assertions and comparisons
  • The class invariant (documented above the method) guarantees that events in the tree always have a valid deadline
  • Special cases use TimePoint::min() for canceled/unscheduled events instead of relying on optional
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for getDeadline() usage in client code
rg "getDeadline\(\)" --type cpp -B 2 -A 2

Length of output: 7469


Script:

#!/bin/bash
# Search for the old declaration to confirm the change
rg "std::optional.*getDeadline" --type cpp

Length of output: 44

Copy link
Collaborator

@serges147 serges147 left a comment

Choose a reason for hiding this comment

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

Plz see couple minor issues

tests/test_olga_scheduler.cpp Outdated Show resolved Hide resolved
include/olga_scheduler/olga_scheduler.hpp Show resolved Hide resolved
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

🧹 Outside diff range and nitpick comments (3)
include/olga_scheduler/olga_scheduler.hpp (1)

139-139: Consider extracting TimePoint comparison to a named constant.

The comparison could be more readable with a named constant:

-                return (dead >= other.deadline_) ? +1 : -1;
+                static constexpr int LATER = +1, EARLIER = -1;
+                return (dead >= other.deadline_) ? LATER : EARLIER;
tests/test_olga_scheduler.cpp (2)

190-193: Improve assertion readability with custom matchers.

Consider creating custom matchers to make the test intentions clearer:

// At the top of the file with other using declarations:
using IsScheduled = testing::Gt;
using IsNotScheduled = testing::Eq;

// Then in the test:
EXPECT_THAT(evt_a.getDeadline(), IsScheduled(Loop::time_point::min())) << "Event A should be scheduled";
EXPECT_THAT(evt_c.getDeadline(), IsNotScheduled(Loop::time_point::min())) << "Event C should not be scheduled";

273-292: Add descriptive comments for timing expectations.

The test would be clearer with comments explaining the expected timing behavior:

+    // Initial scheduling should be at now + period
     EXPECT_THAT(evl.getTree()[0U]->getDeadline().time_since_epoch(), 110ms);

+    // After execution, next deadline should be at execution time + period
     EXPECT_THAT(evl.getTree()[0U]->getDeadline().time_since_epoch(), 140ms);  // Skipped ahead!

+    // Verify phase error accumulation behavior
     EXPECT_THAT(evl.getTree()[0U]->getDeadline().time_since_epoch(), 210ms);  // Skipped ahead!
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 6bcb164 and 0bd1a33.

📒 Files selected for processing (2)
  • include/olga_scheduler/olga_scheduler.hpp (5 hunks)
  • tests/test_olga_scheduler.cpp (7 hunks)
🔇 Additional comments (4)
include/olga_scheduler/olga_scheduler.hpp (3)

96-98: Documentation needs to be updated to reflect the new implementation.

The documentation still refers to an optional type, but the implementation has changed to use TimePoint::min() as a sentinel value.


84-91: LGTM: Cancel implementation maintains correct semantics.

The change from optional check to TimePoint::min() comparison is a clean implementation that preserves the original behavior.


326-326: LGTM: Spin implementation correctly uses the new deadline access pattern.

The change simplifies the code by removing the optional handling while maintaining the same functionality.

tests/test_olga_scheduler.cpp (1)

47-48: Consider using more specific test assertions.

The EXPECT_THAT assertions for type ID comparison could be more specific and readable.

-    EXPECT_THAT(decltype(evt)::_get_type_id_(), ElementsAreArray(event_type_id));
+    EXPECT_THAT(decltype(evt)::_get_type_id_(), ElementsAreArray(event_type_id))
+        << "Event type ID mismatch";

Also applies to: 112-112, 276-276, 305-305

@maksimdrachov maksimdrachov merged commit c6d852d into master Dec 4, 2024
15 checks passed
@maksimdrachov maksimdrachov deleted the conserve-bytes branch December 4, 2024 14:44
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