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

Improve memory management and string handling in ItemInput class #256

Merged
merged 3 commits into from
Oct 27, 2024

Conversation

forntoh
Copy link
Owner

@forntoh forntoh commented Oct 27, 2024

Summary by CodeRabbit

  • Bug Fixes
    • Improved memory management and string handling in the input functionality.
    • Enhanced efficiency of string operations when typing and clearing input values.

- Removed unnecessary dynamic memory allocation
- Updated value assignment logic for better efficiency
Copy link
Contributor

coderabbitai bot commented Oct 27, 2024

Walkthrough

The changes made to the ItemInput class in src/ItemInput.h focus on improving memory management and string handling. The typeChar method now utilizes a stack-allocated buffer for string concatenation instead of dynamic memory allocation, which simplifies the memory management process. The clear method has also been updated to modify the existing buffer directly, rather than assigning it a string literal. These adjustments enhance efficiency by reducing the reliance on dynamic allocations.

Changes

File Change Summary
src/ItemInput.h Modified typeChar to use a stack-allocated buffer for string concatenation; updated clear to set the first character of value to the null terminator instead of assigning a string literal.

Poem

In the burrow where code does flow,
A rabbit hops with a memory glow.
No more new for strings to bind,
Just stack and clear, oh how refined!
With each small change, we leap and cheer,
For efficient code brings us near! 🐇✨


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.

@coderabbitai coderabbitai bot changed the title @coderabbitai Improve memory management and string handling in ItemInput class Oct 27, 2024
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: 2

🧹 Outside diff range comments (1)
src/ItemInput.h (1)

Line range hint 1-304: Improve class design with proper memory management

The class needs several architectural improvements to ensure proper memory management and prevent resource leaks:

  1. Implement the Rule of Three (destructor, copy constructor, assignment operator)
  2. Consider using RAII principles
  3. Add clear ownership semantics for the value pointer

Here's a suggested implementation of the missing methods:

class ItemInput : public MenuItem {
public:
    // ... existing constructors ...

    // Destructor
    ~ItemInput() {
        delete[] value;
    }

    // Copy constructor
    ItemInput(const ItemInput& other)
        : MenuItem(other.text),
          value(new char[strlen(other.value) + 1]),
          view(other.view),
          cursor(other.cursor),
          callback(other.callback) {
        strcpy(value, other.value);
    }

    // Assignment operator
    ItemInput& operator=(const ItemInput& other) {
        if (this != &other) {
            MenuItem::operator=(other);
            char* newValue = new char[strlen(other.value) + 1];
            strcpy(newValue, other.value);
            delete[] value;
            value = newValue;
            view = other.view;
            cursor = other.cursor;
            callback = other.callback;
        }
        return *this;
    }

    // ... rest of the class ...
};

Consider also:

  1. Using smart pointers if available in your environment
  2. Implementing move semantics (Rule of Five) if C++11 is available
  3. Using a fixed-size buffer for embedded systems to avoid dynamic allocation
📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

📥 Commits

Files that changed from the base of the PR and between 02f7235 and 8090f51.

📒 Files selected for processing (1)
  • src/ItemInput.h (2 hunks)

src/ItemInput.h Outdated Show resolved Hide resolved
@forntoh forntoh added the enhancement Improve readability/performance/maintainability label Oct 27, 2024
- Dynamically allocate buffer for value
- Update value pointer after concatenation
- Free previous value memory allocation
@forntoh forntoh marked this pull request as ready for review October 27, 2024 10:33
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: 3

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

📥 Commits

Files that changed from the base of the PR and between 8090f51 and 894f51c.

📒 Files selected for processing (1)
  • src/ItemInput.h (2 hunks)

src/ItemInput.h Show resolved Hide resolved
src/ItemInput.h Show resolved Hide resolved
Copy link
Contributor

Memory usage change @ 894f51c

Board flash % RAM for global variables %
STMicroelectronics:stm32:GenF1 💚 -4 - 0 -0.01 - 0.0 0 - 0 0.0 - 0.0
arduino:avr:uno 💚 -4 - 0 -0.01 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:mkr1000 💚 -4 - 0 -0.0 - 0.0 0 - 0 0.0 - 0.0
esp32:esp32:esp32 🔺 0 - +4 0.0 - 0.0 0 - 0 0.0 - 0.0
esp8266:esp8266:huzzah N/A N/A N/A N/A
Click for full report table
Board examples/Basic
flash
% examples/Basic
RAM for global variables
% examples/ButtonAdapter
flash
% examples/ButtonAdapter
RAM for global variables
% examples/Callbacks
flash
% examples/Callbacks
RAM for global variables
% examples/InputRotary
flash
% examples/InputRotary
RAM for global variables
% examples/IntFloatValues
flash
% examples/IntFloatValues
RAM for global variables
% examples/KeyboardAdapter
flash
% examples/KeyboardAdapter
RAM for global variables
% examples/List
flash
% examples/List
RAM for global variables
% examples/SimpleRotary
flash
% examples/SimpleRotary
RAM for global variables
% examples/SSD1803A_I2C
flash
% examples/SSD1803A_I2C
RAM for global variables
% examples/RTOS
flash
% examples/RTOS
RAM for global variables
%
STMicroelectronics:stm32:GenF1 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 -4 -0.01 0 0.0 0 0.0 0 0.0 -4 -0.01 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0
arduino:avr:uno 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 -4 -0.01 0 0.0 0 0.0 0 0.0 -4 -0.01 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0
arduino:samd:mkr1000 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 -4 -0.0 0 0.0 0 0.0 0 0.0 -4 -0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0
esp32:esp32:esp32 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 4 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0
esp8266:esp8266:huzzah N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A
Click for full report CSV
Board,examples/Basic<br>flash,%,examples/Basic<br>RAM for global variables,%,examples/ButtonAdapter<br>flash,%,examples/ButtonAdapter<br>RAM for global variables,%,examples/Callbacks<br>flash,%,examples/Callbacks<br>RAM for global variables,%,examples/InputRotary<br>flash,%,examples/InputRotary<br>RAM for global variables,%,examples/IntFloatValues<br>flash,%,examples/IntFloatValues<br>RAM for global variables,%,examples/KeyboardAdapter<br>flash,%,examples/KeyboardAdapter<br>RAM for global variables,%,examples/List<br>flash,%,examples/List<br>RAM for global variables,%,examples/SimpleRotary<br>flash,%,examples/SimpleRotary<br>RAM for global variables,%,examples/SSD1803A_I2C<br>flash,%,examples/SSD1803A_I2C<br>RAM for global variables,%,examples/RTOS<br>flash,%,examples/RTOS<br>RAM for global variables,%
STMicroelectronics:stm32:GenF1,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,-4,-0.01,0,0.0,0,0.0,0,0.0,-4,-0.01,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0
arduino:avr:uno,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,-4,-0.01,0,0.0,0,0.0,0,0.0,-4,-0.01,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0
arduino:samd:mkr1000,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,-4,-0.0,0,0.0,0,0.0,0,0.0,-4,-0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,,,,
esp32:esp32:esp32,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,4,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0
esp8266:esp8266:huzzah,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,,,,

@forntoh forntoh added bugfix I have fixed a bug and removed bugfix I have fixed a bug labels Oct 27, 2024
@forntoh forntoh merged commit 5155935 into master Oct 27, 2024
21 checks passed
@forntoh forntoh deleted the enhancement/input branch October 27, 2024 10:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improve readability/performance/maintainability
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant