Skip to content

Conversation

@anikser
Copy link
Owner

@anikser anikser commented Jul 20, 2025

Summary

  • Replaced unsafe manual memory management with safe arena-based heap allocation
  • Implemented mark & sweep garbage collection with tri-color marking
  • Fixed DST implementation issues by using Vec<u8> for string storage

Changes

New Heap Module (src/common/heap.rs)

  • Arena allocator: Objects stored in a Vec<Option<GcObject>> with index-based references
  • Free list: Reuses freed object slots for better memory efficiency
  • GC implementation: Mark & sweep with white/gray/black tri-color marking
  • Root management: Explicit root set tracking for accurate collection

Updated Value Representation

  • Changed Value::Object from raw pointer to ObjectRef (index)
  • Removed unsafe Obj struct with manual linked list
  • Fixed Box construction issue with DST types

VM Integration

  • Passes heap reference to compiler for string allocation
  • Manages GC roots for stack values during execution
  • Updated string concatenation to use heap allocation
  • Proper object display handling through heap lookups

Benefits

  • Memory safety: No more raw pointers or manual deallocation
  • Automatic GC: Configurable threshold-based collection
  • Better performance: Arena allocation reduces fragmentation
  • Rust idiomatic: Uses safe abstractions throughout

Test plan

  • Basic string allocation works
  • String concatenation allocates properly
  • GC can be triggered manually
  • Add comprehensive GC stress tests
  • Benchmark GC performance

🤖 Generated with Claude Code

This commit introduces a comprehensive heap allocation and garbage collection system to replace the previous unsafe manual memory management approach.

Key changes:
- Add new heap module with arena-based object allocation using indices instead of raw pointers
- Implement mark & sweep garbage collection with tri-color marking (white/gray/black)
- Replace unsafe DST implementation with safe Vec<u8> based string storage
- Update Value enum to use ObjectRef (index) instead of raw Obj pointers
- Integrate heap with VM and compiler for proper string allocation
- Add GC root management for stack values during execution
- Fix string concatenation to use heap allocation
- Remove all manual memory management code

The new system provides:
- Memory safety through safe Rust abstractions
- Automatic memory reclamation with configurable GC thresholds
- Better performance via arena allocation with free list
- Proper tracking of allocated memory and live objects

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
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