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

Region-based allocation for AST nodes #122

Merged
merged 3 commits into from
Feb 12, 2025
Merged

Conversation

SIGMazer
Copy link
Contributor

@SIGMazer SIGMazer commented Feb 8, 2025

Description

As the AST grows more complex, managing it becomes increasingly difficult, and free_ast becomes larger. This PR introduces a single Region to allocate all AST nodes, allowing them to be freed at once, simplifying memory management and reducing overhead.

Related Issue

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • Performance improvement
  • Refactor

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have documented my changes in the code or documentation
  • I have added tests that prove my changes work (if applicable)
  • I have run the unit tests locally
  • I have run the valgrind memory tests locally
  • All new and existing tests pass

@SIGMazer SIGMazer requested a review from araujo88 as a code owner February 8, 2025 16:28
@SIGMazer SIGMazer added enhancement New feature or request refactor Modify existing working code but keeping the same functionality. labels Feb 8, 2025
Signed-off-by: SIGMazer <[email protected]>
@SIGMazer
Copy link
Contributor Author

@araujo88 can you take a look

@araujo88
Copy link
Member

@SIGMazer can you explain in more detail how this region-based allocation works? Or provide a reference?

@SIGMazer
Copy link
Contributor Author

@SIGMazer can you explain in more detail how this region-based allocation works? Or provide a reference?

We pre-allocate a memory chunk (Arena) and use it to allocate all ASTNode instances. This approach allows us to free the entire Arena at once, instead of traversing each ASTNode individually and freeing them one by one.

Reference

@araujo88
Copy link
Member

@SIGMazer can you explain in more detail how this region-based allocation works? Or provide a reference?

We pre-allocate a memory chunk (Arena) and use it to allocate all ASTNode instances. This approach allows us to free the entire Arena at once, instead of traversing each ASTNode individually and freeing them one by one.

Reference

* https://en.wikipedia.org/wiki/Region-based_memory_management

* https://www.gingerbill.org/article/2019/02/08/memory-allocation-strategies-002/

Very interesting! I wasn't familiar with that approach. Should we be concerned with the disadvantages? I understand that, as for the current usages of brainrot right now (which are small programs) this shouldn't be a concern.

Region-based memory management works best when the number of regions is relatively small and each contains many objects; programs that contain many sparse regions will exhibit internal fragmentation, leading to wasted memory and a time overhead for region management. Again, in the presence of region inference this problem can be more difficult to diagnose.

@SIGMazer
Copy link
Contributor Author

@SIGMazer can you explain in more detail how this region-based allocation works? Or provide a reference?

We pre-allocate a memory chunk (Arena) and use it to allocate all ASTNode instances. This approach allows us to free the entire Arena at once, instead of traversing each ASTNode individually and freeing them one by one.

Reference

* https://en.wikipedia.org/wiki/Region-based_memory_management

* https://www.gingerbill.org/article/2019/02/08/memory-allocation-strategies-002/

Very interesting! I wasn't familiar with that approach. Should we be concerned with the disadvantages? I understand that, as for the current usages of brainrot right now (which are small programs) this shouldn't be a concern.

Region-based memory management works best when the number of regions is relatively small and each contains many objects; programs that contain many sparse regions will exhibit internal fragmentation, leading to wasted memory and a time overhead for region management. Again, in the presence of region inference this problem can be more difficult to diagnose.

I don't think this affects our case because we allocate a single contiguous region for all AST allocations, meaning we won't encounter any sparse regions. In the future, if we need to delete or transform nodes before freeing the entire Arena, we can introduce optimizations such as merging underutilized regions. Alternatively, we could use a bump allocator, which treats the region as a stack to avoid fragmentation. However, in our current case, I believe our implementation will not face any issues.

@SIGMazer SIGMazer requested a review from araujo88 February 12, 2025 16:20
@araujo88 araujo88 merged commit 0886781 into main Feb 12, 2025
2 checks passed
@araujo88 araujo88 deleted the refactor/mem_allocation branch February 12, 2025 21:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request refactor Modify existing working code but keeping the same functionality.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants