Skip to content

changed InputBuffer#1696

Open
lwfl1111 wants to merge 1 commit intobdring:mainfrom
lwfl1111:InputBuffer
Open

changed InputBuffer#1696
lwfl1111 wants to merge 1 commit intobdring:mainfrom
lwfl1111:InputBuffer

Conversation

@lwfl1111
Copy link

@lwfl1111 lwfl1111 commented Sep 4, 2025

Subject: Refactor: Simplify Circular Buffer Index Calculation in InputBuffer

Hello,

Thank you for maintaining this project. I'd like to contribute a refactoring for the InputBuffer class, specifically the circular buffer implementation in InputBuffer.cpp.

Changes Made:

The original code in the write(uint8_t c), push(const char* data), and read() functions used conditional checks to handle buffer wrap-around. I've replaced this logic with modulo operations (% RXBUFFERSIZE) to simplify the index calculations.

Modified Functions:

size_t InputBuffer::write(uint8_t c): Simplified the write index calculation to (_RXbufferpos + _RXbufferSize) % RXBUFFERSIZE.

bool InputBuffer::push(const char* data): Simplified the index calculation within the loop to (_RXbufferpos + _RXbufferSize + i) % RXBUFFERSIZE. Also optimized it to use the pre-calculated data_size instead of calling strlen repeatedly.

int InputBuffer::read(): Simplified the read position update to (_RXbufferpos + 1) % RXBUFFERSIZE.

Benefits:

Conciseness & Readability: The code is shorter and more clearly expresses the circular buffer logic.

Maintainability: The modulo operator provides a standard and robust way to handle index wrapping.

Minor Efficiency: Removed redundant condition checks and an unnecessary strlen call.

The functional behavior remains entirely unchanged. This is a code cleanliness improvement focused on making the logic more elegant and easier to understand.

I have tested the changes to ensure all buffer operations continue to work correctly, including at the buffer boundaries.

Please let me know if you have any questions or feedback.

Thank you for your time and consideration.

Best regards,
[lwfl1111]

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.

1 participant