-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Reduce flash size of TetrisAI_V2 by 97% #5317
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
base: main
Are you sure you want to change the base?
Conversation
Main branch without Tetris Flash: [======== ] 79.8% (used 1255301 bytes from 1572864 bytes) Main branch with Tetris (+196kb) Flash: [========= ] 92.3% (used 1452049 bytes from 1572864 bytes) This commit with Tetris (+6kb, 97% less flash) Flash: [======== ] 80.2% (used 1261625 bytes from 1572864 bytes)
WalkthroughThis PR removes unused include dependencies from three header files in the TetrisAI_v2 module and refactors the TetrisBag class by removing a public method overload and replacing std::rotate with a manual loop implementation. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes 🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this 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
🤖 Fix all issues with AI agents
In `@usermods/TetrisAI_v2/tetrisbag.h`:
- Around line 86-92: queuePiece() can underflow when the queue is empty because
piecesQueue.size() - 1 will wrap and cause out-of-range access; fix by returning
early (or asserting) when the queue is empty (e.g., check piecesQueue.empty() or
queueLength == 0) before shifting elements and assigning getNextPiece(),
ensuring you guard the piecesQueue access in queuePiece().
| void queuePiece() | ||
| { | ||
| //move vector to left | ||
| std::rotate(piecesQueue.begin(), piecesQueue.begin() + 1, piecesQueue.end()); | ||
| for (uint8_t i = 1; i < piecesQueue.size(); i++) { | ||
| piecesQueue[i - 1] = piecesQueue[i]; | ||
| } | ||
| piecesQueue[piecesQueue.size() - 1] = getNextPiece(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Guard against empty queue to avoid out-of-range access.
If queueLength == 0, piecesQueue.size() - 1 underflows and the assignment is out-of-bounds. Add a quick empty check (or assert) to keep this safe.
🐛 Suggested fix
void queuePiece()
{
+ if (piecesQueue.empty()) return;
//move vector to left
for (uint8_t i = 1; i < piecesQueue.size(); i++) {
piecesQueue[i - 1] = piecesQueue[i];
}
piecesQueue[piecesQueue.size() - 1] = getNextPiece();
}🤖 Prompt for AI Agents
In `@usermods/TetrisAI_v2/tetrisbag.h` around lines 86 - 92, queuePiece() can
underflow when the queue is empty because piecesQueue.size() - 1 will wrap and
cause out-of-range access; fix by returning early (or asserting) when the queue
is empty (e.g., check piecesQueue.empty() or queueLength == 0) before shifting
elements and assigning getNextPiece(), ensuring you guard the piecesQueue access
in queuePiece().
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Disregard, for loop is never entered if piecesQueue.size() < 1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Aircoookie You're absolutely right that the for loop won't execute when the queue is empty. I was also concerned about the assignment on the next line (piecesQueue[piecesQueue.size() - 1] = getNextPiece();), which could underflow if size() == 0. However, if the queue is always initialized with a non-zero size in practice, that wouldn't be an issue. I'll defer to your judgment on this! 👍
Aircoookie
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thank you! Awesome that we can achieve such huge flash savings by avoiding these imports
Main branch without Tetris
Flash: [======== ] 79.8% (used 1255301 bytes from 1572864 bytes)
Main branch with Tetris (+196kb)
Flash: [========= ] 92.3% (used 1452049 bytes from 1572864 bytes)
This commit with Tetris (+6kb, 97% less flash)
Flash: [======== ] 80.2% (used 1261625 bytes from 1572864 bytes)
Summary by CodeRabbit
Release Notes
Refactor
Chores
✏️ Tip: You can customize this high-level summary in your review settings.