BorrowScope Runtime
The runtime tracking system for BorrowScope that records ownership and borrowing events during program execution.
Features
- Zero-cost abstractions: Tracking functions are inlined and return values unchanged
- Type safety: Generic functions work with any type without boxing
- Thread safety: All operations are thread-safe using efficient synchronization
- Event sourcing: Store events and build graphs on demand
- JSON export: Export tracking data for visualization
Usage
use *;
// Track variable creation
let x = track_new;
// Track borrowing
let r = track_borrow;
// Track drops
track_drop;
track_drop;
// Get events
let events = get_events;
println!;
// Build ownership graph
let graph = get_graph;
println!;
// Export to JSON
export_json.unwrap;
API Overview
Tracking Functions
track_new(name, value)- Track variable creationtrack_borrow(name, value)- Track immutable borrowtrack_borrow_mut(name, value)- Track mutable borrowtrack_move(from, to, value)- Track ownership movetrack_drop(name)- Track variable drop
Query Functions
get_events()- Get all tracked eventsget_graph()- Build ownership graph from eventsreset()- Clear all tracking data
Export Functions
export_json(path)- Export to JSON file
Architecture
The runtime uses an event sourcing pattern:
- Track operations as events (New, Borrow, Move, Drop)
- Store events in a thread-safe global tracker
- Build graphs from event streams on demand
- Export data to JSON for visualization
Performance
- Single operation: ~75ns
- 1000 operations: ~150μs
- JSON export (1000 events): ~1ms
- Memory: ~80 bytes per event
See BASELINE.md for detailed performance metrics.
Testing
# Run all tests (single-threaded due to global state)
# Run benchmarks
Documentation
# Generate and open documentation
Error Handling
The runtime uses a custom Result<T> type with comprehensive error variants:
SerializationError- JSON serialization failedIoError- File I/O errorExportError- Export operation failedInvalidEventSequence- Invalid event dataLockError- Lock acquisition failed
Thread Safety
All tracking operations are thread-safe. The runtime uses:
parking_lot::Mutexfor efficient locking (40-60% faster than std)AtomicU64for lock-free timestamp generation- Event sourcing to avoid complex concurrent graph updates
License
See the main BorrowScope repository for license information.