Skip to content

Statically allocate space for internal state instead of allocating this on the heap#19

Merged
tavdog merged 1 commit intotronbyt:static_testfrom
IngmarStein:main
Mar 21, 2025
Merged

Statically allocate space for internal state instead of allocating this on the heap#19
tavdog merged 1 commit intotronbyt:static_testfrom
IngmarStein:main

Conversation

@IngmarStein
Copy link
Copy Markdown
Member

No description provided.

@IngmarStein IngmarStein requested a review from tavdog March 21, 2025 08:05
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Hello @IngmarStein, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

Summary of Changes

This pull request focuses on improving memory management by statically allocating space for internal state instead of dynamically allocating it on the heap. This change primarily affects the display.cpp and gfx.c files. The goal is to reduce the risk of memory allocation failures and potentially improve performance by avoiding dynamic memory allocation.

Highlights

  • Static Allocation: The primary change is to allocate the _matrix object in display.cpp and the _state struct in gfx.c statically instead of using new and calloc respectively. This means the memory for these objects is allocated at compile time rather than during runtime.
  • Initialization Flag: In gfx.c, a boolean flag _initialized is introduced to ensure the graphics state is initialized only once, replacing the previous check that relied on the _state pointer being non-NULL.
  • Memory Management: The gfx_update function in gfx.c now directly uses the statically allocated _state struct, simplifying memory access and reducing the overhead of dynamic allocation for the graphics state.

Changelog

Click here to see the changelog
  • src/display.cpp
    • Replaced dynamic allocation of _matrix with static allocation.
    • Modified display_initialize to use the statically allocated _matrix object.
    • Updated display_set_brightness, display_shutdown, display_draw, and display_clear to use _matrix. instead of _matrix->.
    • Moved pin definitions and clock phase inversion logic closer to the mxconfig definition for better readability.
    • Removed unnecessary invert_clock_phase variable.
  • src/gfx.c
    • Replaced dynamic allocation of _state with static allocation.
    • Introduced _initialized flag to ensure initialization occurs only once.
    • Modified gfx_initialize and gfx_update to use the statically allocated _state struct.
    • Updated code to access members of _state using _state. instead of _state->.
    • Corrected line number reference for _state->task in gfx_initialize from line 79 to line 77.
    • Corrected line number reference for xSemaphoreTake(_state->mutex in gfx_loop from line 143 to line 141.
    • Corrected line number reference for xSemaphoreGive(_state->mutex in gfx_loop from line 161 to line 159.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.


From heap's expanse to static hold,
A tale of memory, bravely told.
No more the calloc's anxious plea,
Just fixed abode for you and me.

In C's domain, where pointers roam,
A static struct, now calls it home.
Efficiency's song, a sweet refrain,
As memory leaks, no longer stain.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request aims to improve memory management by statically allocating space for internal state instead of using heap allocation. This is a good optimization that can reduce memory fragmentation and improve performance. The changes look good overall, but there are a few areas that could be improved.

Summary of Findings

  • Static initialization: The _initialized flag should be initialized to false at its declaration to ensure proper initial state.
  • Error message clarity: The error message in gfx_update could be more specific about which value is NULL.

Merge Readiness

The changes in this pull request are a good step towards optimizing memory usage. However, the issues identified should be addressed before merging. I am unable to directly approve this pull request, and recommend that others review and approve this code before merging.

Comment thread src/gfx.c
Comment on lines 117 to 118
} else {
ESP_LOGE("main", "Buffer or input data is NULL");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

To improve clarity, the error message could specify which value is NULL. This will make debugging easier.

Suggested change
} else {
ESP_LOGE("main", "Buffer or input data is NULL");
ESP_LOGE("main", "Buffer (_state.buf) or input data (webp) is NULL");

@tavdog tavdog changed the base branch from main to pixoticker_static March 21, 2025 08:11
@tavdog tavdog changed the base branch from pixoticker_static to static_test March 21, 2025 08:19
@tavdog tavdog merged commit 6781809 into tronbyt:static_test Mar 21, 2025
1 check 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